String[] words = text.split("\\s+");
for(int j = 0; j < words.size(); j++) {
System.out.println(j)
}
我在上面的循环中遇到错误:
HelloWorld.java:18:错误:找不到符号 for(int j = 0; j&lt; words.size(); j ++){ ^ 符号:方法大小() location:String []类型的变量字 1错误
不确定合成有什么问题?
答案 0 :(得分:1)
将words.size()
更改为words.length
。
因为数组没有size()
方法。
答案 1 :(得分:1)
您正尝试在数组上调用size()
,数组具有length
属性(不是size
)。此外,我认为您要打印words[j]
(不是j
)。最后,您可以使用for each
loop之类的
String[] words = text.split("\\s+");
for(String word : words) {
System.out.println(word);
}
或者,您可以使用Arrays.toString(Object[])
并跳过循环
System.out.println(Arrays.toString(text.split("\\s+")));