JAVA对字符串数组进行排序,添加缺失数字。

huangapple go评论80阅读模式
英文:

JAVA Sort String Array Add Missing Numbers

问题

在Java中,我有一个字符串数组。它包含了不同编号的字符串。例如:x1,x3,x5,x9,y1,y3,y6,y9,z14,z17,z22,z50。我该如何按照字母和数字对这个数组进行排序,并按顺序添加缺失的字符串元素(x2等)?

英文:

In java, I have a string array. It contains strings with different numbers. For example: x1,x3,x5,x9,y1,y3,y6,y9,z14,z17,z22,z50. How can I sort this array by both letters and numbers, and add the missing string elements (x2...) in order?

答案1

得分: 1

这是解决方案的翻译:

1. 对数组进行排序
2. 获取每个字母的最小和最大数字并将它们保存在不同的映射中
3. 对于每个字母从其保存的范围内添加到列表中

以下是解决方案

public static void main(String[] args) {
String[] arr = new String[] {"x1","x3","x5","x9","y1","y3","y6","y9","z14","z17","z22","z50"};
System.out.println(Arrays.toString(addMissing(arr)));
}
private static String[] addMissing(String[] arr) {
Arrays.sort(arr);
Map<String,Integer> min = new HashMap<>();
Map<String,Integer> max = new HashMap<>();
for(int i = 0; i < arr.length; i++) {
String str = arr[i];
int number = getNum(str);
String letter = getChar(str);
if(min.containsKey(letter)) {
if(min.get(letter) > number)
min.put(letter, number);
}else {
min.put(letter, number);
}
if(max.containsKey(letter)) {
if(max.get(letter) < number)
max.put(letter, number);
}else {
max.put(letter, number);
}
}
List list = new ArrayList<>();
for(String key : min.keySet()) {
int minNumber = min.get(key), maxNumber = max.get(key);
for(int i = minNumber; i <= maxNumber; i++)
list.add(key+i);
}
String[] res = new String[list.size()];
for(int i = 0; i < list.size(); i++) res[i] = list.get(i);
return res;
}
private static String getChar(String str) {
return str.replaceAll("\d", "");
}
private static int getNum(String str) {
return Integer.parseInt(str.replaceAll("\D+", ""));
}

输出:

[x1, x2, x3, x4, x5, x6, x7, x8, x9, y1, y2, y3, y4, y5, y6, y7, y8, y9, z14, z15, z16, z17, z18, z19, z20, z21, z22, z23, z24, z25, z26, z27, z28, z29, z30, z31, z32, z33, z34, z35, z36, z37, z38, z39, z40, z41, z42, z43, z44, z45, z46, z47, z48, z49, z50]

希望这有助于您理解代码和解决方案。

英文:

A simple way to do this would be the following:

  1. Sort the array.
  2. Get the minimum and the maximum number of each letter and save them in separate maps.
  3. For each letter, add to a list from the specified saved range of it.

Here is the solution:

public static void main(String[] args) {
	String[] arr = new String[] {&quot;x1&quot;,&quot;x3&quot;,&quot;x5&quot;,&quot;x9&quot;,&quot;y1&quot;,&quot;y3&quot;,&quot;y6&quot;,&quot;y9&quot;,&quot;z14&quot;,&quot;z17&quot;,&quot;z22&quot;,&quot;z50&quot;};
	System.out.println(Arrays.toString(addMissing(arr)));
}
private static String[] addMissing(String[] arr) {
	Arrays.sort(arr);
	Map&lt;String,Integer&gt; min = new HashMap&lt;&gt;();
	Map&lt;String,Integer&gt; max = new HashMap&lt;&gt;();
	for(int i = 0; i &lt; arr.length; i++) {
		String str = arr[i];
		int number = getNum(str);
		String letter = getChar(str);
		if(min.containsKey(letter)) {
			if(min.get(letter) &gt; number)
				min.put(letter, number);
		}else {
			min.put(letter, number);
		}
		if(max.containsKey(letter)) {
			if(max.get(letter) &lt; number)
				max.put(letter, number);
		}else {
			max.put(letter, number);
		}
	}
	List&lt;String&gt; list = new ArrayList&lt;&gt;();
	for(String key : min.keySet()) {
		int minNumber = min.get(key), maxNumber = max.get(key);
		for(int i = minNumber; i &lt;= maxNumber; i++)
			list.add(key+i);
	}
	String[] res = new String[list.size()];
	for(int i = 0; i &lt; list.size(); i++) res[i] = list.get(i);
	return res;
}
private static String getChar(String str) {
	return str.replaceAll(&quot;\\d&quot;, &quot;&quot;);
}
private static int getNum(String str) {
	return Integer.parseInt(str.replaceAll(&quot;\\D+&quot;,&quot;&quot;));
}

Output:

[x1, x2, x3, x4, x5, x6, x7, x8, x9, y1, y2, y3, y4, y5, y6, y7, y8, y9, z14, z15, z16, z17, z18, z19, z20, z21, z22, z23, z24, z25, z26, z27, z28, z29, z30, z31, z32, z33, z34, z35, z36, z37, z38, z39, z40, z41, z42, z43, z44, z45, z46, z47, z48, z49, z50]

答案2

得分: 0

尝试一下这个。

String[] arr = new String[] {"x1","x3","x5","x9","y1","y3","y6","y9","z14","z17","z22","z50"};
List<String> result = Arrays.stream(arr)
    .collect(Collectors.groupingBy(s -> s.substring(0, 1),
        Collectors.mapping(s -> Integer.parseInt(s.substring(1)), Collectors.toList())))
    .entrySet().stream()
    .flatMap(e -> IntStream.rangeClosed(
        e.getValue().stream().mapToInt(Integer::intValue).min().getAsInt(),
        e.getValue().stream().mapToInt(Integer::intValue).max().getAsInt())
        .mapToObj(i -> e.getKey() + i))
    .sorted()
    .collect(Collectors.toList());
System.out.println(result);

输出结果

[x1, x2, x3, x4, x5, x6, x7, x8, x9, y1, y2, y3, y4, y5, y6, y7, y8, y9, z14, z15, z16, z17, z18, z19, z20, z21, z22, z23, z24, z25, z26, z27, z28, z29, z30, z31, z32, z33, z34, z35, z36, z37, z38, z39, z40, z41, z42, z43, z44, z45, z46, z47, z48, z49, z50]

groupingBy 的中间结果

{x=[1, 3, 5, 9], y=[1, 3, 6, 9], z=[14, 17, 22, 50]}
英文:

Try this.

String[] arr = new String[] {&quot;x1&quot;,&quot;x3&quot;,&quot;x5&quot;,&quot;x9&quot;,&quot;y1&quot;,&quot;y3&quot;,&quot;y6&quot;,&quot;y9&quot;,&quot;z14&quot;,&quot;z17&quot;,&quot;z22&quot;,&quot;z50&quot;};
List&lt;String&gt; result = Arrays.stream(arr)
    .collect(Collectors.groupingBy(s -&gt; s.substring(0, 1),
        Collectors.mapping(s -&gt; Integer.parseInt(s.substring(1)), Collectors.toList())))
    .entrySet().stream()
    .flatMap(e -&gt; IntStream.rangeClosed(
        e.getValue().stream().mapToInt(Integer::intValue).min().getAsInt(),
        e.getValue().stream().mapToInt(Integer::intValue).max().getAsInt())
        .mapToObj(i -&gt; e.getKey() + i))
    .sorted()
    .collect(Collectors.toList());
System.out.println(result);

output

[x1, x2, x3, x4, x5, x6, x7, x8, x9, y1, y2, y3, y4, y5, y6, y7, y8, y9, z14, z15, z16, z17, z18, z19, z20, z21, z22, z23, z24, z25, z26, z27, z28, z29, z30, z31, z32, z33, z34, z35, z36, z37, z38, z39, z40, z41, z42, z43, z44, z45, z46, z47, z48, z49, z50]

Intermediate result of groupingBy

{x=[1, 3, 5, 9], y=[1, 3, 6, 9], z=[14, 17, 22, 50]}

huangapple
  • 本文由 发表于 2020年8月16日 03:10:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63429873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定