Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter01 : ArrayList 본문

JAVA/chapter23_collection_framework

chapter01 : ArrayList

GAWON 2023. 5. 31. 09:11
package org.joonzis.ex;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Ex01_ArrayList {
	public static void main(String[] args) {
		
		List<Integer> list = new ArrayList<Integer>();
		//ArrayList<Integer> list = new ArrayList<Integer>();
		
		// 1. 저장 : add()
		list.add(new Integer(1));
		list.add(new Integer(2));
		list.add(5);
		list.add(4);
		list.add(3);
		list.add(5,2);	// 인덱스를 지정하여 값을 넣을 수 있다.  add(인덱스 번호, 값)
		
		// 2. 저장된 요소 가져오기 : get(index)
		System.out.println("첫 번째 요소 : " + list.get(0));
		System.out.println("두 번째 요소 : " + list.get(1));
		System.out.println("세 번째 요소 : " + list.get(2));
		
		// 3. 크기 : size() -> length와 같은 역할
		for(int i=0; i<list.size(); i++) {
			System.out.println(list.get(i));
		}
		
		// 4. 삭제 : remove(index), clear()
		list.remove(0);		// 0번 인덱스의 데이터 삭제
//		list.clear();		// 저장된 모든 객체를 삭제
		
		System.out.println(list);
		
		// 5. 확인(저장 유무 확인) : contains(객체)
		List<Integer> list2 = new ArrayList<Integer>();
		list2.add(5);
		list2.add(6);
		list2.add(7);
		for(int i=0; i<list2.size(); i++) {
			if(list.contains(list2.get(i))) {
				System.out.println("중복되는 값 : " + list2.get(i));
			}
		}
		
		// 6. 컬렉션은 그냥 출력해도 된다.
		System.out.println(list);
		
		// 7. 정렬 : sort(list)
		Collections.sort(list);
		System.out.println("정렬 후 list : " + list);
		
		// 8. 최악의 성능으로 list의 모든 데이터 삭제
		while(list.size() > 0) {
			list.remove(0);		// 앞 인덱스부터 삭제
		}
		
		
		
	}
}

'JAVA > chapter23_collection_framework' 카테고리의 다른 글

chapter06 : HashMap  (0) 2023.05.31
chapter05 : TreeSet  (0) 2023.05.31
chapter04 : HashSet  (0) 2023.05.31
chapter03 : LinkedList  (0) 2023.05.31
chapter02 : iterator  (0) 2023.05.31