我仍然遇到一个越界错误,我不知道为什么。我添加了一个print语句来查看firstList和secondList实际上是什么,并且它都搞砸了我将在下面发布输出,所以也许有人可以看到出了什么问题。
输出:
Names before sorting: [Justin, Butch, Mandy, Sarah, Jack, Natalie, Brent, Ashley]
[Justin, null, null, null]
[Justin, Butch, null, null]
[Justin, Butch, Mandy, null]
[Justin, Butch, Mandy, Sarah]
[Natalie, null, null, null]
[Natalie, Natalie, null, null]
[Natalie, Natalie, Natalie, null]
[Natalie, Natalie, Natalie, Natalie]
[Justin, null]
[Justin, Butch]
[Sarah, null]
[Sarah, Sarah]
[Justin]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at MergeSorter.sort(MergeSorter.java:22)
at MergeSorter.sort(MergeSorter.java:25)
at MergeSorter.sort(MergeSorter.java:25)
at MergeSorter.main(MergeSorter.java:68)
正如你所看到的,阵列都搞砸了,我不知道为什么,请帮忙!
import java.util.*;
public class MergeSorter
{
public static void sort(String[] names)
{
if (names.length <= 1)
{
return;
}
String[] firstList = new String[names.length / 2];
String[] secondList = new String[names.length - firstList.length];
for (int i = 0; i < firstList.length; i++)
{
firstList[i] = names[i];
System.out.println(Arrays.toString(firstList));
}
for (int i = 0; i < secondList.length; i++)
{
secondList[i] = names[firstList.length + 1];
System.out.println(Arrays.toString(secondList));
}
sort(firstList);
sort(secondList);
merge(firstList, secondList, names);
}
private static void merge(String[] firstList, String[] secondList, String[] names)
{
int first = 0;
int second = 0;
int names1 = 0;
while (first < firstList.length && second < secondList.length)
{
if (firstList[first].compareTo(secondList[second]) < 0)
{
names[names1] = firstList[first];
first++;
}
else
{
names[names1] = secondList[second];
second++;
}
names1++;
}
while(first < firstList.length)
{
names[names1] = firstList[first];
first++;
names1++;
}
while(second < secondList.length)
{
names[names1] = secondList[second];
second++;
names1++;
}
}
public static void main(String[] args)
{
String[] names = {"Justin", "Butch", "Mandy", "Sarah", "Jack", "Natalie", "Brent", "Ashley"};
System.out.println("Names before sorting: " + Arrays.toString(names));
sort(names);
System.out.println("Names after sorting: " + Arrays.toString(names));
}
}
答案 0 :(得分:0)
您的错误在第26行:
secondList[i] = names[firstList.length + 1];
这显然超出了数组的范围,将其更改为:
secondList[i] = names[firstList.length + i];
看看你是否得到了理想的结果:
Names before sorting: [Justin, Butch, Mandy, Sarah, Jack, Natalie, Brent, Ashley]
[Justin, null, null, null]
[Justin, Butch, null, null]
[Justin, Butch, Mandy, null]
[Justin, Butch, Mandy, Sarah]
[Jack, null, null, null]
[Jack, Natalie, null, null]
[Jack, Natalie, Brent, null]
[Jack, Natalie, Brent, Ashley]
[Justin, null]
[Justin, Butch]
[Mandy, null]
[Mandy, Sarah]
[Justin]
[Butch]
[Mandy]
[Sarah]
[Jack, null]
[Jack, Natalie]
[Brent, null]
[Brent, Ashley]
[Jack]
[Natalie]
[Brent]
[Ashley]
Names after sorting: [Ashley, Brent, Butch, Jack, Justin, Mandy, Natalie, Sarah]
答案 1 :(得分:0)
You can use this code as well to compare the string as it is simple to compare string :
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});