gpt4 book ai didi

java - 在java中仅对字符串数组中的数字元素进行排序?

转载 作者:行者123 更新时间:2023-11-30 08:17:42 25 4
gpt4 key购买 nike

我热衷于仅对字符串数组中的数字元素进行排序。我是用java做的。请帮我解决这个问题。

这是我的问题

对于给定的字符集,仅选择整数并按降序对它们进行排序并放置在其位置上,而其他字符位置保持不变。位置的变化只能是整数,不能是其他字符。

示例输入:-
d,1,4,c,9,6
109,87,911,b,645
77,19,#,.,95
8,99,14,2,5,6,49

示例输出:-
情况#1:d,9,6,c,4,1
案例#2:911,645,109,b,87
案例#3:95,77,#,.,19
案例 #4:99,49,14,8,6,5,2

感谢所有观众。请大家帮我解决这个Java问题

这是我的代码,到目前为止我已经尝试过。

import java.util.Arrays;
import java.util.Iterator;
import java.util.ArrayList;

class alphaNumeric {

public static void main(String a[]) {

String s1[] = new String[9];
ArrayList l_numList = new ArrayList();
ArrayList l_strList = new ArrayList();

s1[0] = "1000.1";
s1[1] = "100";
s1[2] = "xBC100";
s1[3] = "XBB100";
s1[4] = "TEST";
s1[5] = "AYZ2100";
s1[6] = "99";
s1[7] = "90";
s1[8] = "1000";
System.out.print("Before sorting, numbers are ");
for(int i = 0; i < s1.length; i++)
{
System.out.print(s1[i]+" ");
}
System.out.println();



for (int i = 0; i < s1.length; i++) {
if (isNumber(s1[i])) {
l_numList.add(s1[i]);
} else {
l_strList.add(s1[i]);
}
}
Object[] l_objArray = (Object[]) l_numList.toArray();
int l_intArray[] = new int[l_objArray.length];
for (int i = 0; i < l_objArray.length; i++) {
l_intArray[i] = Integer.parseInt((String) l_objArray[i]);
}
Arrays.sort(l_intArray);
for (int i = 0; i < l_intArray.length; i++) {
System.out.println("after Numsort: " + l_intArray[i]);
}

System.out.print("After sorting, numbers are ");
for(int i = 0; i < l_intArray.length; i++)
{
System.out.print(l_intArray[i]+" ");
}
Object[] l_strArray = (Object[]) l_strList.toArray();
Arrays.sort(l_strArray);
for (int i = 0; i < l_strArray.length; i++) {
System.out.println("after Strsort: " + l_strArray[i]);
}
}

static boolean isNumber(String s) {
String validChars = "0123456789";
boolean isNumber = true;

for (int i = 0; i < s.length() && isNumber; i++) {
char c = s.charAt(i);
if (validChars.indexOf(c) == -1) {
isNumber = false;
} else {
isNumber = true;
}
}
return isNumber;
}
}

最佳答案

我不知道你想做什么,但我会这样做

  • 仅提取整数列表创建它们在原始数组中出现的索引列表
  • 使用标准反向排序对该列表进行排序
  • 使用索引 List 将排序后的 List 值放回原始数组

工作示例

public class SortOnlyNumbers {
public static void main(String[] args) {
sortOnlyNumbers(new String[] { "d", "1", "4", "c", "9", "6" });
sortOnlyNumbers(new String[] { "109", "87", "911", "b", "645" });
sortOnlyNumbers(new String[] { "77", "19", "#", ".", "95" });
sortOnlyNumbers(new String[] { "8", "99", "14", "2", "5", "6", "49" });
}

private static void sortOnlyNumbers(String[] array) {
List<Integer> indexes = new ArrayList<Integer>();
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 0; i < array.length; i++) {
try {
numbers.add(Integer.parseInt(array[i]));
indexes.add(i);
} catch (NumberFormatException e) {
// don't care
}
}
Collections.sort(numbers, Collections.reverseOrder());
for (int i = 0; i < numbers.size(); i++) {
array[indexes.get(i)] = String.valueOf(numbers.get(i));
}
System.out.println(Arrays.toString(array));
}
}

输出

[d, 9, 6, c, 4, 1]
[911, 645, 109, b, 87]
[95, 77, #, ., 19]
[99, 49, 14, 8, 6, 5, 2]

关于java - 在java中仅对字符串数组中的数字元素进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29419903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com