1> 역할 / 용도 : 서로 다른 타입의 데이터를 저장 동적 길이 배열
2> 변수 / 생성자 / 메소드 :
ArrayList list = new ArrayList();
//최초 10개의 데이터 저장 메모리 확보, 모자라면 자동으로 10개씩 증가
//타입상관없음
ArrayList list = new ArrayList(5);
//최초 5개의 데이터 저장 메모리 확보, 모자라면 자동으로 5개씩 증가
ArrayList list = new ArrayList(5, 2);
//최초 5개의 데이터 저장 메모리 확보, 모자라면 자동으로 2개씩 증가
ArrayList list = new ArrayList(); //타입 미설정 Object로 선언
ArrayList<Student> members = new ArrayList<Student>(); //타입설정 Student객체만 사용가능
ArrayList<Integer> num = new ArrayList<Integer>(); //타입설정 int타입만 사용가능
ArrayList<Integer> num2 = new ArrayList<>(); //new에서 타입 파라미터 생략가능
ArrayList<Integer> num3 = new ArrayList<Integer>(10); //초기 용량(capacity)지정
ArrayList<Integer> list2 = new ArrayList<Integer>(Arrays.asList(1,2,3)); //생성시 값추가
ArrayList<Object> arrayList = new ArrayList<>();
리스트 크기 size()
arrayList.size(); //엘리먼트 개수 반환
데이터 추가 add(x), add(x, y)
arrayList.add(Object value); //ArrayList의 마지막 위치에 value 추가
arrayList.add(int index, Object value); //ArrayList 해당 index에 value 추가
데이터 삭제 remove(x), clear()
arrayList.remove(int index); //index에 해당하는 데이터 제거
arrayList.claer(); //모든 데이터 제거
데이터 조회 get(x)
arrayList.get(int index); //해당 index에 있는 데이터 가져옴
데이터 수정 set(x, y)
arryList.set(int index, Object value); //해당 인덱스의 데이터 value로 변경
인덱스 조회 indexOf(x)
arrayList.indexOf(Object value); //해당 값 없을 시 -1 반환
비어있는 리스트인지 확인 isEmpty()
arrayList.isEmpty(); //boolean값 반환
ArrayList<Integer> numbers = new ArrayList<>();
//Iterator 사용
Iterator it = numbers.iterator();
while(it.hasNext()){
int value = (int) it.next(); //Object 타입이므로 int로 형변환
System.out.println(value);
}
}
//foreach 사용
for(int value : numbers) {
System.out.println(value);