Java

[고급자바] Collection(List)

elog 2023. 3. 1. 12:00

1. ArrayList

A. 객체 생성

System.out.println("<객체 생성>");
ArrayList list1 = new ArrayList();

System.out.println("처음 크기 : " + list1.size());

B. 데이터 추가하기

System.out.println("<데이터 추가하기>");
list1.add("aaa");
list1.add("bbb");
list1.add(123);
list1.add('k');
list1.add(true);
list1.add(123.45);

System.out.println("list1 ==> " + list1);
System.out.println("size ==> " + list1.size());

C. 데이터 꺼내오기

System.out.println("<데이터 꺼내오기>");
System.out.println("1번째 자료 : " + list1.get(1));

D. 데이터 삭제하기

System.out.println("<데이터 끼워넣기>");
list1.add(3, "zzz");
System.out.println("list1 => " + list1);

E. 데이터 변경하기

System.out.println("<데이터 변경하기>");
String sTemp = (String)list1.set(3, "yyy");

System.out.println("list1 => " + list1);
System.out.println("sTemp => " + sTemp);

F. 데이터 삭제하기

System.out.println("<데이터 삭제하기>");
list1.remove(3);
System.out.println("3번째 자료 삭제 후 list1 => " + list1);
		
list1.remove("bbb");
System.out.println("bbb 자료 삭제 후 list1 => " + list1);


3-1. 제네릭 사용하기

A. 객체 생성

System.out.println("<객체 생성>");
ArrayList<String> list2 = new ArrayList<>();
System.out.println("처음 크기 : " + list2.size());

B. 데이터 추가하기

System.out.println("<데이터 추가하기>");
list2.add("AAAA");
list2.add("BBBB");
list2.add("CCCC");
list2.add("DDDD");
list2.add("EEEE");
		
System.out.println("<리스트의 모든 데이터 출력하기1>");
for(int i = 0; i < list2.size(); i++) {
	System.out.println(i + " ==> " + list2.get(i));
}
		
System.out.println("<리스트의 모든 데이터 출력하기2>");
for (String str : list2) {
	System.out.println(str);
}

C. 데이터 존재 여부 확인하기

contains(비교객체)

- '비교객체'가 있으면 true, 없으면 false 반환

 

indexOf(비교객체)

- 리스트에 '비교객체'가 있으면 '비교객체'가 저장된 index값 반환/없으면 -1반환

- 검색방향 : 앞 >> 뒤

 

lastIndexOf(비교객체)

- 리스트에 '비교객체'가 있으면 '비교객체'가 저장된 index값 반환/없으면 -1반환

- 검색방향 : 뒤 >> 앞

System.out.println("<데이터 존재 여부 확인하기>");
System.out.println("DDDD값 존재 여부 : " + list2.contains("DDDD"));
System.out.println("ZZZZ값 존재 여부 : " + list2.contains("ZZZZ"));

D. 데이터 위치값 검색하기

System.out.println("<데이터 추가하기>");
list2.add("AAAA");
list2.add("BBBB");
list2.add("CCCC");
list2.add("DDDD");
list2.add("EEEE");
System.out.println("list2 => " + list2);
System.out.println();

System.out.println("<데이터 위치값 검색하기>");
System.out.println("DDDD의 위치값 : " + list2.indexOf("DDDD"));
System.out.println("ZZZZ의 위치값 : " + list2.indexOf("ZZZZ"));
System.out.println("DDDD의 위치값 : " + list2.lastIndexOf("DDDD"));

E. 배열로 변환하기

toArray()

- 리스트 안의 데이터를 배열로 변환해서 반환한다.

- 기본적으로 Object형 배열로 변환한다.

 

toArray(new 제네릭타입명[0])

- 제네릭 타입의 배열로 변환해서 반환한다.

System.out.println("<배열로 변환하기>");
Object[] strArr = list2.toArray();

System.out.println("List의 개수 : " + list2.size());
System.out.println("배열의 개수 : " + strArr.length);

F. 모든 데이터 출력하기

System.out.println("<모든 데이터 출력하기1>");
for(int i = 0; i < strArr.length; i++) {
	System.out.println(i + "번째 자료 : " + strArr[i]);
}
System.out.println("---------------------------------");
		
System.out.println("<모든 데이터 출력하기2>");
// 제네릭 타입의 배열로 변환해서 가져오기
String[] strArr2 = list2.toArray(new String[0]);
for (String s : strArr2) {
	System.out.println(s);
}

G. 전체 코드

public class ArrayListTest01 {
	public static void main(String[] args) {
		// ArrayList의 기본적인 사용법은 Vector와 같다.
		System.out.println("<객체 생성>");
		ArrayList list1 = new ArrayList();
		System.out.println("처음 크기 : " + list1.size());
		System.out.println();
		
		// add()메서드를 이용해서 데이터를 추가한다.
		System.out.println("<데이터 추가하기>");
		list1.add("aaa");
		list1.add("bbb");
		list1.add(123);
		list1.add('k');
		list1.add(true);
		list1.add(123.45);
		
		System.out.println("list1 ==> " + list1);
		System.out.println("size ==> " + list1.size());
		System.out.println();
		
		// get()메서드를 이용해서 데이터를 꺼내온다.
		System.out.println("<데이터 꺼내오기>");
		System.out.println("1번째 자료 : " + list1.get(1));
		System.out.println();
		
		// 데이터 끼워넣기도 같다.
		System.out.println("<데이터 끼워넣기>");
		list1.add(3, "zzz");
		System.out.println("list1 => " + list1);
		System.out.println();
		
		// 데이터 변경하기
		System.out.println("<데이터 변경하기>");
		String sTemp = (String)list1.set(3, "yyy");
		System.out.println("list1 => " + list1);
		System.out.println("sTemp => " + sTemp);
		System.out.println();
		
		// 삭제도 같다.
		System.out.println("<데이터 삭제하기>");
		list1.remove(3);
		System.out.println("3번째 자료 삭제 후 list1 => " + list1);
		
		list1.remove("bbb");
		System.out.println("bbb 자료 삭제 후 list1 => " + list1);
		System.out.println();
		
//		-----------------------------------------------------------------------
		// 제네릭을 사용할 수 있다.
		System.out.println("------------------------------------");
		System.out.println("<제네릭 사용하기>");
		System.out.println("<객체 생성>");
		ArrayList<String> list2 = new ArrayList<>();
		System.out.println("처음 크기 : " + list2.size());
		System.out.println();
		
		System.out.println("<데이터 추가하기>");
		list2.add("AAAA");
		list2.add("BBBB");
		list2.add("CCCC");
		list2.add("DDDD");
		list2.add("EEEE");
		
		System.out.println("<리스트의 모든 데이터 출력하기1>");
		for(int i = 0; i < list2.size(); i++) {
			System.out.println(i + " ==> " + list2.get(i));
		}
		System.out.println();
		
		System.out.println("<리스트의 모든 데이터 출력하기2>");
		for (String str : list2) {
			System.out.println(str);
		}
		System.out.println("---------------------------------");
		
		// contains(비교객체) ==> 리스트에 저장된 데이터 중에서 '비교객체'가 있으면 true, 없으면 false 반환
		System.out.println("<데이터 존재 여부 확인하기>");
		System.out.println("DDDD값 존재 여부 : " + list2.contains("DDDD"));
		System.out.println("ZZZZ값 존재 여부 : " + list2.contains("ZZZZ"));
		System.out.println();
		
		// indexOf(비교객체)
		// lastIndexOf(비교객체) ==> 리스트에 '비교객체'가 있으면 '비교객체'가 저장된 index값을 반환하고
		//				없으면 -1을 반환한다.
		// - indexOf()는 검색 방향이 앞에서부터 뒤쪽으로 검색하고,
		// - lastIndexOf()메서드는 뒤에서 앞쪽으로 검색한다.
		
		System.out.println("<데이터 추가하기>");
		list2.add("AAAA");
		list2.add("BBBB");
		list2.add("CCCC");
		list2.add("DDDD");
		list2.add("EEEE");
		System.out.println("list2 => " + list2);
		System.out.println();
		
		System.out.println("<데이터 위치값 검색하기>");
		System.out.println("DDDD의 위치값 : " + list2.indexOf("DDDD"));
		System.out.println("ZZZZ의 위치값 : " + list2.indexOf("ZZZZ"));
		System.out.println("DDDD의 위치값 : " + list2.lastIndexOf("DDDD"));
		System.out.println("---------------------------------");
		
		// -toArray() ==> 리스트 안의 데이터를 배열로 변환해서 반환한다.
		//            ==> 기본적으로 Object형 배열로 변환한다.
		
		// - toArray(new 제네릭타입명[0]) ==> 제네릭 타입의 배열로 변환해서 반환한다.
		System.out.println("<배열로 변환하기>");
		Object[] strArr = list2.toArray();
//		String[] strArr = (String[])list2.toArray();	// 이 방법은 사용할 수 없다.
		
		System.out.println("List의 개수 : " + list2.size());
		System.out.println("배열의 개수 : " + strArr.length);
		System.out.println();
		
		System.out.println("<모든 데이터 출력하기1>");
		for(int i = 0; i < strArr.length; i++) {
			System.out.println(i + "번째 자료 : " + strArr[i]);
		}
		System.out.println("---------------------------------");
		
		System.out.println("<모든 데이터 출력하기2>");
		// 제네릭 타입의 배열로 변환해서 가져오기
		String[] strArr2 = list2.toArray(new String[0]);
		for (String s : strArr2) {
			System.out.println(s);
		}
		
	}
}