다음과 같은 txt 파일이 있습니다.
it was the best of times it was the worst of times
it was the age of wisdom it was the age of foolishness
it was the epoch of belief it was the epoch of incredulity
it was the season of light it was the season of darkness
it was the spring of hope it was the winter of despair
그리고 각 단어를 쉼표로 구분하여 하나의 배열 목록에 넣고 싶습니다. 지금까지 다음이 있습니다.
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class arraylist{
public static void main(String[] args) {
try {
Scanner s = new Scanner(new File("tinyTale.txt"));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
list.add(s.next());
System.out.println(list);
}
s.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
출력으로 다음을 얻습니다.
[it]
[it, was]
[it, was, the]
[it, was, the, best]
[it, was, the, best, of]
[it, was, the, best, of, times]
[it, was, the, best, of, times, it]
[it, was, the, best, of, times, it, was]
[it, was, the, best, of, times, it, was, the]
[it, was, the, best, of, times, it, was, the, worst]
[it, was, the, best, of, times, it, was, the, worst, of]
[it, was, the, best, of, times, it, was, the, worst, of, times]
[it, was, the, best, of, times, it, was, the, worst, of, times, it]
[it, was, the, best, of, times, it, was, the, worst, of, times, it, was]
[it, was, the, best, of, times, it, was, the, worst, of, times, it, was, the]
[it, was, the, best, of, times, it, was, the, worst, of, times, it, was, the, age]
[it, was, the, best, of, times, it, was, the, worst, of, times, it, was, the, age, of]
[it, was, the, best, of, times, it, was, the, worst, of, times, it, was, the, age, of, wisdom]
[it, was, the, best, of, times, it, was, the, worst, of, times, it, was, the, age, of, wisdom, it]
[it, was, the, best, of, times, it, was, the, worst, of, times, it, was, the, age, of, wisdom, it, was]
[it, was, the, best, of, times, it, was, the, worst, of, times, it, was, the, age, of, wisdom, it, was, the]
... 나머지 내용을 계속 진행하므로 공간이있을 때마다 다시 시작됩니다. 인쇄되는 마지막 것은 제가 필요한 것입니다. 어떤 도움을 주시면 감사하겠습니다!
이것은 println 문이 루프 내에 있기 때문입니다. 원하는 출력을 보려면 루프 System.out.println(list);
밖으로 이동 while
하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다