Java新手实现提取最长字符串功能遇cannot find symbol错误求提示
提示:解决"cannot find symbol: variable list"错误
Hey there, let's break down what's going on here with your code:
- First off, the error you're seeing is all about variable scope. Right now, you're declaring your
ArrayList<String> listinside the firstforloop. That means each time the loop runs, a brand new list gets created, and once that iteration of the loop finishes, that list is no longer accessible outside of it. When you try to uselistlater (outside the loop), the compiler has no idea what you're talking about because it doesn't exist in that scope. - A quick fix hint: Move the line where you initialize
listbefore the firstforloop. That way, it's created once and accessible throughout the entire method. - One extra little thing to double-check: In your inner loop, you're using
inputArray[i]to check the length, but that's just the element from the outer loop's current iteration. You probably want to be checking each element in the array (inputArray[j]) instead to make sure you're adding all the longest strings.
内容的提问来源于stack exchange,提问作者Szewniak
相关产品推荐
相关产品推荐

