Collections - list

jinkyung·2021년 1월 15일
0

JAVA2

목록 보기
26/35

string 제네릭을 사용한 arraylist

package ex05.collections.list00;

import java.util.ArrayList;

public class ArrayListEx {
	public static void main(String[] args) {
		ArrayList<String> arrList = new ArrayList<String>();
		arrList.add("홍길동");
		arrList.add("임꺽정");
		arrList.add("장길산");
		arrList.add("일지매");
		arrList.add("차돌바위");
		for (int i = 0; i < arrList.size(); i++) {
			System.out.println(arrList.get(i) + " ");
		}
		System.out.println();
		
		for(String str : arrList) {
			System.out.println(str + " ");
		}
	}
}

제네릭에는 기본 자료형이 올 수 없기 때문에 Wrapper class를 만들었다.

package ex05.collections.list01;

import java.util.ArrayList;

public class ArrayListEx {
	public static void main(String[] args) {
		//ArrayList<int> arrList = new ArrayList<int>();
		ArrayList<Integer> arrList = new ArrayList<Integer>();
		arrList.add(10);	// int 10이 -> Integer로 변환되어서 삽입
		arrList.add(20);
		arrList.add(30);
		arrList.add(40);
		arrList.add(50);
		for (int i = 0; i < arrList.size(); i++) {
			System.out.println(arrList.get(i) + " ");
		}
		System.out.println();
		
		for(int num : arrList) {
			System.out.println(num + " ");
		}
	}
}


객체 타입의 제네릭을 이용한 arraylist

tostring을 오버라이딩 해야 값이 나온다.

package ex05.collections.list02;

import java.util.ArrayList;

class Human{
	private String name;
	private int age;
	
	Human(String name, int age){
		this.name = name;
		this.age = age;
	}

	@Override
	public String toString() {
		return name+":"+age;
		//return super.toString();		//object 클래스의 tostring 
	}
	
	
}

public class ArrayListEx {
	public static void main(String[] args) {
		ArrayList<Human> arrList = new ArrayList<Human>();
		arrList.add(new Human("홍길동", 24));
		arrList.add(new Human("임꺽정", 33));
		arrList.add(new Human("장길산", 44));
		arrList.add(new Human("일지매", 55));
		arrList.add(new Human("차돌바위", 66));
		for (int i = 0; i < arrList.size(); i++) {
			System.out.println(arrList.get(i) + " ");
		}
		System.out.println();
		
		for(Human hu : arrList) {
			// Object로부터 상속받은 String toString()가 호출됨
			System.out.println(hu + " ");
		}
	}
}

-삭제

package ex05.collections.list03;

import java.util.ArrayList;

class Human{
	private String name;
	private int age;
	
	Human(String name, int age){
		this.name = name;
		this.age = age;
	}

	
	
	
	public String getName() {
		return name;
	}


	public int getAge() {
		return age;
	}


	@Override
	public String toString() {
		return name+":"+age;
		//return super.toString();		//object 클래스의 tostring 
	}
	
	
}

public class ArrayListEx {
	public static void main(String[] args) {
		ArrayList<Human> arrList = new ArrayList<Human>();
		arrList.add(new Human("홍길동", 24));
		arrList.add(new Human("임꺽정", 33));
		arrList.add(new Human("장길산", 44));
		arrList.add(new Human("일지매", 55));
		arrList.add(new Human("차돌바위", 66));

		System.out.println();
		
		
		arrList.remove(0);
		
		for(Human hu : arrList) {
			System.out.println(hu + " ");
		}
		System.out.println();
		
		
		//장길산만 삭제하고 싶다면 
		for (int i = 0; i < arrList.size(); i++) {
			if(arrList.get(i).getName().equals("장길산"))
			{
				arrList.remove(i);
				break;
			}
		}
		
		for(Human hu : arrList) {		//장길산이 삭제되어 출력된다.
			System.out.println(hu + " ");
		}
		System.out.println();
	}
}


linked list

package ex05.collections.list04;

import java.util.LinkedList;

public class LinkedListEx {
	public static void main(String[] args) {
		LinkedList<String> arrList = new LinkedList<String>();
		arrList.add("홍길동");
		arrList.add("임꺽정");
		arrList.add("장길산");
		arrList.add("일지매");
		arrList.add("차돌바위");
		for (int i = 0; i < arrList.size(); i++) {
			System.out.println(arrList.get(i) + " ");
		}
		System.out.println();
		
		for(String str : arrList) {
			System.out.println(str + " ");
		}
    }
 }


vector

package ex05.collections.list05;

import java.util.Vector;

public class VectorEx {
	public static void main(String[] args) {
		Vector<String> arrList = new Vector<String>();
		arrList.add("홍길동");
		arrList.add("임꺽정");
		arrList.add("장길산");
		arrList.add("일지매");
		arrList.add("차돌바위");
		for (int i = 0; i < arrList.size(); i++) {
			System.out.println(arrList.get(i) + " ");
		}
		System.out.println();
		
		for(String str : arrList) {
			System.out.println(str + " ");
		}
	}
}



결국 arraylist, linkedlist, vector는 모두 비슷하며 순서가 있음

-iterator로 접근 가능 --> 나중에 자료구조를 바꾸기 쉽도록 iterator를 사용.

package ex05.collections.iterator07;

import java.util.ArrayList;
import java.util.Iterator;

public class IteratorEx {
	public static void main(String[] args) {
	
	ArrayList<String> arrList = new ArrayList<String>();
	arrList.add("홍길동");
	arrList.add("임꺽정");
	arrList.add("장길산");
	arrList.add("일지매");
	arrList.add("차돌바위");


	Iterator<String> itr = arrList.iterator();
	while(itr.hasNext())
		System.out.println(itr.next());
		
	}

}

0개의 댓글

관련 채용 정보