
Inner class (= Nested class) :
๐ Note Code
ListIterator vs iterator ์ฐจ์ด ?
: โ ๋ ๋ค Interface / Java์์ Collection ์ํ (๋ฐ๋ณต์)
Iterator : ๋จ์ผ๋ฐฉํฅ (์์ผ๋ก = ๋ค์์์๋ง only) / ์ด์ ์์๋ก ๋์๊ฐ๊ฑฐ๋ ์์ ํ ์ ์์ / List, Set , Map ์ธํฐํ์ด์ค์ ๋๊ฐ์ด ์ฌ์ฉํ ์ ์์ / remove ์ญ์ ๋ง ๊ฐ๋ฅ
- iterator.remove(); // ํ์ฌ ์ํ ์ค์ธ ์์๋ฅผ ์ญ์ ํฉ๋๋ค.
- myList.add("Four"); // ์๋ฌ (์ญ์ ๋ง ๊ฐ๋ฅํ๋ฏ๋ก)
- iterator.set("NewValue"); // ์๋ฌ
Test1
๐ป ์ ๋ ฅ
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
//ArrayListํ์ฉ
public class Test1 {
public static void main(String[] args) {
ArrayList<String> lists = new ArrayList<String>();
lists.add("์์ธ");
lists.add("๋ถ์ฐ");
lists.add("๋๊ตฌ");
ListIterator<String> it = lists.listIterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println("\n--------------");
//ListIterator๋ง ๊ฐ๋ฅ (์๋ฐฉํฅ)
while (it.hasPrevious()) { //eof๊น์ง ๋ฐ์ดํฐ ๋ด๋ ค๊ฐ๊ฑธ ์ญ์ผ๋ก ๋๋๋ฆด ์ ์์
System.out.print(it.previous() + " ");
}
System.out.println();
List<String> lists1 = new ArrayList<String>();
lists1.addAll(lists); //์์ธ๋ถ์ฐ๋๊ตฌ
lists1.add("์ธ์ฒ");//์์ธ๋ถ์ฐ๋๊ตฌ์ธ์ฒ
//indexOf์ ๊ฐ์ ๋ฉ์๋๋ก ์์น ํ์ธํ๋ ์์
์ ์ผ๋ฐ์ ์ผ๋ก ListIterator๊ฐ ๋ ํธ๋ฆฌํจ
//collection์์ collection๋ฃ์ ์ ์์
int n = lists1.indexOf("๋ถ์ฐ"); //๋ถ์ฐ์ index =1
lists1.add(n+1,"๊ด์ฃผ");//n+1=2, ๊ทธ์๋ฆฌ์ ๊ด์ฃผ ๋ฃ๊ณ ๊ธฐ์กด์ ๋ค ๋ค๋ก ๋ฐ๊ธฐ
for(String s : lists1) {
System.out.print(s+" ");
}
System.out.println();
Iterator<String> it1 = lists1.iterator();
while (it.hasNext()) {
String str = it.next();
if (str.startsWith("๋ถ์ฐ")) {
System.out.print(str + " ");
}
}
}
}
์์ธ ๋ถ์ฐ ๋๊ตฌ
--------------
๋๊ตฌ ๋ถ์ฐ ์์ธ
์์ธ ๋ถ์ฐ ๊ด์ฃผ ๋๊ตฌ ์ธ์ฒ
๋ถ์ฐ
๐ Note Code
Test2
๐ป ์ ๋ ฅ
import java.util.Hashtable;
import java.util.Iterator;
public class Test2 {
public static String name[] = { "๋ฐฐ์์ง", "์ด์ด๋ด", "์ ์ธ๋", "๋ฐ์ ํ", "๊ณ ๋ฌธ์" }; // value
public static String tel[] = { "111-111", "222-222", "333-333", "111-111", "555-555" }; // key : ์ ์ผํ ๊ฐ
public static void main(String[] args) {
// Map์ผ๋ก ๊ณ ์ณ๋๋จ -> Map<String, String> h = new Hashtable<String, String>();
Hashtable<String, String> h = new Hashtable<String, String>();
for (int i = 0; i < name.length; i++) {
h.put(tel[i], name[i]);
}
System.out.println(h); //[Map]์์ ๊ฐ์ฅ ๋น ๋ฅด๊ณ ํจ์จ์ ์ด๊ฒ ์ถ๋ ฅํด์ค ๊ฒฐ๊ณผ๋ฌผ
String str; //value๊ฐ //String์ ์ง์ญ๋ณ์์ - string์ null์ด๋ ๋น๊ตํ ๋๋ ==๋ก ๋น๊ต
str = h.get("111-111");//key๋ฅผ ์ฃผ๋ฉด value๋ฅผ ์ค
if(str==null) {
System.out.println("์๋ฃ์์");
}else {
System.out.println(str);
}
if(h.containsKey("222-222")) {
System.out.println("222-222ํค๊ฐ ์๋ค");
}
if(h.contains("์ด์ด๋ด")) { //์ฌ์ฉํ๋๊ฑฐ ๋๋ฌผ๋ค
System.out.println("์ด๋ด์ด ์์ด์!");
}
h.remove("222-222"); //์ญ์
if (h.containsKey("222-222")) { //์๋ค์๋ค๋ฅผ ์๋ ค์ฃผ๋ key
System.out.println("222-222ํค๊ฐ ์๋ค");
} else {
System.out.println("222-222ํค๊ฐ ์๋ค");
}
//map,set (๋ชจ๋ collection) ๋ฐ์ดํฐ ํ์
= object
//์ค๋ฆฌ์ง๋ <> ์ด ์๋ฆฌ ์ฃผ์ธ = set / set์๋ฐ์ดํฐํ์
String
Iterator<String> it = h.keySet().iterator();//<iterator ์ํ์ - key์ ํ์
๊ฐ์ ์จ์ฃผ๋ฉด๋จ>
while(it.hasNext()) {
String key = it.next(); // ๋ฐ์ดํฐ๊ฐ key์ vaule๋ก ๋๋์ด์ ธ์์ด์ key๋ง ๋จผ์ ๊บผ๋ = key์ ์๋ฃํ : string
String value = h.get(key); //key๋ฅผ ์ฃผ๋ฉด value๊ฐ์ ์ ์์
System.out.println(key+" : "+value);
}
}
}
{333-333=์ ์ธ๋, 111-111=๋ฐ์ ํ, 222-222=์ด์ด๋ด, 555-555=๊ณ ๋ฌธ์}
๋ฐ์ ํ
222-222ํค๊ฐ ์๋ค
์ด๋ด์ด ์์ด์!
222-222ํค๊ฐ ์๋ค
333-333 : ์ ์ธ๋
111-111 : ๋ฐ์ ํ
555-555 : ๊ณ ๋ฌธ์