java

List(ArrayList)

코로그 주인장 2023. 1. 18. 21:36

리스트는 배열과 비슷한 자바의 자료형으로 배열보다 편리한 기능을 많이 가지고 있다.

리스트와 배열의 가장 큰 차이는 배열은 크기가 정해져 있지만 리스트는 크기가 정해져 있지 않고 동적으로 변한다는 점이다.

(동적으로 자료형의 갯수가 변하는 상황이라면 List를 사용해야 함!!!)

 

 

ArrayList: List중 가장 간단한 자료형

(List 자료형에는 ArrayList, Vector, LinkedList 등의 List 인터페이스를 구현한 자료형이 있음)

// 박찬호 선수가 총 3번의 투구를 138, 129, 142(km) 의 속도록 던졌다면 다음과 같이 코드를 작성할 수 있다.
import java.util.ArrayList;
// ArrayList를 사용하기 위해서는 import java.util.ArrayList와 같이 ArrayList를 먼저 import

public class Sample {
    public static void main(String[] args) {
        ArrayList pitches = new ArrayList();
        pitches.add("138");
        pitches.add("129");
        pitches.add("142");
    }
}


// 첫번째 위치에 "133"이라는 투구 스피드를 삽입하고 싶다면 아래와 같이 삽입할 위치를 파라미터로 넘겨주어야 한다.
pitches.add(0, "133");    // 첫번째 위치에 133 삽입.

// 2번 째 위치에 133을 삽일 할 경우에는 다음과 같이
pitches.add(1, "133");

 

 

// 2번째 투구 스피드를 출력하고 싶다면 다음과 같이
import java.util.ArrayList;

public class Sample {
    public static void main(String[] args) {
        ArrayList pitches = new ArrayList();
        pitches.add("138");
        pitches.add("129");
        pitches.add("142");
        System.out.println(pitches.get(1));
        // ArrayList의 get 메서드를 이용하면 특정 인덱스의 값을 추출할 수 있다.
    }
}

 

 

// size 메서드는 ArrayList의 갯수를 리턴
System.out.println(pitches.size());

// contains 메서드는 리스트 안에 해당 항목이 있는지를 판별하여 그 결과를 boolean으로 리턴
System.out.println(pitches.contains("142"));

// remove 메서드: remove(객체) / remove(인덱스)
// remove(객체)의 경우는 리스트에서 객체에 해당되는 항목을 삭제하고 삭제한 결과(true, false)를 리턴
System.out.println(pitches.remove("129"));  // 결과: true
// remove(인덱스)의 경우는 해당 인덱스의 항목을 삭제하고 삭제된 항목을 리턴
System.out.println(pitches.remove(0)); // 결과: 138

// 값이 존재하는 경우 해당 엘레멘트의 인덱스를 리턴, 값이 존재하지 않을 경우 -1을 리턴
int index = colors.indexOf("142");
System.out.println(index);

 

제네릭스(java j2se 5.0 이후에 도입된 개념)

 

 

// 보통 뒷 부분의 자료형은 굳이 적지 않아도 의미가 명확하기 때문에 다음의 표기법을 사용하는 것이 좋다.
ArrayList<String> pitches = new ArrayList<>();  // 선호되는 방식

// 제네릭스가 도입되기 전인 J2SE 1.4 까지는 위의 코드를 다음과 같이 사용
ArrayList pitches = new ArrayList();

두 개 코드의 차이점은 ArrayList 라는 자료형 타입 바로 옆에 <String> 과 같은 문구가 있느냐 없느냐의 차이

위에서 사용한 첫번째 코드의 <String> 이라는 제네릭스 표현식은 "ArrayList 안에 담을 수 있는 자료형은 String 타입 뿐이다" 라는 것을 의미한다. 즉, 제네릭스를 이용하면 좀 더 명확한 타입체크가 가능하며 코드 작성시에도 이점이 있다.

 

// 제네릭스 사용x
ArrayList pitches = new ArrayList();
pitches.add("138");
pitches.add("129");

String one = (String) pitches.get(0);
String two = (String) pitches.get(1);
// ArrayList 안에 추가되는 객체는 Object 자료형으로 인식
// Object 자료형은 모든 객체가 상속하고 있는 가장 기본적인 자료형

ArrayList 객체인 pitches에 값을 넣을 때는 문제가 안되지만 값을 가져올 경우에는 항상 Object 자료형에서 String 자료형으로 다음과 같이 형변환(casting)을 해야 한다.

String one = (String) pitches.get(0); // Object 자료형을 String 자료형으로 캐스팅한다.

주의할 점은 pitches 안에는 String 객체 이외의 객체도 넣을 수 있기 때문에 형 변환 과정에서 잘못된 형변환으로 인한 오류가 발생할 가능성이 있다

// 제네릭스 사용o
ArrayList<String> pitches = new ArrayList<>();
pitches.add("138");
pitches.add("129");

String one = pitches.get(0);  // 형 변환이 필요없다.
String two = pitches.get(1);  // 형 변환이 필요없다.

제네릭스로 자료형을 선언하기만 하면 그 이후로는 자료형에 대한 형변환 과정이 필요없다

(컴파일러가 pitches에는 반드시 String 자료형만 추가 되어야 함을 알기 때문)

 

제네릭스를 이용하면 형변환에 의한 불필요한 코딩과 잘못된 형변환에 의한 런타임 오류를 방지할 수 있다.

 

import java.util.ArrayList;

public class Sample {
    public static void main(String[] args) {
        ArrayList<String> pitches = new ArrayList<>();  // 제네릭스를 사용한 표현
        pitches.add("138");
        pitches.add("129");
        pitches.add("142");
        System.out.println(pitches);  // [138, 129, 142] 출력
    }
}

 

이미 문자열 배열이 존재할 경우에는 보다 편하게 ArrayList를 생성할 수 있다.

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
    public static void main(String[] args) {
        String[] data = {"138", "129", "142"};  // 이미 투구수 데이터 배열이 있다.
        ArrayList<String> pitches = new ArrayList<>(Arrays.asList(data));
        System.out.println(pitches);  // [138, 129, 142] 출력
    }
}

 

 

java.util.Arrays 클래스의 asList 메서드를 사용하면 이미 존재하는 문자열 배열로 ArrayList를 생성할 수 있다.

또는 다음과 같이 String 배열 대신 String 자료형을 여러개 전달하여 생성할 수도 있다.

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
    public static void main(String[] args) {
        ArrayList<String> pitches = new ArrayList<>(Arrays.asList("138", "129", "142"));
        System.out.println(pitches);
    }
}

 

 

String.join() (Java 8 버전부터 사용)

String.join("구분자", 리스트객체)와 같이 사용하여 리스트의 각 요소에 "구분자"를 삽입하여 하나의 문자열로 만들 수 있다.

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
    public static void main(String[] args) {
        ArrayList<String> pitches = new ArrayList<>(Arrays.asList("138", "129", "142"));
        String result = String.join(",", pitches);
        System.out.println(result);  // 138,129,142 출력
    }
}

String.join은 다음처럼 문자열 배열에도 사용할 수 있다.

public class Sample {
    public static void main(String[] args) {
        String[] pitches = new String[]{"138", "129", "142"};
        String result = String.join(",", pitches);
        System.out.println(result);  // 138,129,142 출력
    }
}

join 메서드 없이 구현한 코드

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
    public static void main(String[] args) {
        ArrayList<String> pitches = new ArrayList<>(Arrays.asList("138", "129", "142"));
        String result = "";
        for (int i = 0; i < pitches.size(); i++) {
            result += pitches.get(i);
            result += ",";  // 콤마를 추가한다.
        }
        result = result.substring(0, result.length() - 1);  // 마지막 콤마는 제거한다.
        System.out.println(result);  // 138,129,142 출력
    }
}

// substring은 문자열 중 특정 부분을 뽑아낼 경우에 사용
// substring(시작위치, 끝위치): 문자열의 시작위치에서 끝위치 전까지의 문자를 뽑아냄,

 

리스트 정렬(sort메서드 사용, java8부터 가능)

sort 메서드에는 정렬기준을 파라미터로 전달해야 한다. 정렬기준에는 다음처럼 오름차순, 내림차순이 있다.

오름차순(순방향) 정렬 - Comparator.naturalOrder()

내림차순(역방향) 정렬 - Comparator.reverseOrder()

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class Sample {
    public static void main(String[] args) {
        ArrayList<String> pitches = new ArrayList<>(Arrays.asList("138", "129", "142"));
        pitches.sort(Comparator.naturalOrder());  // 오름차순으로 정렬
        System.out.println(pitches);  // [129, 138, 142] 출력
    }
}

 

ArrayList 전체 값 확인

ArrayList의 모든 값들을 순회해서 출력하고 싶은 경우 다양한 방법을 사용가능

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ListIterator;

public class ArrayListTest {
    public static void main(String[] args) {
        ArrayList<String> colors = new ArrayList<>(Arrays.asList("Black", "White", "Green", "Red"));
        // for-each loop
        for (String color : colors) {
            System.out.print(color + "  ");
        }
        System.out.println();

        // for loop
        for (int i = 0; i < colors.size(); ++i) {
            System.out.print(colors.get(i) + "  ");
        }
        System.out.println();

        // using iterator
        Iterator<String> iterator = colors.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + "  ");
        }
        System.out.println();

        // using listIterator
        ListIterator<String> listIterator = colors.listIterator(colors.size());
        while (listIterator.hasPrevious()) {
            System.out.print(listIterator.previous() + "  ");
        }
        System.out.println();
    }
}

1. for-each 반복문으로 각각의 값을 순회해서 출력

2. get() 메소드로 각 인덱스의 값을 순차적으로 탐색

3/4. iterator나 listIterator를 통해 값들을 순회

(listIterator의 경우 생성 시 ArrayList의 크기를 입력해주고 역방향으로 출력 가능) 

<참고>

https://wikidocs.net/207

 

03-07 리스트 (List)

[TOC] 리스트는 배열과 비슷한 자바의 자료형으로 배열보다 편리한 기능을 많이 가지고 있다. 리스트와 배열의 가장 큰 차이는 배열은 크기가 정해져 있지만 리스트는 크기가 …

wikidocs.net

https://psychoria.tistory.com/765

 

[Java] 자바 ArrayList 사용 방법

ArrayList는 자바에서 기본적으로 많이 사용되는 클래스입니다. ArrayList는 자바의 List 인터페이스를 상속받은 여러 클래스 중 하나입니다. 일반 배열과 동일하게 연속된 메모리 공간을 사용하며 인

psychoria.tistory.com