Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将所有可能的子字符串存储在字符串[]中_Java_String_Substring - Fatal编程技术网

Java 将所有可能的子字符串存储在字符串[]中

Java 将所有可能的子字符串存储在字符串[]中,java,string,substring,Java,String,Substring,我想将所有可能的子字符串存储在String[]中。我尝试了这个,但出现了一个错误: public void sub(String word){ String [] Str=new String[100];

我想将所有可能的子字符串存储在
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]);           
        }            
    }          
}             
public void子(字符串字){
字符串[]Str=新字符串[100];
int n=0;
for(int from=0;from
好的,这相当清楚地告诉您错误是什么:您没有声明
str
。您声明了
str
,但是Java的标识符区分大小写,
str
str
不是同一个标识符

所以改变

String [] Str=new String[100];


以前,当你没有说错误是什么的时候,我(和其他人)注意到了一些其他的事情:

这里有一个顺序问题:

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>();
List str=new ArrayList();

要在这里放置或读取元素,只需使用
str.add(substring)
str.get(index)

当您键入问题时,旁边有一个略带橙色的框,标题为“如何格式化”。值得一读。顶部还有一个工具栏,使格式化、标记代码等变得简单,下面有一个预览区,向您准确显示问题发布时的样子。对于下一个问题,请使用这些工具。(另外,缩进代码。)这一次我已经为您更正了。好的,我明白了。错误是:找不到符号,变量str,位置:class substring您编写变量
str
,并将其命名为
str
。大写优先,小写第二。->使用
str
always.tnx。但它只打印a,al,l.@Pshemo现在它工作正常了。tnx all.Now我有字符串[]wordsArray和我想找到wordsArray的numer[i]是其他wordsArray中的子字符串。有解决方案吗?@user3359479这是一个单独的问题,所以请为它创建单独的帖子。另外,不要忘了包含您试图解决它的代码。
List<String> str = new ArrayList<String>();