일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 26 | 27 | 28 |
29 | 30 |
- leetcode
- coding
- Algorithm
- 알고리즘
- binary tree
- two pointers
- programmers
- ArrayList
- Array
- 재귀함수
- 부분배열
- Java
- greedy
- priority queue
- DP
- 우선순위 큐
- 리트코드
- 브루트포스
- PCCP
- dfs
- 깊이우선탐색
- hashset
- string
- recursion
- HashMap
- Today
- Total
지식창고
[Java] ArrayList Method, 사용법 본문
ArrayList ?
ArrayList는 자바에서 매우 많이 사용되는 클래스이다.
List 인터페이스를 상속받은 클래스 중 하나로, List의 성격을 가지고 있다.
Array 와 이름이 비슷하여 헷갈릴 수 있지만 가장 큰 차이점은 크기가 가변적이라는 것이다.
Hash와 마찬가지로 내부적으로 Capacity가 할당되어 있으며 그 이상을 저장하려고 한다면 더 큰 공간의 메모리를 새로 할당해주는 방식이다.
ArrayList의 특징
크기가 가변적이다.
인덱스는 0부터 시작한다.
Hash와 마찬가지로 내부적인 메모리 할당(Capacity)이 따로 있다.
ArrayList Declaration
import java.util.ArrayList;
ArrayList<Integer> list1 = new ArrayList<Integer>(); // 타입 지정
ArrayList<Integer> list2 = new ArrayList<>(); // 타입 생략
ArrayList<Integer> list3 = new ArrayList<>(10); // 초기 메모리 용량(Capacity) 설정
ArrayList<Integer> list4 = new ArrayList<>(list1); // list1과 같은 값으로 초기화
ArrayList<Integer> list5 = new ArrayList<>(Arrays.asList(10, 20, 30)); // 배열을 List로 초기화
ArrayList Method
- add((index), value) - 순서대로 List에 value를 추가, index를 쓰면 그 자리에 추가
- get(index) - index의 value 반환
- set(index, value) - index의 old_value를 value로 변경
- indexOf(value) - value를 가지는 첫 번째 index 반환
- lastIndexOf(value) - value를 가지는 마지막 index 반환
- remove(index or value) - 해당 index or 해당 value중 첫 번째 원소 삭제
- clear() - 값 모두 삭제
- size() - 원소 개수 반환
- contains(value) - value가 List에 있으면 true
- containsAll(value1, value2, ...) - 매개변수에 있는 value들이 List에 모두 있다면 true
- isEmpty() - List가 비어 있다면 true
- toArray() - Array타입으로 반환
- addAll(collections) - collections와 합치기
- retainAll(collections) - collections와 겹치는 값 빼고 모두 삭제 (교집합 구하기)
- removeAll(collections) - collections와 겹치는 값 모두 삭제
1. value 추가
add((index), value)
순서대로 List에 value를 추가, index를 쓰면 그 자리에 추가
list.add(10);
value(10) 가 ArrayList에 추가된다.
2. value 반환
get(index)
index의 value 반환
int zero = list.get(0);
0 번째 value를 반환해준다.
3. value 변경
set(index, value)
index의 old_value를 value로 변경
list.set(0,20);
0번째 value를 20으로 변경해준다.
4. index 반환
indexOf(value)
value를 가지는 첫 번째 index 반환
lastIndexOf(value)
value를 가지는 마지막 index 반환
list.indexOf(20);
list.lastIndexOf(10);
5. 요소 삭제
remove(index or value)
해당 index or 해당 value 중 첫 번째 원소 삭제
clear()
값 모두 삭제
list.remove(10);
list.clear();
6. 그 외 Method
size()
ArrayList 에 들어있는 데이터의 개수를 반환
int size = list.size();
contains(value)
ArrayList에 해당 value가 들어있는지 아닌지 확인해준다.
ArrayList가 가지는 data-type을 value로 넣어야 한다.
들어있으면 - true
안 들어있으면 - false
list.add(10);
list.add(100);
if(list.contains(10)){
System.out.println("10이 들어있음");
}
else {
System.out.println("10이 없음");
}
containsAll(value1, value2, ...)
ArrayList에 해당 value들이 들어있는지 아닌지 확인해준다.
ArrayList가 가지는 data-type을 value로 넣어야 한다.
전부 들어있으면 - true
하나라도 안 들어있으면 - false
list.add(10);
list.add(100);
if(list.containsAll(10, 100)){
System.out.println("10과 100이 들어있음");
}
else {
System.out.println("10과 100이 들어있지않음");
}
isEmpty()
ArrayList가 비어있는지 확인해준다.
비어있으면 - true
데이터가 있으면 - false
if(list.isEmpty()){
System.out.println("비어있음");
}
else {
System.out.println("비어있지 않음");
list.clear(); // 비우기
}
toArray()
ArrayList에 들어있는 데이터들을 Array로 반환해준다.
// ex.1
Object[] arr = list.toArray();
// ex.2
Integer[] arr = new Integer[5];
list.toArray(arr);
// ex.3
// arr, arr2 두 배열이 같은 값을 가지게 됨
Integer[] arr = new Integer[5];
Integer[] arr2 = list.toArray(arr);
addAll(collections)
collections와 합치기
retainAll(collections)
collections와 겹치는 값 빼고 모두 삭제 (교집합 구하기)
removeAll(collections)
collections와 겹치는 값 모두 삭제
'Language > Java' 카테고리의 다른 글
[Java] Integer 클래스와 Integer 메소드 모음 (0) | 2023.01.30 |
---|---|
[Java] Arrays 클래스와 Arrays 메소드 모음 (0) | 2023.01.24 |
[Java] Priority Queue Method, 사용법 (0) | 2023.01.04 |
[Java] Hash Map Method, 사용법 (0) | 2023.01.04 |