cch*_*hua 1 java string vector arraylist
假设文本文件包含:
He is a boy .
She is sick .
Ali is playing .
We are eating .
The dog is barking .
He and his brother are running .
Run Code Online (Sandbox Code Playgroud)
如何将所有句子放入数组列表格式:
He is
is a
a boy
boy .
She is
is sick
sick .
Run Code Online (Sandbox Code Playgroud)
等等.
我使用数组列表来避免继续读取文本文件来执行某些任务是否可能?或者我需要使用vector来做到这一点?
这应该成功.我没有测试它.这只是一行的代码,你需要一些东西来扫描你的输入并消耗每一行;
String line = "He is a boy .";
String[] arr = line.split(" ");
ArrayList<String> newLines = ArrayList<String>();
for (int i = 1; i < arr.length; i++){
newLines.add(arr[i-1]+" "+arr[i]);
}
Run Code Online (Sandbox Code Playgroud)