: 여러 객체(데이터)를 모아놓은 것을 의미, +
: 표준화, 정형화된 체계적인 프로그래밍 방식, 기능만을 제공
: 컬렉션(다수의 객체)를 다루기 위한 표준화된 프로그래밍 방식
컬렉션을 쉽고 편리하게 다룰 수 있는 다양한 클래스 제공
많은 수의 데이터를 다루는데 유용하다.
배열과 컬렉션의 차이점
- 동일한 타입만을 저장이 가능하다.
- 방의 크기가 고정: 방의 크기가 수정이 불가능하다. (값을 선언한 뒤에는 값을 늘리거나 수정이 불가능.)
List : 인터페이스 , 객체화 불가능 . 타입으로만 지정이 가능하다.
ArrayList<>, Vector<>, LinkedList<>
=> 구현 클래스로만이 객체화가 가능
컬렉션 프레임워크를 이루는 주요클래스 및 인터페이스와 구현클래스
: List인터페이스를 구현한 컬렉션 클래스 . 클래스이기 때문에 여러가지 메소드를 포함하고 있다.
중복을 허용하고.데이터를 순차적으로 저장한다.
사용시 import, ArrayList해야함
List<String> aList = new ArrayList<>();
//0.객체생성
List<String> aList = new ArrayList<>();
//1. List 에 값 넣기
aList.add("가");
aList.add("나");
//방의 크기를 알려줌
System.out.println(aList.size());
//방의 값을 출력하기
System.out.println(aList);
System.out.println(aList.toString());
//방의 값을 제거할때
aList.remove("가")

alist1.add(3);alist1.add(1,6);List<Integer> aList2 = new ArrayList<>();
aList2.add(2);
aList2.addAll(aList1);
=> aList2.addAll(aList1); => aList2 객체에 addall로 aList1객체를 가져와 넣겠다라는 뜻
List<Integer> aList3 = new ArrayList<>();
aList3.add(4);
aList3.add(5);
System.out.println(aList3);
aList3.addAll(1,aList);
System.out.println(aList3);
=> aList3객체에 aList객체를 넣을것,
aList3.addAll(1,aList);
=>1번 방 뒤에 aList객체를 넣겠다.
aList.set(1, 5);
System.out.println(aList);
특정 방 번호의 값을 삭제
aList.remove(1)
=> aList의 1번 방의 값을 삭제
방에 들어간 값으로 삭제
aList.remove(new Interger(2));
=> aList의 2 값을 삭제
모든 값을 삭제
aList3.clear();
isEmpty() : 값이 없으면 : true, 값이 존재하면 : false
System.out.println(aList3.isEmpty());
=> 배열안에 값이 있는지 확인
방의 index길이 확인
System.out.println(aList.size());
get(int index) : 방번호의 값을 가지고 올때
System.out.println("0 번째 : " + aList3.get(0));
<예제>
Student class의 값을 ArrayList를 이용해 넣고 출력하기
class Student {
String name;
int stuID;
Student(String name, int stuID){
this.name = name;
this.StuID = stuID;
}
}
public class {
public static void main(String[] args) {
ArrayList<Student> a1 = new ArrayList();
Student s1 = new Student ("홍길동",1111);
Student s2 = new Student("이순신",2222);
Student s3 = new Student("강감찬",3333);
a1.add(s1);
a1.add(s2);
a1.add(s3);
//내용출력
for(int i = 0; i < a1.size(); i++) {
Student ss1 = a1.get(i);
System.out.println(ss1);
}
// Enhanced For
for(Student K : a1) {
System.out.println(k);
}