ArrayList와 배열의 차이점
배열은 생성자 초기화를 한 후 새로운 인덱스 값을 추가,제거할 수 없지만 ArrayList의 경우에는 생성자 초기화 후 
추가적으로 인덱스 값을 제거하거나 추가 가능 
즉 ArrayList가 더욱 유동적으로 활용된다

Integer 타입

📌기존의 배열 방식 [1] : 기초적인 방식 

int[] score = new int[5] //생성자 선언 ,  5개의 integer를 담고있는 배열
//score에 하나하나 값을 대입 
    score[0] = 55;
    score[1] = 72;
    score[2] = 30;
    score[3] = 12;

📌기존의 배열 방식 [2] : 축약형

int[] score = new int[] {10,20,30,40,50};  //한번에 생성자 생성과 초기화 가능
int[] score= {10,20,30,40,50};  // 위에 코드보다 더 축약된 버전

📌 ArrayList 방식

ArraayList<Integer> score = new ArrayList<>();
    score.add(0,10);
    score.add(1,20);
    score.add(2,30);
    score.add(3,40);

📎 인덱스 값 추가

score.add(0,100); //0번째 인덱스값에 100을 추가하겠다

	System.out.println(score); // [100,10,20,30,40]

📎 인덱스 값 제거

score.remove(0); //0인덱스값을 제거  (위에서 추가한 0인덱스의 100값이 제거)

	System.out.println(score); // [10,20,30,40]

📎 인덱스 값 하나만 추출

System.out.println(score.get(2)); // score의 2번째 인덱스값 추출 => [30]

📎 인덱스 길이 추출

int count = score.length; //score라는 생성자의 길이를 세는 함수

 

String 타입

📌 기존의 배열 방식 [1] : 기초적인 방식

String[] names = {"홍길동","이순신","유관순"};  // 1)일반 기초배열

📌 ArrayList 방식

 ArrayList<String> colors = new ArrayList<>(); //ArrayList 스트링배열
     colors.add("blue");
     colors.add("yello");
     colors.add("green");
     colors.add("puple");

📎 인덱스 값 변경 (String타입만 가능)

 colors.set(4,"black"); //set() method로 기존에 추가된 값 변경가능(스트링배열만 가능)

 

복사했습니다!