제네릭 리스트에 점수 및 단어를 저장하고 출력하는 문제입니다 이를 해결하는 다음 프로그램을 해석하세요
86 89 90 apple banana carrot
프로그램 시작
먼저, 점수 초기화 및 출력
단어 초기화 및 출력
프로그램 종료
제네릭 리스트 클래스
리스트 생성 초기화
리스트 항목 추가
리스트 항목 반환
// 파일명 : ./Chapter19/GenericListMain.java
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 }
}
// 파일명 : ./Chapter19/GenericList.java
// 제네릭 리스트 클래스
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 }
}
※ 실행순서 및 메모리상태는 A키(이전) 및 D키(다음)를 눌러도 확인할 수 있습니다