문제
String 객체를 포함하는 가변 길이 배열 ArrayList를 생성하고 활용하는 문제입니다 이를 해결하는 프로그램의 다음 실행상태에 대해 빈칸을 채우세요
프로그램의 실행순서 및 실행상태
1 public static void main( String[] args ) {
2 ArrayList<String> words = new ArrayList<String>();
3 while( true ) {
단어를 입력하세요(종료시quit): carrot
4 if ( word.equals("quit") )
6 words.add( word );
3 while( true ) {
단어를 입력하세요(종료시quit): banana
4 if ( word.equals("quit") )
6 words.add( word );
3 while( true ) {
단어를 입력하세요(종료시quit): apple
4 if ( word.equals("quit") )
6 words.add( word );
3 while( true ) {
단어를 입력하세요(종료시quit): quit
4 if ( word.equals("quit") )
5 break;
7 Collections.sort( words );
apple banana carrot
8 }
프로그램 코드
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class WordArrayList {
1 public static void main( String[] args ) {
Scanner scan = new Scanner( System.in );
2 ArrayList<String> words = new ArrayList<String>();
3 while( true ) {
System.out.print( "단어를 입력하세요(종료시quit): " );
String word = scan.nextLine();
4 if ( word.equals("quit") )
5 break;
6 words.add( word );
}
7 Collections.sort( words );
for( String word : words )
System.out.print( word + " " );
scan.close();
8 }
}