2015년 9월 2일 수요일

[자바열거자 Enumeration을 개선된 for문으로 변경하기]

[자바열거자 Enumeration을 개선된 for문으로 변경하기]

아래에서 주석처리한 부분이 과거 Enumeration을 이용하여 컬렉션에서 자료를 순회, 탐색하는 방법이며 이를 개선된 for문으로 변경 하였으니 참고하세요.

package java1;

import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;

class ClassTest {
public static void main(String[] args) {
Properties prop = System.getProperties();
// Enumeration enum1 = prop.keys();

for (Map.Entry<Object, Object> entry : prop.entrySet()) {
System.out.println("key : " + entry.getKey() + "-"
+ entry.getValue());

// while (enum1.hasMoreElements()) {
// String key = (String) enum1.nextElement();
// String val = (String) prop.get(key);
// System.out.println("KEY:" + key + ",Value:" + val);
// }
}
}
}

[결과]
key : java.runtime.name-Java(TM) SE Runtime Environment
key : sun.boot.library.path-C:\Program Files\Java\jre7\bin
key : java.vm.version-23.25-b01
key : java.vm.vendor-Oracle Corporation
key : java.vendor.url-http://java.oracle.com/
key : path.separator-;
key : java.vm.name-Java HotSpot(TM) Client VM
key : file.encoding.pkg-sun.io
key : user.country-KR
key : user.script-
key : sun.java.launcher-SUN_STANDARD
key : sun.os.patch.level-Service Pack 3
key : java.vm.specification.name-Java Virtual Machine Specification
key : user.dir-C:\java3\project\java1
key : java.runtime.version-1.7.0_25-b16
key : java.awt.graphicsenv-sun.awt.Win32GraphicsEnvironment
key : java.endorsed.dirs-C:\Program Files\Java\jre7\lib\endorsed
key : os.arch-x86
key : java.io.tmpdir-C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\
key : line.separator-

key : java.vm.specification.vendor-Oracle Corporation
key : user.variant-
key : os.name-Windows XP
key : sun.jnu.encoding-MS949
key : java.library.path-C:\Program Files\Java\jre7\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;D:\app\Administrator\product\11.2.0\client_1;D:\app\Administrator\product\11.2.0\client_1\bin;c:\app\Administrator\product\11.2.0\dbhome_5\bin;c:\app\Administrator\product\11.2.0\dbhome_3\bin;c:\app\Administrator\product\11.2.0\dbhome_1\bin;C:\oraclexe\app\oracle\product\11.2.0\server\bin;;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Java\jdk1.7.0_25\bin;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\java3\eclipse;;.
key : java.specification.name-Java Platform API Specification
key : java.class.version-51.0
key : sun.management.compiler-HotSpot Client Compiler
key : os.version-5.1
key : user.home-C:\Documents and Settings\Administrator
key : user.timezone-
key : java.awt.printerjob-sun.awt.windows.WPrinterJob
key : file.encoding-MS949
key : java.specification.version-1.7
key : java.class.path-C:\java3\project\java1\bin
key : user.name-Administrator
key : java.vm.specification.version-1.7
key : sun.java.command-java1.ClassTest 101 102 103 101
key : java.home-C:\Program Files\Java\jre7
key : sun.arch.data.model-32
key : user.language-ko
key : java.specification.vendor-Oracle Corporation
key : awt.toolkit-sun.awt.windows.WToolkit
key : java.vm.info-mixed mode, sharing
key : java.version-1.7.0_25
key : java.ext.dirs-C:\Program Files\Java\jre7\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
key : sun.boot.class.path-C:\Program Files\Java\jre7\lib\resources.jar;C:\Program Files\Java\jre7\lib\rt.jar;C:\Program Files\Java\jre7\lib\sunrsasign.jar;C:\Program Files\Java\jre7\lib\jsse.jar;C:\Program Files\Java\jre7\lib\jce.jar;C:\Program Files\Java\jre7\lib\charsets.jar;C:\Program Files\Java\jre7\lib\jfr.jar;C:\Program Files\Java\jre7\classes
key : java.vendor-Oracle Corporation
key : file.separator-\
key : java.vendor.url.bug-http://bugreport.sun.com/bugreport/
key : sun.io.unicode.encoding-UnicodeLittle
key : sun.cpu.endian-little
key : sun.desktop-windows
key : sun.cpu.isalist-pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86


댓글 없음:

댓글 쓰기