งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Java collection framework

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Java collection framework"— ใบสำเนางานนำเสนอ:

1 Lecture6 Array List อ.เหมรัศมิ์ วชิรหัตถพงศ์ คณะวิทยาการสารสนเทศ มหาวิทยาลัยบูรพา

2 Java collection framework

3 The ArrayList Class จากปัญหาของ Array ที่กล่าวมานั้นมีปัญหาในเรื่องของการกำหนดจำนวนข้อมูลที่ต้องกำหนดไว้ตั้งแต่เริ่มต้น ไม่สามารถเพิ่มขยายขนาดได้ตอนประมวลผล การเพิ่ม ลบ ข้อมูลในอาร์เรย์มีข้อจำกัดและเกิดความยุ่งยาก การ Implement LinkedList เองเพื่อแก้ปัญ Array จะต้องเขียนโปรแกรมยุ่งยาก ใน Java จึงได้จัดเตรียม Class พิเศษที่ใช้จัดการคล้าย Array แต่มีความยืดหยุ่นเหมือน Linked List เราเรียกว่า ArrayList Class ซึ่งอยู่ใน Standard Library ของ Java

4 The ArrayList Class การเรียกใช้จะต้อง import java.uril.ArrayList เข้ามาในโปรแกรม การประกาศตัวแปรเป็นชนิด ArrayList จะใช้ ArrayList <T> Object = new ArrayList<T> (); โดยที่ <T> หมายถึงชนิดข้อมูล เช่น String , Int เป็นต้น ตัวอย่าง ArrayList <String> StrList = new ArrayList<String>(); ArrayList <Double> Dlist = new ArrayList<Double>();

5 Example ArrayList<String> names = new ArrayList<String>();
names.add("Marty Stepp"); names.add("Stuart Reges");

6 ArrayList vs. array การสร้าง Array และ ArrayList การกำหนดค่าข้อมูล
String[] names = new String[5]; ArrayList<String> list = new ArrayList<String>(); การกำหนดค่าข้อมูล names[0] = "Jessica"; list.add("Jessica"); การเข้าถึงข้อมูล String s = names[0]; String s = list.get(0);

7 ArrayList vs. array 2 การเข้าถึงข้อมูลและดำเนินการบางอย่างที่ข้อมูล ขั้นต้นด้วย "B" for (int i = 0; i < names.length; i++) { if (names[i].startsWith("B")) { ... } } for (int i = 0; i < list.size(); i++) { if (list.get(i).startsWith("B")) { ... } ค้นหาข้อมูลใน List ที่มีข้อมูล "Benson" if (names[i].equals("Benson")) { ... } if (list.contains("Benson")) { ... }

8 ArrayList as parameter
public static void name(ArrayList<Type> name) { Example: // Removes all plural words from the given list. public static void removePlural(ArrayList<String> list) { for (int i = 0; i < list.size(); i++) { String str = list.get(i); if (str.endsWith("s")) { list.remove(i); i--; } การส่งค่ากลับเป็น ArrayList: public static ArrayList<Type> methodName(params)

9 ArrayList of primitives?
การสร้าง ArrayList จะต้องใช้ object ของ Class มา สร้าง ไม่สามารถกำหนดให้เป็นชนิดข้อมูลพื้นฐานได้ (Primitive Data Type) // illegal -- int cannot be a type parameter ArrayList<int> list = new ArrayList<int>(); แต่สามารถกำหนด ArrayList ให้กับชนิดข้อมูลที่เป็น ชนิดข้อมูลพื้นฐานจะต้องใช้ Class พิเศษเรียกว่า wrapper classes // creates a list of ints ArrayList<Integer> list = new ArrayList<Integer>();

10 Wrapper classes ตัวอย่างการสร้าง ArrayList จากชนิดข้อมูลพื้นฐาน
. ตัวอย่างการสร้าง ArrayList จากชนิดข้อมูลพื้นฐาน ArrayList<Double> grades = new ArrayList<Double>(); grades.add(3.2); grades.add(2.7); ... double myGrade = grades.get(0); Primitive Type Wrapper Type int Integer double Double char Character boolean Boolean

11 ArrayList Method ชื่อ Method ความหมาย add(item) add(index,item)
set(index,item) แทนที่ข้อมูลในตำแหน่งที่ระบุใน index (เหมือนกับ z[index]=item) get(index) ส่งค่าข้อมูล ณ ตำแหน่งที่ระบุใน index (เหมือนกับ data = z[index] ) size() ส่งค่าขนาดของ element ใน ArrayList (เหมือนกับ z.length) remove(index) ลบข้อมูล ณ ตำแหน่งของ index ที่ระบุ (ส่งค่ากลับเป็นข้อมูลที่ถูกลบ) remove(value) ลบข้อมูลที่มีค่าเท่ากับ value (ส่งค่ากลับเป็น true ถ้าสามารถลบได้ และ false ถ้าลบไม่ได้) clear() ลบข้อมูลที่ตัวที่อยู่ใน ArrayList

12 ArrayList Method ชื่อ Method ความหมาย contains(item) indexOf(item)
ใช้ตรวจสอบว่ามีข้อมูล item ใน Array List หรือไม่ (ส่งค่ากลับเป็น true ถ้าพบ และส่งค่ากลับเป็น false ถ้าไม่พบ) indexOf(item) ส่งค่ากลับเป็นตำแหน่ง index ของข้อมูล item ใน ArrayList ตำแหน่งแรกที่พบ lastIndexOf(item) ส่งค่ากลับเป็นตำแหน่ง index ของข้อมูล item ใน ArrayList ตำแหน่งสุดท้ายที่พบ isEmpty(list) ใช้ตรวจสอบว่า ArrayList ว่างหรือไม่ (ส่งค่ากลับเป็น true ถ้า ArrayList นั้นว่างและ false ถ้าไม่ว่า)

13 List operations a b c d x a b c x d a b c d c a b c d a b c d a b c d
add(x) true add(3,x) true a b c d x a b c x d 1 2 3 4 1 2 3 4 remove(x) false remove(c) true remove(1) b a b c d c a b c d 1 2 3 1 2 indexOf(x) -1 get(3) d indexOf(c) 2 a b c d a b c d c 1 2 3 1 2 3 4 ? 4 contains(b) true size() 4 contains(x) false a b c d a b c d 1 2 3 1 2 3

14 Method add(item); ArrayList<String> words;
words = new ArrayList<String>(); words.add(0,"it"); words.add("is"); words.add("a"); words.add("lie"); System.out.println(words); OUTPUT [it, is, a, lie] The add(item) method adds item to the end of the ArrayList. The add(spot, item) method adds the item at the spot specified. All other items are shifted toward the end of the ArrayList.

15 Method add(item); ArrayList<Integer> nums;
nums = new ArrayList<Integer>(); nums.add(34); nums.add(0,99); nums.add(21); nums.add(11); System.out.println(nums); OUTPUT [99, 34, 21, 11] The add(item) method adds item to the end of the ArrayList. The add(spot, item) method adds the item at the spot specified. All other items are shifted toward the end of the ArrayList.

16 Method set(index,item);
ArrayList<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.set(0,66); ray.add(53); ray.set(1,93); ray.add(22); System.out.println(ray); OUTPUT [66, 93, 53, 22] The add(item) method adds item to the end of the ArrayList. The set(spot, item) method replaces the reference at spot with item. The location / address of item is placed in spot.

17 Method size(); for (int i=0; i<ray.size(); i++) {
System.out.println(ray.get(i)); } Method size จะส่งค่ากลับเป็นจำนวนของ element ใน ArrayList. The size() method returns the number of items in the ArrayList. If the ArrayList is storing seven references, size() would return a 7.

18 Method get(); OUTPUT23.23 65.6 ArrayList<Double> ray;
ray = new ArrayList<Double>(); ray.add(23.23); ray.add(11.11); ray.add(12.1); ray.add(65.6); System.out.println(ray.get(0)); System.out.println(ray.get(3)); OUTPUT The get(spot) method returns the reference stored at spot. .get(index) จะส่งค่ากลับเป็น pointer ที่ชี้ไปยังข้อมูลในตำแหน่งที่ index

19 Method get(); OUTPUT23 11 12 65 ArrayList<Integer> ray;
ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.add(12); ray.add(65); for(int i=0; i<ray.size(); i++) System.out.println(ray.get(i)); OUTPUT The get(spot) method returns the reference stored at spot. .get(index) จะส่งค่ากลับเป็น pointer ที่ชี้ไปยังข้อมูลในตำแหน่งที่ index

20 val จะเก็บค่าข้อมูลในแต่ละตำแหน่งของ ArrayList เมื่อมีการวนลูป
ArrayList<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.add(12); ray.add(65); for( int val : ray ) System.out.println(val); OUTPUT The ehanced for loop pulls one value at a time from ray and puts each value in val. val จะเก็บค่าข้อมูลในแต่ละตำแหน่งของ ArrayList เมื่อมีการวนลูป

21 Method remove(index);
ArrayList<String> ray; ray = new ArrayList<String>(); ray.add("a"); ray.add("b"); ray.remove(0); ray.add("c"); ray.add("d"); System.out.println(ray); OUTPUT [c, d] The remove method will remove the item at the specified spot / location or the specified value. When an item is removed, all items above the removed item are shifted down toward the front of the ArrayList. All items are shifted to the left. [a, b] becomes [b] [b, c, d] becomes [c, d]

22 Method remove(value);
ArrayList<String> ray; ray = new ArrayList<String>(); ray.add("a"); ray.add("b"); ray.remove("a"); ray.add("c"); ray.add("d"); ray.remove("d"); System.out.println(ray); OUTPUT [b, c] The remove method will remove the item at the specified spot / location or the specified value. When an item is removed, all items above the removed item are shifted down toward the front of the ArrayList. All items are shifted to the left. [a, b] becomes [b] [b, c, d] becomes [b, c]

23 Method contains(value);
ArrayList<String> ray; ray = new ArrayList<String>(); ray.add("a"); ray.add("x"); ray.contains(“a”);  ส่งค่ากลับเป็น True ray.add("t"); ray.contains("w");  ส่งค่ากลับเป็น false The clear() method removes all items from the ArrayList. The ArrayList becomes an [] empty ArrayList.

24 Method indexOf(value);
ArrayList<String> ray; ray = new ArrayList<String>(); ray.add("a"); ray.add("x"); ray.indexOf(“x”);  ส่งค่ากลับเป็น 1 ray.add("t"); ray.indexOf("w");  ส่งค่ากลับเป็น -1 The clear() method removes all items from the ArrayList. The ArrayList becomes an [] empty ArrayList.

25 Method lastIndexOf(value);
ArrayList<String> ray; ray = new ArrayList<String>(); ray.add("a"); ray.add("x"); ray.add(“x”); ray.lastIndexOf(“x”);  ส่งค่ากลับเป็น 2 ray.add("t"); ray.lastIndexOf(“w");  ส่งค่ากลับเป็น -1 The clear() method removes all items from the ArrayList. The ArrayList becomes an [] empty ArrayList.

26 Method isEmpty(list);
ArrayList<String> ray; ray = new ArrayList<String>(); ArrayList<String> ray1; Ray1 = new ArrayList<String>(); ray.add("a"); ray.add("x"); ray.add(“x”); ray.isEmpty (“x”);  ส่งค่ากลับเป็น false ray1.isEmpty(“w");  ส่งค่ากลับเป็น true The clear() method removes all items from the ArrayList. The ArrayList becomes an [] empty ArrayList.

27 Method clear(); ArrayList<String> ray;
ray = new ArrayList<String>(); ray.add("a"); ray.add("x"); ray.clear(); ray.add("t"); ray.add("w"); System.out.println(ray); OUTPUT [t, w] The clear() method removes all items from the ArrayList. The ArrayList becomes an [] empty ArrayList.

28 Method clear(); การกำหนดค่าเริ่มต้นให้กับ ArrayList?
ArrayList<String> names = new ArrayList<String>(); names.add(“Smith”); names.add(“Jones”); จะได้ผลลัพธ์เดียวกับการกำหนดค่า ArrayList ในลักษณะนี้: ArrayList<String> names = new ArrayList<String> (Arrays.asList("Smith", "Jones")); import java.util.ArrayList; import java.util.Arrays;

29 [A, B, 10, 10, 10.56, true, 10.56] import java.util.*; class arrex1 {
public static void main(String args[]) ArrayList arr = new ArrayList(); // Adding Elements arr.add("A"); arr.add("B"); arr.add(new Integer(10)); arr.add(2,new Integer(10)); arr.add(new Float(10.56f)); arr.add(new Boolean(true)); arr.add(new Double(10.56)); System.out.println(arr); [A, B, 10, 10, 10.56, true, 10.56]

30 System.out.println(arr); arr.remove(2);
// Removing Elements arr.remove("B"); System.out.println(arr); arr.remove(2); arr.set(2,"Object"); arr.set(4,"Java"); } Removes “B” From List [A, 10, 10, 10.56, true, 10.56] Removes element from index 2 from List [A, 10, 10.56, true, 10.56] Sets value for index 2 element as “Object” Sets value for index 4 element as “Java” [A, 10, Object, true, Java]

31 มีที่ผิดคือ // Sum of integers Stored in an Unparametrized Arrayslists
import java.util.*; class sum { public static void main(String args[]) ArrayList integers = new ArrayList(10); // Adding Elements integers.add(10); integers.add(-5); integers.add(12); integers.add(-34); integers.add(3,90); integers.add(3,-110); int sum=0; // Forward Traversing Using for loop for(int i=0;i< integers.size();i++) sum = sum + integers.get(i); System.out.println("Sum ="+sum); } } มีที่ผิดคือ ArrayList in use is Unparametrized. While getting/retrieving elements, We have to typecast it to base type beforeuse

32 การใช้ Type Cast การใช้ Type Cast int sum=0;
// Forward Traversing Using for loop for(int i=0;i< integers.size();i++) sum = sum + ((Integer)integers.get(i)).intValue(); System.out.println("Sum ="+sum); การใช้ Type Cast

33 การพิมพ์ข้อมูลจากตำแหน่งสุดท้าย
int sum=0; // Forward Traversing Using for loop for(int i = integers.size()-1; i>=0; i++) sum = sum + ((Integer)integers.get(i)).intValue(); System.out.println("Sum ="+sum);

34 การใช้ iterator(); การใช้ method iterator()
ใน Object iterator ประกอบด้วย method 3 ตัว: next() – เลื่อนตำแหน่งไปยังตำแหน่ง ถัดไป hasNext() – ตรวจสอบว่าข้อมูลชุดถัดไปมี ข้อมูลหรือไม่ remove() – ลบข้อมูลในตำแหน่งที่ระบุ

35 การใช้ iterator(); ใช้ได้เฉพาะการเข้าถึงข้อมูลจากตำแหน่งแรกไปยังตำแหน่งสุดท้าย int sum=0; // STEP1 get an iterator for ArrayList Iterator itr = integers . iterator(); while(itr.hasNext()) sum = sum + ((Integer)itr.next()).intValue(); System.out.println("Sum ="+sum);

36 Sort() Method static void sort(ArrayList<String> names) {
for(int i=0;i<names.size();i++) for(int j=i+1;j<names.size();j++) String str1 = names.get(i); String str2 = names.get(j); if(str1.compareTo(str2) > 0) String temp = names.get(i); names.set(i,str2); names.set(j,str1); }

37 END!


ดาวน์โหลด ppt Java collection framework

งานนำเสนอที่คล้ายกัน


Ads by Google