gpt4 book ai didi

java - Java比较词

转载 作者:行者123 更新时间:2023-12-01 05:26:20 25 4
gpt4 key购买 nike

之前,我在array out of bounds上遇到问题,但现在不再收到该问题。

我的问题是使用我的ArrayList时,它将打印出:

aba, aba, abaa, abaxxa, runner, runner, runners, school, school, schooling, schools, tasker, tasker


省略 taskrun

我已经看了一段时间了,我无法弄清楚。任何帮助将是巨大的!

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 run school task abaa tasker任务abaxaa赛跑者赛跑者学校上课,但是当我打印出来aba aba abaa abaxaa赛跑者赛跑者学校学校上学任务者tasker。因此,我想念任务而逃避

最佳答案

好像在这里

for( int i = 0; i < Words.length ; i++ ){
c = root(Words[i],Words[i+1]);


i+1 = words.length时将抛出 ArrayIndexOutOfBounds异常。

关于java - Java比较词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9629300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com