문제
제네릭 리스트에 점수 및 단어를 저장하고 출력하는 문제입니다 실행순서를 클릭하세요
86 89 90
apple banana carrot
프로그램 코드
public class GenericListMain {
1 public static void main( String[] args ) {
2 GenericList<Integer> scores =
3 new GenericList<Integer>( 3 );
4 scores.add( 86 );
5 scores.add( 89 );
6 scores.add( 90 );
7 for( int i = 0; i < scores.size; i++ )
8 System.out.print( scores.get( i ) + " " );
System.out.println();
9 GenericList<String> words =
10 new GenericList<String>( 3 );
11 words.add( "apple" );
12 words.add( "banana" );
13 words.add( "carrot" );
14 for( int i = 0; i < words.size; i++ )
15 System.out.print( words.get( i ) + " " );
16 }
}
public class GenericList<T> {
private Object[] list;
protected int size;
F1b public GenericList( int capacity ) {
size = 0;
F11 list = new Object[capacity];
F1e }
F2b public void add( T item ) {
F21 list[size++] = item;
F2e }
F3b public T get( int index ) {
F31 return (T)list[index];
F3e }
}