我之前遇到array out of bounds
的问题,但我不再收到了。
我的问题是在使用我的ArrayList
时,会打印出来:
aba, aba, abaa, abaxxa, runner, runner, runners, school, school, schooling, schools, tasker, tasker
退出task
和run
。
我一直在看这个,我无法弄明白。任何帮助都会很棒!
import java.io.*;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class RootWords {
public static void main(String[] args) {
String Word;
List<String> xList = new ArrayList<String>();
try{
BufferedReader reader = new BufferedReader(new FileReader("Words.txt"));
while ((Word = reader.readLine()) != null) {
xList.add(Word);
}
}
catch (IOException ioe){
System.out.println("Problem opening input file");
}
String[] Words = new String[ xList.size() ];
Collections.sort(xList);
xList.toArray( Words );
int c;
String roots = "";
for( int i = 0; i < Words.length ; i++ ){
c = root(Words[i],Words[i+1]);
if(c >= 3 || c <=5 ){
roots = Words[i];
System.out.printf("%s\n", roots);
while(Words[i].startsWith(roots)){
System.out.printf("%s\n", Words[i]);
i++;
}
// i--;
}
}
}
public static int root(String a, String b){
int min;
if(a.length() < b.length() ){
min= a.length();
}
else{
min = b.length();
}
//return min;
int i;
i = 0;
while (i < min)
{
if (a.charAt(i) == b.charAt(i))
i++;
else break;
}
return i;
}
}
我没有使用我的代码接收数组,我无法打印完整列表。我得到一段时间与aba运行学校任务abaa任务员任务abaxaa亚军跑步者学校教育,但然后当我打印出aba aba abaa abaxaa亚军亚军跑步者学校学校学校任务员任务。因此我缺少任务并运行
答案 0 :(得分:0)
似乎在这里..
for( int i = 0; i < Words.length ; i++ ){
c = root(Words[i],Words[i+1]);
当i+1 = words.length
时会抛出ArrayIndexOutOfBounds
例外。
答案 1 :(得分:0)
您正在访问索引高于数组长度的数组,这就是抛出异常的原因。因此,您需要在访问阵列之前检查索引。以下是我的建议。
import java.io.*;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class RootWords {
public static void main(String[] args) {
String Word;
List<String> xList = new ArrayList<String>();
try{
BufferedReader reader = new BufferedReader(new FileReader("Words.txt"));
while ((Word = reader.readLine()) != null) {
xList.add(Word);
}
}
catch (IOException ioe){
System.out.println("Problem opening input file");
}
String[] Words = new String[ xList.size() ];
Collections.sort(xList);
xList.toArray( Words );
int c;
String roots = "";
for( int i = 0; i < Words.length; i++ )
{
if (i+1 == Words.length)
{
break;
}
c = root(Words[i],Words[i+1]);
if (c >= 3 || c <=5 )
{
roots = Words[i];
System.out.printf("%s\n", roots);
while(i < Words.length && Words[i].startsWith(roots))
{
System.out.printf("%s\n", Words[i]);
i++;
}
// i--;
}
}
}
public static int root(String a, String b){
int min;
if(a.length() < b.length() ){
min= a.length();
}
else{
min = b.length();
}
//return min;
int i;
i = 0;
while (i < min)
{
if (a.charAt(i) == b.charAt(i))
i++;
else break;
}
return i;
}
}
答案 2 :(得分:0)
for( int i = 0; i < Words.length ; i++ ){
c = root(Words[i],Words[i+1]);
在上面循环@上次迭代时
c = root(Words[i],Words[i+1]);
代码中的<+> i + 1代表ArrayIndexOutOfBounds exception
,因为i + 1 = Words.length和array index start form 0 to
Words.length-1
我没有给你最好的解决方案我只是指出你做错了什么..
for (int i = 0; i < Words.length; i++) {
c = root(Words[i], Words[i + 1]);
if (c >= 3 || c <= 5) {
roots = Words[i];
System.out.printf("%s\n", roots);
while (Words[i].startsWith(roots)) {
System.out.printf("%s\n", Words[i]);
if (i + 1 == Words.length|| !Words[i + 1].startsWith(roots)) /**
what is going on there when Words[i].startsWith(roots) tells false so before
that its increment your counter and in for loop it also increment ,So 2 consultative
increment for **i**
And 2nd Problem is when **i=Words.length-1** then i its also increment so that in
next iteration **Words[i].startsWith(roots)); i=Words.length** thats why it throw
exception ..*/ {
break;
}
i++;
}
// i--;
}
}
}
答案 3 :(得分:0)
使用Comparator或Comparable对对象的数组进行排序