728x90

1.Vector 컬렉션 클래스(멀티스레드에 안전하고, 동기화 처리가 되어있다.)

-List 인터페이스 구현한 클래스

- 순서 유지가 가능하고, 중복 허용이 가능하다.

-Arraylist와의 차이점으로는 Vector은 멀티 스레드에 안전하다.


public class Ex02{
	Public static void main(String[] args) {
    //Vector v = new Vector(10, 0); //10개 다 차면 0개 씩 증가시키겠다.
    Vector v = new Vector(); //기본형으로 초기용량 10, 증가치가 0인것
    
    v.add("신짱구");
    v.addElement("신형만");
    
    //요소갯수
    System.out.println(v.size()); //2 요소 2개를 추가했기 때문에 2가 나오고
    System.out.println(v.capcity()); //10 기본용량은 10이기 때문에 10이 나온다.
    
    v.trimToSize(); //Size에 공백을 제거 하였기 때문에
    System.out.println(v.capacity()); //2가 출력된다.
    
    v.setSize(6); //안에 들어가있는 요소가 6개 2개의 요소는 박현주, 박진용이 들어가 있는거고 나머지 4개는 null이 들어감
	System.out.println(v.size()); //6
	System.out.println(v.capacity()); //6
    
    System.out.println(v); //[신짱구, 신형만, null, null, null, null]
		
	System.out.println(v.get(0)); //신짱구
	System.out.println(v.get(1)); //신형만
    
    //Returns the component at the specified index.
	//This method is identical in functionality to the get(int) method (which is part of the List interface).
	System.out.println(v.elementAt(0)); //신짱구
	System.out.println(v.elementAt(1)); //신형만
    
    //Iterator [반복자]
	//vector안에 있는 모든 요소를 반환하는 메서드
	//Enumeration [열거자]
	Enumeration en = v.elements(); //열거자를 돌려준다.
	//hasMoreElements() 				==반복자의 hasMoreNext()랑 똑같음
	//다음 요소 가지고 있니?
	//en.hasMoreElements()				==next
	while (en.hasMoreElements()) {
		String name = (String) en.nextElement();
		System.out.println(name);
		
	}
		
	//v.setSize(6) nullX4
	//[문제] v안에 null인 요소는 모두 제거하세요.
	
	//v.remove(0); == v.removeElementAt(0);
	//v.remove("홍길동"); == v.removeElement("홍길동");
	//v.removeAllElements();==v.clear();
	/*
	v.removeIf(new Predicate<String>() {
		@Override
		public boolean test(String t) {
			return t==null;
		}
	});
	System.out.println(v);
	*/
	System.out.println("람다와 스트림");
	v.removeIf( (t) -> t == null);
	
	System.out.println(v);
	
	Object [] names = v.toArray();
	
	for(Object obj:names) {
		System.out.println( (String)obj );
	}
		
	//첫번째 요소를 가져오겠다.
	//v.firstElement()==v.get(0)==v.elementAt(0)
	
	//마지막 요소를 가져오겠다.
	//v.lastElement()==v.get(v.size()-1) == v.elementAt(v.size()-1)
	
	//박찬호를 1번 값에 집어 넣겠다.
	//v.insertElementAt("박찬호", 1);== v.add(1, "박찬호");
	//isXXX() ~이니 아니니, 항상 true/false
	
	//v.clear();
	//System.out.println(v.size()); //0
	//System.out.println(v.isEmpty()); //true
		
	//[a,b,c,d,e,f]
	//v.subList(int fromIndex, int toIndex)
	//List slist = v.subList(1,2); //1<=요소 <4
	//System.out.println(slist);
	
	//v.sort(null)
	//v.setElementAt("홍진용", 1); == v.set(1, "홍진용");

    
    
    
    
    
    
    
    }

}
728x90