排序数组无法获得工作方法

时间:2013-04-05 22:33:16

标签: c

标记是一个关键字或标签,用于将您的问题与其他类似问题进行分类。标记是一个关键字或标签,用于将您的问题与其他类似问题进行分类。

2 个答案:

答案 0 :(得分:1)

在以下三个代码块中,我将首先编写您的行,然后编写它应该是什么:

String[] sort = new String[listNames];
String[] sort = new String[listNames.length];
                                    ^^^^^^^

for (int x = 0; x < sort; x++) {
for (int x = 0; x < sort.length; x++) {
                        ^^^^^^^

sort[x] = JOptionPane.showMessageDialog(" Tutor LAST NAME and FIRST NAME Listed in Alphabetically Order"+(x+1)+ " " + "For example: 'Smith, John'");
sort[x] = JOptionPane.showMessageDialog(null, " Tutor LAST NAME and FIRST NAME Listed in Alphabetically Order"+(x+1)+ " " + "For example: 'Smith, John'");
                                        ^^^^^

假设您真的想在排序之前创建一个新数组,那么您的方法应该是:

public static String[] sortNames(String[] listNames) {
    String[] copy = Arrays.copyOf(listNames, listNames.length);
    Arrays.sort(copy);
    return copy;
}

如果必须是方法,但您不必创建第二个数组,则可以这样做:

public static void sortNames(String[] listNames) {
    Arrays.sort(listNames);
}

答案 1 :(得分:0)

你应该改变

for (int x = 0; x < sort; x++) 

for (int x = 0; x < sort.length; x++)

代替。和

String[] sort = new String[listNames];

String[] sort = new String[listNames.length];

此外,JOptionPane.showMessageDialog()应该以这种方式使用:

JOptionPane.showMessageDialog(null," Tutor LAST NAME and FIRST NAME Listed in Alphabetically Order"+(x+1)+ " " + "For example: 'Smith, John'");