ืืื ืฉืืืช subList()?
ืืกืืจืช ืืืืกืคืื ืืื ืจืืื ืคืืคืืืจื ืืืื ื-Java API. ืืืฉืง ืจืฉืืื ืืืืืงื ArrayList ืื ืื ืจืื ืืืืื ืืืฉืืืื ืืืืชืจ ืืืกืืจืช Collections. subList ืืื ืฉืืื ืืืืฉืง List ืืืืคืฉืจืช ืื ืืืฆืืจ ืจืฉืืื ืืืฉื ืืืืง ืืจืฉืืื ืงืืืืช. ืขื ืืืช, ืืจืฉืืื ืืืืฉื ืฉื ืืฆืจื ืืื ืจืง ืชืฆืืื ืขื ืืคื ืื ืืจืฉืืื ืืืงืืจืืช. ืืืืืื, ืงื ืืช ืืจืฉืืื ืฉื [1,2,3,4,5,6]. ื ื ืื ืฉืืจืฆืื ื ืืืฆืืจ ืจืฉืืื ืืืฉื ืืื ืืจืืืืื ืืจืืฉืื ืื ืืืืืจืื ืื. ืืชืจืืืฉ ืืื, ืืฉืืื list.subList() ืชืขืืืจ ืื. ืืืชืืื subList(fromIndex, toIndex) ืืฉ ืจืง ืฆืืจื ืืืช ืืืื ืืืจืฉืช ืฉื ื ืืจืืืื ืืื, ืฉืื ืืืื ืืงืก ืืจืืฉืื (fromIndex) ืืืืื ืืงืก ืืืืจืื (toIndex) . ืื ืืืืืจ ืืช ืืืืง ืฉืืื fromIndex ื- toIndex ืืจืฉืืื ืืืฉื. ืืฉ ื ืงืืื ืืฉืืื ืฉืฆืจืื ืืืืืจ. ืืจืฉืืื ืืืืฉื ืฉื ืืฆืจื ืชืืืื ืืช fromIndex ืืื ืชืืืื ืืช toIndex. ืื ืืืืืืจืืชื ืขืืืจ ืืชืจืืืฉ ืืขืื ืืืื ืืฉืื ืืื. List = [1,2,3,4,5,6] newList = List.subList(1,5) ืืืืืื ืฉ- subList ืืื ืฉืืื ืฉื โโืืืฉืง ืจืฉืืื, ืืชื ืืืื ืืืฉืชืืฉ ืื ืืืืืืืงืืื ArrayList, LinkedList, Stack ื-Vector. ืขื ืืืช, ืืืืืจ ืื ื ืชืืงื ืืขืืงืจ ืืืืืืืงืืื ArrayList ื-LinkedList.ืืืืื ืืฉืืืช subList ืืืืืืืงื ArrayList.
ืื ื ืืืจืืืื ืขื ArrayList ืฉื ืืืื ืืช. ืืืืจ ืืื ื ื ืกื ืืืืืืจ ืืช ืืืืง ืฉืืื ืืืืื ื ืืฉื ื ืืืจืืืขื.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 ืืืืจ ืืช ืืืืื ื ืืจืืืขื, ืืฉืืื subList() ืชืกืืจ ืืืชื ืืืืืื ืฉืืื ืื ื-toIndex.
ืืืืื ืืฉืืืช subList ืืืืืืืงื LinkedList.
ืืืืืื ืื, ื ืฉืชืืฉ ืืฉืืืช sublist ืืจืืื LinkedList. ืฉืื, ืื ืืืืืจ ืืช ืืจืฉืืื ืืื ืืืื ืืงืก ืฉืฆืืื fromIndex(ืืืื) ื- toIndex(ืืืขืื) . ืืืืจ ืฉืืืจื ื ืฉืืจืฉืืื ืืืืืืจืช ืขื ืืื ืฉืืืช 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: Node 1 Node 2 Node 3 Node 4 Node 5 Node 6 Node 7 ืืืื ืืื ืฉื ืชืช-ืืจืฉืืื: Node 3 Node 4 Node 5 ืืืื ืืื ืฉื LinkedList LinkedList ืืืืจ ืืกืจืช ืฆืืืช 4: Node 1 Node 2 Node 3 Node 5 Node 6 ืฆืืืช 7
ืื ืืงืจื ืื ืืืื ืืงืกืื ืืืื ืืืืฅ ืืชืืื ื-subList()?
ืฉืืืช subList ืืืืืจื ืฉื ื ืกืืืื ืฉื ืืจืืืื. ืืืื ื ืกืชืื ืขืืืื. ืฉืงืื ืืฆื ืื ืืืื ืืงืกืื ืฉืฆืืื ื ื ืืฆืืื ืืืืฅ ืืืืื ืฉื ืืืื ื List (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