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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | package com.test; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util. Set ; public class Test { public static void main(String[] args) { // 컬렉션: 하나의 변수에 여러개값을 저장 List<string> lists = new ArrayList<string>(); lists. add ( "1" ); lists. add ( "2" ); lists. add ( "3" ); lists. add ( "4" ); lists. add ( "5" ); lists. add ( "1" ); System. out .println(lists); // 순서( order )가 있음. 배열과 다르게 크기가 가변적임. 중복적용이안됨 // list는 인터페이스 for (String s:lists){ System. out .println(s); }//1 (jdk1.4는 못씀) Iterator<string> it = lists.iterator(); while(it.hasNext()){ String s = it. next (); System. out .println(s); }//2 for ( int idx=0; idx<lists. size (); idx++){= "" string= "" s= "lists.get(idx);" system. out .println(s);= "" }= "" 3= "" set <string= "" > set = new HashSet<string>(); set . add ( "1" ); set . add ( "2" ); set . add ( "3" ); set . add ( "4" ); set . add ( "5" ); set . add ( "1" ); System. out .println( set ); // 중복적용안됨. 순서가 없음 // List와 출력방법 동일 Map<string, string= "" > map = new HashMap<string, string= "" >(); map.put( "1" , "11" ); map.put( "2" , "22" ); map.put( "3" , "33" ); map.put( "4" , "44" ); map.put( "5" , "55" ); map.put( "1" , "66" ); System. out .println(map); // 순서가 없음. key ( set 의 역활), value Iterator<string> it1 = map.keySet().iterator(); while(it1.hasNext()){ String k = it1. next (); String v = map.get(k); System. out .println(k+ ":" +v); }// } }// </string></string,></string,></string></lists. size ();></string></string></string> |
'java' 카테고리의 다른 글
배열 + 제어문으로 중복값 찾기 (0) | 2013.07.25 |
---|---|
자바 리플렉션 사용하기 (Using Java Reflection) (0) | 2013.07.25 |
Enumeration, 열거형 (0) | 2013.07.25 |
자바 디컴파일러 JAD - 다운로드 사용법 (0) | 2013.07.25 |
아스키코드 값 (0) | 2013.07.25 |