본문 바로가기

BackEnd/구 생활코딩 자바

List<E> 인터페이스를 구현하는 컬렉션 클래스들

반응형

1. List<E> 인터페이스를 구현하는 제네릭 클래스들의 공통점

1) 동일한 인스턴스의 중복 저장을 허용한다

2) 인스턴스의 저장 순서가 유지된다.


2.  List<E> 인터페이스를 구현하는 제네릭 클래스들

1) ArrayList<E>

- ArrayList<E>는 이름이 의미하듯이 배열 기반으로 데이터를 저장한다.

- 데이터의 저장을 위해서 인덱스 정보를 별도로 관리할 필요가 없다.

- 인데스를 통해 데이터의 참조가 용이해서 빠른 참조가 가능하다.

- 데이터의 삭제를 위한 추가적인 코드의 작성이 필요 없다.

- 데이터의 삭제에 필요한 연산과정이 매우 길다.

- 저장되는 인스턴스의 수에 따라서 크기가 자동으로 늘어나 배열과 달리 길이를 고민하지 않아도 된다.

- 저장소의 용량을 늘리는 과정에서 많은 시간이 소요된다.    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.ArrayList;
 
class a01_IntroArrayList
{
    public static void main(String[] args)
    {
        ArrayList<Integer> list=new ArrayList<Integer>();//
        
        /* 데이터의 저장 */
        list.add(new Integer(11));//순서대로 저장
        list.add(new Integer(22));
        list.add(new Integer(33));
        
        /* 데이터의 참조 */
        System.out.println("1차 참조");
        for(int i=0; i<list.size(); i++)
            System.out.println(list.get(i));//0이 첫번째
        
        /* 데이터의 삭제 */
        list.remove(0);//첫번째 삭제 ,인스턴스의 참조값이지워지는거지,인스턴스가 소멸되는것은 아니다
        System.out.println("2차 참조");
        for(int i=0; i<list.size(); i++)
            System.out.println(list.get(i));    
    }
}
cs


 2) LinkedList<E>

- 배열이 아닌 리스트(서로서로 연결하는 방식으로 데이터를 저장)라는 자료구조를 기반으로 데이터를 저장.

- 저장소의 용량을 늘리는 과정이 간단하다.

- 데이터의 삭제가 매우 간단하다.

- 데이터의 참조가 다소 불편하다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.LinkedList;
 
class a03_IntroLinkedList
{
    public static void main(String[] args)
    {
        LinkedList<Integer> list=new LinkedList<Integer>();
        
        /* 데이터의 저장 */
        list.add(new Integer(11));
        list.add(new Integer(22));
        list.add(new Integer(33));
        
        /* 데이터의 참조 */
        System.out.println("1차 참조");
        for(int i=0; i<list.size(); i++)
            System.out.println(list.get(i));
        
        /* 데이터의 삭제 */
        list.remove(0);
        System.out.println("2차 참조");
        for(int i=0; i<list.size(); i++)
            System.out.println(list.get(i));    
    }
}

cs



반응형