
Contents
5. 실습1. ArrayList란?
배열의 크기를 동적으로 변경하면서 사용할 수 있다. (컬렉션이라고 함)
배열이랑 비슷하지만, 배열과는 달리 자유롭게 공간을 추가·삭제할 수 있다.
연속되어 있는 공간이 아니다. > LinkedList의 특징도 있다.
LinkedList (영.원.히.쓰.지.마) 이런게 있다는 것만 알고 있기

- 1이 2의 주소(4번지)를 가지고 있다. 다음 번지에 해당하는 주소 링크를 가지고 있기 때문에 그 링크를 바탕으로 타고 나가며 연속된 공간이 아니지만! 저장된 값을 찾을 수 있다.
- 이 링크로 list(예시문의 변수)를 만드는 것. 이런 자료구조를 LinkedList라고 부른다.
- 만약 4라는 값을 찾고 싶어도 다이렉트로 못찾는다. (하나하나 다음 번지를 방문해가면서 찾아야함) 운이 나쁘면 엄청 늦게 찾는다.
- 즉, 풀스캔 (전체를 순회) 할 때 사용한다.
ex) 전체 데이터를 +1이나, *1.1씩 해야할 때, (ex.모든 직원들의 연봉을 10% 인상하고 싶다!)
그런 식으로 가공할 때 이런 구조를 사용한다. 특정 값 하나를 찾고 싶다! 같은 경우에는 사용하지 않음!!
2. ArrayList 사용법 (생성)
ArrayList 를 선언해야 한다.
ArrayList<String> —> 문자열 추가 하고 싶을때 String
ArrayList<String> list = new ArrayList<>();
ArrayList<여기에는 쓰려는 자료형을 넣음> list
= new ArrayList<>()(남는 공간에 넣으니까 공간을 미리 확보하지 않는다.);
2-1. ArrayList 추가 - add()
2-2. ArrayList 호출 - get()
get 메소드는 배열의 index 위치에 있는 요소 값을 반환한다.


Arraylist를 불러올 때는 힙에 띄운 ArrayList변수명.get( ) 으로 불러온다.
(ex. list.get() )
배열은 list[];
2-3. ArrayList 삭제 - remove()
remove 메소드는 배열의 index 위치에 있는 요소 값을 제거하고 그 값을 반환한다.



ArrayList변수명.remove(삭제하려는 값 인덱스)로 삭제한다. (ex. list.remove(3);
해당 코드에서, ArrayList를 삭제하기 전에는 '지영'이라고 출력되는데,
삭제된 이후에는 3번 인덱스는 찾을 수 없다고 오류가 뜬다.
2-4. ArrayList 위치 바꾸기 - set()

2-5. ArrayList 포함 여부 검사 - contains()
if (list.contains("APPLE"))
System.out.println("APPLE이 리스트에서 발견 됨");
boolean 타입
contains()는 String 에서도 다룬 적이 있다.
2-6. ArrayList의 인덱스 반환 - indexOf()
package ex08;
import java.util.ArrayList;
public class Test08 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Mango");
list.add("Pear");
list.add("Grape");
int index = list.indexOf("Mango");
System.out.println("Mango의 위치 : " + index);
if (list.contains("Mango")) {
System.out.println("Mango의 위치");
}
}
}
indexOf() 메소드를 이용하여 특정 문자열을 찾을 수 있다.
: ArrayList 안에 저장된 데이터를 찾아서 인덱스를 반환하는 메소드
2-7. ArrayList에 객체를 저장하기
package ex08;
import java.util.ArrayList;
class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
}
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<Point> list = new ArrayList<>();
list.add(new Point(0, 0));
list.add(new Point(4, 0));
list.add(new Point(3, 5));
list.add(new Point(-1, 3));
list.add(new Point(13, 2));
System.out.println(list);
}
}
<Point>는 ArrayList가 Point 객체들을 저장하는 리스트로 설정되었음을 나타냄
ArrayList<Point>로 설정하면, list에는 Point 객체들만 추가(저장)할 수 있게 된다.
따라서, Point 객체를 생성(new)하여 add 메서드를 통해 list에 추가할 수 있다.
Point 객체 = int 타입의 멤버 변수인 x와 y
3. Array, ArrayList, LinkedList의 차이


[ 크기를 알아내는 법 ]
배열 : array변수명.length
ArrayList : arrayList변수명.size()
문자열 : System.out.println(string변수명.length());
컬렉션에서 왔나요?
5. 실습
package ex03;
import java.util.ArrayList;
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
//<>가 뭐져? -> 제너릭
//제너릭 -> ArrayList가 내부적으로 element들이 String 문자라는 것이다.
//실제로는 제너릭 사용해야 함.
list.add("철수");
list.add("영희");
list.add("순신");
list.add("지영");
System.out.println(list.get(3));
list.remove(3); // 3번째 요소 삭제
System.out.println(list.get(3));
}
}
Share article