17. การใช1คาส,Eง for เพEออ1างองสมาชกของอะเรย9
7
โดยท,วไปเราจะใช1ค7าส,ง for ในการอ1างองถLงสมาชกของอะเรย9
E E
ต,วอย=างเช=น
for(int i = 0; i < 5; i++) {
System.out.println(x[i]);
}
หรอ
for(int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
18. Enhanced for
เราย,งสามารถท(จะเข(ยนค7าส,ง for ในร<ปแบบท(เร(ยกว=า enhanced for เพEอ
E E E
แจกแจงค=าของต,วแปร อะเรย9โดยอ,ตโนม,ต
ต,วอย=างเช=น
for(int i : x) {
System.out.println(x[i]);
}
19. ต,วอย=างโปรแกรมการใช1ค7าส,ง for
E
public class ForWithArrays {
public static void main(String args[]) {
int []x;
x = new int[4];
for (int i=0; i<x.length; i++) {
x[i] = i*2;
}
System.out.println("x = "+x);
for (int i=0; i<x.length; i++) {
System.out.println("x["+i+"] = "+x[i]);
}
}
}
31. ต,วอย=างโปรแกรม
public class CopyArrays {
public static void main(String args[]) {
String []scr = {"Copy","an","array","from",
" source"," to"," destination."};
String []dst = new String[4];
System.arraycopy(scr,3,dst,0,4);
for(int i=0; i<dst.length; i++) {
System.out.print(dst[i]);
}
System.out.println();
}
}
ผลล,พธ9ท(Eได1จากการร,นโปรแกรม from source to destination.
32. Collection API
คลาสใน Collection API สามารถท(Eจะน7ามาใชเกบขอม"ลทเป2นออปเจคได
หลายตว
โครงสร1างข1อม<ลของคลาสใน Collection API จะคลายกบของอะเรย ต*างกน
ตรงทขนาดโครงสร1างข1อม<ลของคลาสใน Collection API สามารถท(Eจะถ<ก
เปลยนแปลงได
33. Collection API
Collection API ประกอบไปด1วยอนเตอร9เฟสท(Eส7าค,ญด,งน(J
• Collection
• Set
• List
• Map
Collection API ประกอบไปด1วยคลาสท(Eส7าค,ญด,งน(J
• HashSet
• ArrayList
• Vector
• LinkedList
• HashMap
36. อนเตอร9เฟส Set
สบทอดมาจากอนเตอร9เฟส Collection
ค=าข1อม<ลของสมาชกจะไม*สามารถซ&(ากนได1 และล ดบของสมาชกไม=ม(
ความส7าค,ญ
คลาสส7าค,ญท(E implement อนเตอร9เฟส Set คอคลาส HashSet
37. ต,วอย=างโปรแกรม Set
import java.util.*;
public class SampleSet {
public static void main(String args[]) {
Set s = new HashSet();
s.add("C#");
s.add("Java");
s.add("Pascal");
System.out.println("The size of this set is
"+s.size());
System.out.println("The contents are "+s);
System.out.println("Removing C#");
38. ต,วอย=างโปรแกรม Set
s.remove("C#");
System.out.println("Now this set contains C#: "+
s.contains("C#"));
s.add("Java");
System.out.println("Now the size is "+s.size());
System.out.println("The contents are "+s);
}
}
ผลล,พธ9ท(Eได1จากการร,นโปรแกรม
The size of this set is 3
The contents are [Java, Pascal, C#]
Removing C#
Now this set contains C#: false
Now the size is 2
The contents are [Java, Pascal]
40. ต,วอย=างโปรแกรม List
import java.util.*;
public class SampleList {
public static void main(String args[]) {
List l = new LinkedList();
l.add("C#");
l.add("Java");
l.add("Pascal");
System.out.println("The size is "+l.size());
System.out.println("The contents are "+l);
System.out.println("The first one is "+l.get(0));
41. ต,วอย=างโปรแกรม List
l.add("Java");
System.out.println("The contents are "+l);
System.out.println("The index of Java is "+
l.indexOf("Java"));
}
}
ผลล,พธ9ท(Eได1จากการร,นโปรแกรม
The size is 3
The contents are [C#, Java, Pascal]
The first one is C#
The contents are [C#, Java, Pascal, Java]
The index of Java is 1
43. ต,วอย=างโปรแกรม Map
import java.util.*;
public class SampleMap {
public static void main(String args[]) {
Map m = new HashMap();
m.put("1","C#");
m.put("2","Java");
m.put("3","Pascal");
System.out.println("Removing Pascal");
m.remove("3");
System.out.println("The size is "+m.size());
System.out.println("The first one is
"+m.get("1"));
44. ต,วอย=างโปรแกรม Map
m.put("3","Java");
System.out.println("The key of this map are "+
m.keySet());
System.out.println("The contents are "+m.entrySet());
}
}
ผลล,พธ9ท(Eได1จากการร,นโปรแกรม
Removing Pascal
The size is 2
The first one is C#
The key of this map are [3, 2, 1]
The contents are [3=Java, 2=Java, 1=C#]
49. คลาส Vector
เปFนคลาสท(E implements อนเตอร9เฟส List
ม( constructor แบบต=างๆด,งน(J
• new Vector()
• new Vector(int initialCapacity)
• new Vector(int initialCapacity,int capacityIncrement)
50. ต,วอย=างโปรแกรม Vector
import java.util.*;
public class SampleEnumeration {
public static void main(String args[]) {
Vector v = new Vector();
v.add("C#");
v.add("Java");
v.add("Pascal");
Enumeration e = v.elements();
while (e.hasMoreElements()) {
System.out.print(e.nextElement()+"
");
}
}
}
ผลล,พธ9ทได1จากการร,นโปรแกรม
(E C# Java Pascal
51. การใช1ค7าส,ง for และ Generic
E
เราสามารถท(Eจะใช1ค7าส,Eง Genericในการก7าหนดชนดข1อม<ลของอHอบเจกต9ท(E
อย<ในคอลเลHกช,Eน
=
ต,วอย=างเช=น
List<String> myList = new LinkedList<String>();
เราสามารถท(จะใช1ค7าส,ง for
E E ในการท(จะแจกแจงค=าของอHอบเจกต9ประเภท
E
คอลเลHกช,Eนแทนท(จะใช1อนเตอร9เฟส Iterator
E
ต,วอย=างเช=น
for (String stringList : myList) {
System.out.println(stringList);
}
52. ต,วอย=างโปรแกรมการใช1 Generic
public class SampleGeneric {
public static void main(String args[]) {
Set<Integer> scrSet = new HashSet<Integer>();
int[] myInt = {1, 3, 5, 7, 11};
for (int i : myInt) {
scrSet.add(i);
}
for (Integer num : scrSet) {
System.out.print(num + " ");
}
System.out.println();
}
}
53. การใชค(าสง for และ Generic
สบทอดมาจากอนเตอร9เฟส Iterator
ใช1ในการอ1างถLงข1อม<ลสมาชกประเภท List
อนเตอร9เฟส ListIterator มเมธอดทส(าคญทเพมมาจาก Iterator
ดงน&
• boolean hasPrevious()
• Object previous()
• void add(Object element)
• void set(Object element)
ภายในอนเตอร9เฟส List จะม(เมธอดท(ชEอ listIterator() ซLEงเปFน
E
เมธอดท(จะส=งค=ากล,บเปFน ListIterator
E