Programming/JAVA
List / ArrayList 클래스
dev?
2022. 5. 13. 19:29
반응형
List / ArrayList 클래스
- 컬렉션 프레임워크 (Collection Framework)의 한 종류이다.
- 데이터 저장 순서가 존재한다.
- 데이터를 중복으로 저장 가능하다.
- 배열과 비슷한 형태로 저장된다.
- 배열은 저장되는 데이터의 한계를 지정해야 하지만, ArryList는 데이터 저장의 한계가 없다.
[ 사용 예제 ]
1. 선언 <key, value> : 제네릭
List<Integer> list = new ArrayList<Integer>();
2. 추가 list.add(값)
index 0 |
index 1 |
index 2 |
1 | 2 | 3 |
// 데이터 추가 - 맨 뒤에
list.add(1);
list.add(2);
list.add(3);
→ 중간에 추가 list.add(index, 값)
index 0 |
index 1 |
index 2 |
index 3 |
1 | 2 | 20 | 3 |
// 데이터 추가 - 중간에 추가
// add(index, 값)
list.add(2, 20);
3. 수정 list.set(index, 값)
index 0 |
index 1 |
index 2 |
index 3 |
1 | 2 | 500 | 3 |
// 데이터 수정
list.set(2, 500);
4. 확인 list.get(key) / list.size()
// 데이터 값 확인
list.get(2);
// 크기 확인
list.size()
// 전체 데이터 확인
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
5. 데이터 삭제 list.remove(index) / list.clear()
index 0 |
index 1 |
index 2 |
index 3 |
1 | 2 | 3 |
// 데이터 1개 삭제
list.remove(2);
// 전체 삭제
list.clear();
반응형