package com.use.java;
import java.util.*;
public class test {
//ArrayList란?
//ArrayList는 List 인터페이스를 상속받은 클래스로 크기가 가변적으로 변하는 선형리스트
public static void main(String[] args) {
//선언
ArrayList list2 = new ArrayList();//타입 미설정 Object로 선언된다.
ArrayList<Integer> num = new ArrayList<Integer>();//타입설정 int타입만 사용가능
ArrayList<Integer> num2 = new ArrayList<>();//new에서 타입 파라미터 생략가능
ArrayList<Integer> num3 = new ArrayList<Integer>(10);//초기 용량(capacity)지정
ArrayList<Integer> list3 = new ArrayList<Integer>(Arrays.asList(1,2,3));//생성시 값추가
//값 추가 => add(index, value) 메서드 사용 => index를 생략하면 맨 뒤에 데이터 추가
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(3); //값 추가
list.add(null); //null값도 add가능
list.add(1,10); //index 1에 10 삽입
//값 출력 => get(index) 메서드 사
System.out.println(list.get(2));//0번째 값 출력 : null
for(Integer i : list) { //for문을 통한 전체출력
System.out.println(i);
}
//3
//10
//null
//값 검색 => contains(value), indexOf(value) 메서드 사용
//contains(value)는 값이 있다면 true가 리턴되고 값이 없다면 false가 리턴
//indexOf(value)는 해당 값을 가진 인덱스가 있으면 리턴하고, 만약 값이 없다면 -1을 리턴
System.out.println(list.contains(null)); //list에 1이 있는지 검색 : true
System.out.println(list.indexOf(10)); //1이 있는 index반환 없으면 1
//값 삭제 => remove(index) 메서드 사용
list.remove(1); //index 1 제거
list.clear(); //모든 값 제거
//크기 구하기
System.out.println(list.size()); //list 크기 : 0
}
}