- ArrayList보다 데이터의 추가/삭제 속도가 더 빠른 리스트
- 각 데이터 덩어리(노드)가 다음 데이터의와 이전 데이터의 위치를 함께 보관한다
- ArrayList와 같은 List 인터페이스이기 때문에 사용법은 거의 똑같다

 

add(Object);

LinkedList<String> fruits = new LinkedList();

fruits.add("strawBerry");
fruits.add("apple");
fruits.add("orange");
fruits.add("mandarin");

 

 

addFirst() : 첫번째 index에 원하는 값을 넣는다

addLast() : 마지막 index에 원하는 값을 넣는다

fruits.addFirst("watermelon");
fruits.addLast("banana");

 

 

 

add(index, data) : 원하는 위치에 데이터 추가

fruits.add(3, "coconut");

0 : watermelon 1: strawBerry 2: apple 3: coconut

 

remove()

삭제한 값을 반환해준다

 

remove(), removeFirst(): 맨 앞의 데이터(haed)를 지운다

-remove() 괄호안에 아무값도 넣지 않을시 맨앞의 데이터를 지운다

 

remove(index) : 원하는 위치의 값을 지운다

fruits.remove(2);

index 2번째 값인 apple이 지워진다

 

removeFirst() : 첫번째 index 값을 지운다

removeLast() : 마지막 index 값을 지운다

 

ArrayList LinkedList는 모두 List 인터페이스 소속이기 때문에 List로 업캐스팅하여 활용하다

 

List<String> list = new LinkedList<>();

 

업캐스팅을 더 거슬러 올라간다면 컬렉션까지도 갈 수 있다
List set Collection 인터페이스의 자식 인터페이스이다

 

Collection<String> col1 = new LinkedList<>();
Collection<String> col2 = new HashSet<>();

 

모든 컬렉션은 배열로 변환될 수 있다

 

list.add("무궁화호");
list.add("새마을호");
list.add("통일호");
String[] str = new String[list.size()];
list.toArray(str);
System.out.println(Arrays.toString(str));

 

 

// list1.addFirst();

 

업캐스팅 된 상태이기 때문에 LinkedList만의 메서드는 사용할 수 없는 상태

'JAVA' 카테고리의 다른 글

Collections  (0) 2024.05.25
Wrapper class  (0) 2024.05.24
제네릭 (Generic)  (0) 2024.05.23
HashSet  (0) 2024.05.20
자료 구조 (Data Structure)  (0) 2024.05.16

+ Recent posts