存放在字符串中所有可能的子[]
我想存储所有可能的子串在String []
。我试过这个,但得到一个错误:存放在字符串中所有可能的子[]
public void sub(String word){
String [] Str=new String[100];
int n=0;
for (int from = 0; from < word.length(); from++) {
for (int to = from + 1; to <= word.length(); to++) {
str[n]=word.substring(from, to);
n++;
System.out.println(str[n]);
}
}
}
什么是解决方案?
error is: cannot find symbol, variable str, loction: class substring
那么,这相当清楚地告诉你错误是什么:你还没有申报str
。您已宣布Str
,但Java的标识是区分大小写的,str
和Str
是不一样的标识符。
因此改变
String [] Str=new String[100];
到
String [] str=new String[100];
// ^--- lower case
之前,当你没有说是什么错误,有一对夫妇的其他东西Pshemo和我(还有其他)注意到:
您有一个顺序的问题在这里:
str[n]=word.substring(from, to);
n++;
System.out.println(str[n]);
...因为输出字符串之前,你递增n
,你总是会输出null
。只需动增量修复:
str[n]=word.substring(from, to);
System.out.println(str[n]);
n++;
可能会出现更长的话,其中的子串数可以多于100应避免创建固定大小的数组的情况下,但尽量使用动态大小集合另一个可能的问题像List
List<String> str = new ArrayList<String>();
放或读到这里的元素只需使用str.add(substring)
和str.get(index)
。
tnx。但它只是打印一个,al,l。 – user3359479
@Pshemo现在它工作正常。全部都是。 – user3359479
现在我有String [] wordsArray,我想查找单词数组Array [i]是子串换句话说数组。任何解决方案 – user3359479
有什么错误? – PakkuDon
当你输入你的问题时,它旁边有一个橙色的方框,标题为“如何格式化**”。值得一读。顶部还有一个工具栏,用于制作格式,标记代码等等,方便,并在下方显示预览区域,以准确显示您的问题在发布时的样子。对于你的下一个问题,请使用这些工具。 (另外,缩进你的代码。)这次我为你纠正了一些事情。 –
好吧,我明白了。错误是:无法找到符号,变量str,loction:class substring – user3359479