작성자 | 이재영 |
일 시 | 2024. 10. 08 (화) 18:00 ~ 21:00 |
장 소 | 복지관 b128-1호 |
참가자 명단 | 임혜진, 이재영, 성창민, 김명원, 장원준 |
사 진 | ![]() |
목차
- Array
- ArrayList
- LinkedList
이번 주차에는 Java에서 많이 사용하는 자료구조 중 리스트 관련된 자료구조를 공부해볼 것이다.
1. Array
Array는 배열을 뜻하는 자료구조로 고정된 크기의 연속적인 메모리 공간에 데이터를 저장하는 구조이다. 배열의 크기는 선언 시에 정해지며 이후 변경할 수 없다는 특징이 있다. 인덱스를 통해 빠르게 데이터에 접근할 수 있어 시간 복잡도가 O(1)인 장점이 있지만, 크기가 고정되어 있어 배열의 크기를 변경할 수 없고, 삽입 및 삭제 시 비효율적일 수 있다는 단점이 있다.
Array를 선언할 때 양식은 자료형[] 배열명 = new 자료형[배열크기]; 이다. 예제 코드를 보자.
// 크기가 5인 정수형 배열 선언
int[] array = new int[5];
// 배열에 값 할당
array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;
array[4] = 50;
// 배열 요소 출력
for (int i = 0; i < array.length; i++) {
System.out.println("Element at index " + i + ": " + array[i]);
}
Array를 선언하고, 인덱스를 통해 값에 접근이 가능하다.
2. ArrayList
ArrayList는 Array와 비슷하지만 크기가 가변적인 배열로, 데이터가 추가되면 자동으로 크기가 증가하는 리스트이다. 크기가 동적으로 변함므로 Array보다 유연하며, 똑같이 인덱스를 통한 빠른 접근이 가능하다는 장점이 있지만, 중간 삽입 및 삭제는 배열의 데이터 이동이 필요해 성능이 떨어질 수 있다는 단점이 있다.
ArrayList를 선언할 때 양식은 java.util.ArrayList 임포트 후, ArrayList<자료형> Array명 = new ArrayList<>(); 이다. 예제코드를 보자.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// ArrayList 선언
ArrayList<String> arrayList = new ArrayList<>();
// 값 추가
arrayList.add("Apple");
arrayList.add("Banana");
arrayList.add("Cherry");
// ArrayList 출력
for (int i = 0; i < arrayList.size(); i++) {
System.out.println("Element at index " + i + ": " + arrayList.get(i));
}
// 값 제거
arrayList.remove("Banana");
// 제거 후 ArrayList 출력
System.out.println("\nAfter removal:");
for (String fruit : arrayList) {
System.out.println(fruit);
}
}
}
Array와 다르게 클래스를 사용한 객체이기 때문에 관련 메소드가 있는데, 주로 사용하는 메소드는 다음과 같다.
메소드명 | 설명 |
ArrayList.add(Object); | Object를 ArrayList에 넣는 메소드. Object는 앞에서부터 차례대로 들어간다. |
ArrayList.get(index); | index에 위치한 Object를 가져오는 메소드. index 값을 넘으면 IndexOutOfBoundsException을 발생시킨다. |
ArrayList.remove(Object); | ArrayList에 있는 Object를 삭제시킨다. |
ArrayList.size(); | ArrayList에 있는 Object의 갯수를 반환한다. |
ArrayList.clear(); | ArrayList에 있는 모든 Object를 삭제시킨다. |
3. LinkedList
LinkedList는 노드로 이루어진 리스트로, 각 노드는 데이터와 다음 노드를 가리키는 참조를 포함한다. 삽입과 삭제가 특정 노드에서 O(n)으로 빠르다는 장점이 있지만, 인덱스를 통한 접근은 인덱스 만큼의 노드를 거쳐서 가기 때문에 O(n)으로 위에 Array와 ArrayList보다 느리다는 단점이 있다.
위 사진과 같은 구조를 가진 LinkedList는 메모리 낭비가 없고 용량이 고정되지 않는다는 특징이 있다.
LinkedList를 선언하는 양식은 java.util.LinkedList 임포트 후, LinkedList<자료형> 리스트명 = new LinkedList<>(); 이다. 예제코드를 보자.
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
// LinkedList 선언
LinkedList<Integer> linkedList = new LinkedList<>();
// 값 추가
linkedList.add(10);
linkedList.add(20);
linkedList.add(30);
// LinkedList 출력
for (int i = 0; i < linkedList.size(); i++) {
System.out.println("Element at index " + i + ": " + linkedList.get(i));
}
// 첫 번째 요소 제거
linkedList.removeFirst();
// 제거 후 LinkedList 출력
System.out.println("\nAfter removing the first element:");
for (int value : linkedList) {
System.out.println(value);
}
}
}
LinkedList에서도 자주 사용하는 메소드를 알아보자.
메소드명 | 설명 |
LinkedList.add(Object); | Object를 LinkedList에 넣는 메소드. Object는 앞에서부터 차례대로 들어간다. |
LinkedList.get(index); | index에 위치한 Object를 가져오는 메소드. index 값을 넘으면 IndexOutOfBoundsException을 발생시킨다. |
LinkedList.removeFirst(); | 맨 앞에 있는 LinkedList에 있는 Object를 삭제시킨다. |
LinkedList.removeLast(); | 맨 뒤에 있는 LinkedList에 있는 Object를 삭제시킨다. |
LinkedList.remove(index); | LinkedList에서 index에 위치한 Object를 삭제시킨다. |
ArrayList.size(); | LinkedList에 있는 Object의 갯수를 반환한다. |
ArrayList.clear(); | LinkedList에 있는 모든 Object를 삭제시킨다. |
오늘은 이렇게 배열에 대한 자료구조를 공부해 보았다. 상당히 다양한 자료구조가 패키지로 존재하는 자바에서 이런 하나하나가 알고리즘을 푸는 데 도움이 될 것이라고 생각이 들었다.