subList() วิธีการคืออะไร?
Collections Framework เป็นองค์ประกอบที่นิยมมากใน Java API ส่วนต่อประสานรายการและคลาส ArrayList อาจเป็นเครื่องมือที่สำคัญที่สุดใน Collections Framework รายการย่อยเป็นวิธีการในอินเทอร์เฟซรายการที่ให้คุณสร้างรายการใหม่จากส่วนหนึ่งของรายการที่มีอยู่ อย่างไรก็ตาม รายการที่สร้างขึ้นใหม่นี้เป็นเพียงมุมมองที่มีการอ้างอิงถึงรายการต้นฉบับเท่านั้น
ตัวอย่างของวิธีการ subList บนวัตถุ ArrayList
เรากำลังประกาศ ArrayList ของประเทศต่างๆ จากนั้นเราจะพยายามส่งคืนส่วนระหว่างองค์ประกอบที่ 2 และ 4
import java.util.*;
public class Main {
public static void main(String[] args) {
// create an ArrayList
ArrayList list = new ArrayList();
// add values to ArrayList
list.add("USA");
list.add("UK");
list.add("France");
list.add("Germany");
list.add("Russia");
System.out.println("List of the countries:" + list);
//Return the subList : 1 inclusive and 3 exclusive
ArrayList new_list = new ArrayList(list.subList(1, 3));
System.out.println("The subList of the list: "+new_list);
}
}
ผลลัพธ์ของโค้ดด้านบนจะเป็น
รายชื่อประเทศ:[สหรัฐอเมริกา สหราชอาณาจักร ฝรั่งเศส เยอรมนี รัสเซีย] รายการย่อยของรายการ: [สหราชอาณาจักร ฝรั่งเศส]
ใน ArrayList ค่าดัชนีขององค์ประกอบแรกคือ 0 ดังนั้น ค่าดัชนีขององค์ประกอบที่สองและสี่คือ 1 และ 3 ตามลำดับ ดังนั้นเราจึงเรียกใช้ เมธอด sublist()เป็นlist.subList(1, 3 ) อย่างไรก็ตาม โปรดทราบว่าเมธอด subList จะส่งคืนส่วนที่ไม่รวม toIndex ซึ่งเป็นองค์ประกอบที่สี่(“เยอรมนี”)ในกรณีนี้ ดังนั้นจะแสดงผลเป็น"สหราชอาณาจักร"และ"ฝรั่งเศส"เท่านั้น เนื่องจากเอาต์พุตที่ส่งคืนคือ List เอง คุณจึงสามารถเรียกเมธอด List ใดๆ ก็ได้โดยตรง จะเกิดอะไรขึ้นถ้าเราใช้ดัชนีเดียวกันสำหรับทั้งสองพารามิเตอร์ ดัชนีนั้นจะถูกรวมหรือไม่รวมอยู่ในรายการที่ส่งคืนหรือไม่ ลองหากัน
//execute subList() method with the same argument for both parameters.
ArrayList new_list2 = new ArrayList(list.subList(3, 3));
System.out.println("The subList of the list: "+new_list2);
ผลลัพธ์คือ
รายการย่อยของรายการ: [ ]
ผลลัพธ์คือรายการว่าง แม้ว่า fromIndex จะเลือกองค์ประกอบที่ 4 แต่ เมธอด subList()จะลบออกเนื่องจากเป็น toIndex ด้วย
ตัวอย่างของวิธีการ subList บนวัตถุ LinkedList
ในตัวอย่างนี้ เราจะใช้วิธีรายการย่อยในองค์ประกอบ LinkedList อีกครั้ง มันจะส่งคืนรายการระหว่างดัชนีที่ระบุfromIndex(inclusive)และtoIndex(exclusive ) โปรดจำไว้ว่าเรากล่าวว่ารายการที่ส่งคืนโดย เมธอด subList()เป็นเพียงมุมมองที่มีการอ้างอิงถึงรายการต้นฉบับ หากคุณทำการเปลี่ยนแปลงใด ๆ ในรายการย่อย จะมีผลกับรายการเดิมด้วย เราจะทดสอบในตัวอย่างนี้เช่นกัน
import java.util.LinkedList;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Create a LinkedList
LinkedList linkedlist = new LinkedList();
// Add elements to LinkedList
for(int i = 0; i<7; i++){
linkedlist.add("Node "+ (i+1));
}
// Displaying LinkedList elements
System.out.println("Elements of the LinkedList:");
Iterator it= linkedlist.iterator();
while(it.hasNext()){
System.out.print(it.next()+ " ");
}
// invoke subList() method on the linkedList
List sublist = linkedlist.subList(2,5);
// Displaying SubList elements
System.out.println("\nElements of the sublist:");
Iterator subit= sublist.iterator();
while(subit.hasNext()){
System.out.print(subit.next()+" ");
}
/* The changes you made to the sublist will affect the original LinkedList
* Let’s take this example - We
* will remove the element "Node 4" from the sublist.
* Then we will print the original LinkedList.
* Node 4 will not be in the original LinkedList too.
*/
sublist.remove("Node 4");
System.out.println("\nElements of the LinkedList LinkedList After removing Node 4:");
Iterator it2= linkedlist.iterator();
while(it2.hasNext()){
System.out.print(it2.next()+" ");
}
}
}
ผลลัพธ์จะมีลักษณะดังนี้:
องค์ประกอบของ LinkedList: โหนด 1 โหนด 2 โหนด 3 โหนด 4 โหนด 5 โหนด 6 โหนด 7 องค์ประกอบของรายการย่อย: โหนด 3 โหนด 4 โหนด 5 องค์ประกอบของ LinkedList LinkedList หลังจากลบโหนด 4: โหนด 1 โหนด 2 โหนด 3 โหนด 5 โหนด 6 โหนด 7
จะเกิดอะไรขึ้นหากดัชนีอยู่นอกขอบเขตใน subList()
เมธอด รายการ ย่อยส่งคืนข้อยกเว้นสองประเภท ลองดูที่พวกเขา พิจารณาสถานการณ์หากดัชนีที่ระบุอยู่นอกช่วงขององค์ประกอบรายการ(fromIndex < 0 || toIndex > size ) จากนั้น มัน จะโยนIndexOutOfBoundExecption
//using subList() method with fromIndex <0
ArrayList new_list2 = new ArrayList(list.subList(-1, 3));
System.out.println("Portion of the list: "+new_list2);
Exception in thread "main" java.lang.IndexOutOfBoundsException: fromIndex = -1
// using subList() method with toIndex > size
ArrayList new_list2 = new ArrayList(list.subList(3, 6));
System.out.println("Portion of the list: "+new_list2);
Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 6
นอกจากนี้ หาก fromIndex มากกว่า toIndex (fromIndex > toIndex)เมธอด subList() จะแสดงข้อผิดพลาด IllegalArgumentException
//If fromIndex > toIndex
ArrayList new_list2 = new ArrayList(list.subList(5, 3));
System.out.println("Portion of the list: "+new_list2);
Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(5) > toIndex(3)
บทสรุป
ในบทความนี้ เราได้กล่าวถึง เมธอด subListและวิธีการใช้งาน เมธอด subList()ช่วยลดความจำเป็นในการดำเนินการช่วงที่ชัดเจน (เป็นประเภทของการดำเนินการที่มีอยู่ทั่วไปสำหรับอาร์เรย์) สิ่งที่สำคัญที่สุดที่ต้องจำไว้คือเมธอด subList จะไม่ส่งคืนอินสแตนซ์ใหม่ แต่เป็นการดูที่มีการอ้างอิงถึงรายการดั้งเดิม ดังนั้น การใช้ เมธอด subList มากเกินไป ในรายการเดียวกันอาจทำให้เธรดค้างอยู่ในแอปพลิเคชัน Java ของคุณ
อ่านเพิ่มเติม: |
---|
GO TO FULL VERSION