On every iteration of your for loop you are overwriting listOfWords
by wrapping your latest words
array in a new list. So effectively you will always just have your most recent line's list of words as the only item listOfWords
.
To fix this modify following below the body of your for
statement similar to the following:
public static void main(String[] args) {
String[] lines = {"Hello World", "Welcome to StackOverFlow"};
List<String> words;
List<List<String>> listOfWords = new ArrayList<List<String>>(); // Must be instantiated or you will get NullPointerException
for (int i = 0; i < lines.length; i++) {
words = Arrays.asList(lines[i].split(" "));
listOfWords.add(words); //Add to `listOfWords` instead of replace with a totally new one
}
System.out.println(listOfWords);
}
Some of the syntax was not correct so I fixed it. In that process I converted lines
into an Array. Tested and confirmed output.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…