JAVA 프로그래밍

문제

다양한 객체를 포함할 수 있는 가변 길이 배열 Vector를 생성하고 활용하는 문제입니다 이를 해결하는 프로그램의 다음 실행상태에 대해 빈칸을 채우세요 

프로그램의 실행순서 및 실행상태

1  public static void main( String[] args ) {  

2   Vector<String> list = new Vector<String>( 3 ); 

3   list.add( "first " ); 

4   list.add( "second" ); 

5   list.add( "third " ); 
  main()
 list
  [0] ... (obj01)    'first'  ... (obj02)
  [1]                'second'  ... (obj03)
  [2]                'third'  ... (obj04)
                                                 

6   print( list );

Pb  public static void print( Vector<String> list ) {

공간크기 3, 총원소개수 3, (0) first  (1) second (2) third 

P1   if( list.isEmpty() )

P3   else if( list.contains( "cut in line" ) )

Pe  }

7   list.add( 0, "cut in line" ); 

8   list.add( 4, null ); 
  main()
 list
  [0] ... (obj01)    'first'  ... (obj02)
  [1]                'second'  ... (obj03)
  [2]                'third'  ... (obj04)
  [3]                'cut in line'  ... (obj05)
  [4]            
  [5]                      
                                                 

9   print( list );

Pb  public static void print( Vector<String> list ) {

공간크기 6, 총원소개수 5, (0) cut in line (1) first  (2) second (3) third  (4) null

P1   if( list.isEmpty() )

P3   else if( list.contains( "cut in line" ) )

P4    System.out.print( " 새치기 발생" );
 새치기 발생

Pe  }

10   list.remove( 0 ); 

11   list.remove( 0 ); 
  main()
 list
  [0] ... (obj01)    'first'  ... (obj02)
  [1]                'second'  ... (obj03)
  [2]                'third'  ... (obj04)
  [3]                          'cut in line'  ... (obj05)
  [4]                      
  [5]                      
                                                 

12   print( list );

Pb  public static void print( Vector<String> list ) {

공간크기 6, 총원소개수 3, (0) second (1) third  (2) null

P1   if( list.isEmpty() )

P3   else if( list.contains( "cut in line" ) )

Pe  }

13   list.clear();
  main()
 list
  [0]           ... (obj01)
  [1]                      
  [2]                      
  [3]                      
  [4]                      
  [5]                      
                                                 

14   print( list );

Pb  public static void print( Vector<String> list ) {

공간크기 6, 총원소개수 0,

P1   if( list.isEmpty() )

P2    System.out.print( " 빈 벡터" );
 빈 벡터

Pe  }

15  }


프로그램 코드

	import java.util.Vector;
	
	public class VectorUsage 
	{
Pb		public static void print( Vector<String> list ) {
			System.out.print( "공간크기 " + list.capacity() + ", 총원소개수 " + list.size() + "," );
			for( int i = 0; i < list.size(); i++ )
				System.out.print( " (" + i + ") " + list.get(i) );
P1			if( list.isEmpty() )
P2				System.out.print( " 빈 벡터" );
P3			else if( list.contains( "cut in line" ) )
P4				System.out.print( " 새치기 발생" );
			System.out.println();
Pe		}
		
1		public static void main( String[] args ) { 	
2			Vector<String> list = new Vector<String>( 3 ); 
3			list.add( "first " ); 
4			list.add( "second" ); 
5			list.add( "third " ); 
6			print( list );
			
7			list.add( 0, "cut in line" ); 
8			list.add( 4, null ); 
9			print( list );
		
10			list.remove( 0 ); 
11			list.remove( 0 ); 
12			print( list );
					
13			list.clear();
14			print( list );
15		}
	}