遇到阵列问题
问题描述:
我仍然遇到了界限错误,我不知道为什么。我添加了一个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));
}
}
答
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);
}
});
答
你的错误是在第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]
而不是投票,你可以告诉我该怎么改变这个帖子。我在这里寻求帮助! – Dustin 2014-09-30 13:28:37
我没有投票给你,但它可能是因为这看起来像功课,你发布了很多代码,并基本上要求我们为你调试。这就是说,找出数组太小的唯一最简单的方法就是使用调试器并遍历代码,直到发生异常 – sunrize920 2014-09-30 13:30:36
这就是我认为这个网站的目的!这是作业,但我已经完成了这项工作。这不像我要求的代码,所以我可以复制和粘贴它。我无法弄清楚我不断收到的错误。我在哪里可以找到调试器?我是新来的Java。谢谢! – Dustin 2014-09-30 13:32:41