ヘルプ配列を終了すると、スレッド "main" java.lang.ArrayIndexOutOfBoundsExceptionで例外が発生し続ける:& &で検索を終了しようとしましたが、ルートの検索に戻ります。私はいくつかのアイデアやこれを修正するための任意の助けが必要になる良いことが必要です!このスニペットでJava配列例外
import java.io.*;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class Root_Words {
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) && i < Words.length){
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;
}
}
「i」が「Words.length - 1」になったら、「i + 1」は何ですか? Java配列は0または1ベースですか?また、13行目を指定する必要があります。さらに一貫してインデントしてください。 –
例外が発生した行を教えてください。私は13をカウントダウンしますが、それは「試行」です。例外トレースバックに上位5つかそれ以上のアイテムを投稿してください。 –
それは行13ではなく、それは実際に行40である while(){ – helloman