1

我编写了一些代码来对用户输入的随机整数进行排序。我如何将其切换为对随机输入的字母进行排序?Aka,用户输入 j, s, g, w,程序输出 g, j, s, w?

for (int i = 0; i < random.length; i++) { //"random" is array with stored integers
                // Assume first value is x
                x = i;
                for (int j = i + 1; j < random.length; j++) {
                    //find smallest value in array (random)
                    if (random[j] < random[x]) {
                        x = j;
                    }
                }
                if (x != i) {
                    //swap the values if not in correct order
                    final int temp = random[i];
                    random[i] = random[x];
                    random[x] = temp;
                }
                itsATextArea.append(random[i] + "\n");// Output ascending order
            }

最初我希望(尽管我知道我是正确的可能性对我不利)用“String”替换所有“int”会起作用......自然我错了,意识到也许我必须列出哪个字母出现在哪个字母之前通过使用列表,例如 list.add("a"); 等等

如果这看起来像是我要求你们做所有的工作(我不是),我很抱歉,但我不完全确定如何开始做这件事,所以如果有人可以提供一些提示或提示,那就是最欣赏!

4

7 回答 7

5

您可以使用String.compareTo()来做到这一点:

改变这个:

int[] random = new int[sizeyouhad];
...
if (random[j] < random[x]) {
...
final int temp = random[i];

至:

String[] random = new String[sizeyouhad];
...
if (random[j].compareTo(random[x]) < 0) {
...
final String temp = random[i];

试用您的代码:

String[] random = new String[3];
random[0] = "b";
random[1] = "c";
random[2] = "a";
int x = 0;
//"random" is array with stored integers
for (int i = 0; i < random.length; i++) { 
    // Assume first value is x
    x = i;
    for (int j = i + 1; j < random.length; j++) {
        //find smallest value in array (random)
        if (random[j].compareTo(random[x]) < 0) {
            x = j;
        }
    }
    if (x != i) {
        //swap the values if not in correct order
        final String temp = random[i];
        random[i] = random[x];
        random[x] = temp;
    }
    System.out.println(random[i] + "\n");// Output ascending order
}
于 2013-06-10T18:55:26.660 回答
0

如果您只是想对字符串列表进行排序,您可能应该使用该java.util.Collections.sort方法,而不是编写自己的排序例程。

于 2013-06-10T18:54:52.457 回答
0

random原来是int[]?如果您已将其更改为String[],则可以使用String#compareTo方法来辨别一个字符串是否“小于”另一个字符串。

顺便说一句,您可以更改 to 的类型,random然后Comparable[]您可以使用相同的算法对类实现该接口的任何对象进行排序!

于 2013-06-10T18:56:03.857 回答
0

尝试使用Collections.sort()函数

List<String> l = Arrays.asList("j","s", "g","w");
Collections.sort(l);
于 2013-06-10T18:57:21.850 回答
0

如果您认为每个字符都是一个代码点[1],并且您想按 Unicode 代码点顺序[2] 进行排序,那么确实不需要更改您的逻辑。工作是从您获得的任何输入(String、char[] 等)转换为代码点的 int[]。

[1] - http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#codePointAt(int) [2] - http://en.wikipedia.org/wiki/代码点

于 2013-06-10T18:58:25.570 回答
0

Object您可以使用泛型使您的代码适用于任何类型。

于 2013-06-10T19:08:13.117 回答
0

以下代码非常简单并且运行良好(使用这个库,您可以用几行代码解决您的问题):

import static ch.lambdaj.Lambda.sort;
import static ch.lambdaj.Lambda.on;
import java.util.Arrays;
import java.util.List;

public class Test{
        public static void main(String[] args) {
            List<String> list =  Arrays.asList("1","102","-50","54","ABS");

            List<String> newList = sort(list, on(String.class));
            System.out.println(newList);//[-50, 1, 102, 54, ABS]


}
}

此代码使用 lambda 库(在此处下载网站)。在网站上找到这个例子:

List<Person> sorted = sort(persons, on(Person.class).getAge());
于 2013-06-10T19:32:53.933 回答