CodeGym /ื‘ืœื•ื’ Java /Random-HE /ืฉื™ื˜ืช Sublist() ื‘-Java: ArrayList ื•- List
John Squirrels
ืจึธืžึธื”
San Francisco

ืฉื™ื˜ืช Sublist() ื‘-Java: ArrayList ื•- List

ืคื•ืจืกื ื‘ืงื‘ื•ืฆื”

ืžื”ื™ ืฉื™ื˜ืช subList()?

ืžืกื’ืจืช ื”ืื•ืกืคื™ื ื”ื™ื ืจื›ื™ื‘ ืคื•ืคื•ืœืจื™ ืžืื•ื“ ื‘-Java API. ืžืžืฉืง ืจืฉื™ืžื” ื•ืžื—ืœืงื” ArrayList ื”ื ื›ื ืจืื” ื”ื›ืœื™ื ื”ื—ืฉื•ื‘ื™ื ื‘ื™ื•ืชืจ ื‘ืžืกื’ืจืช Collections. subList ื”ื™ื ืฉื™ื˜ื” ื‘ืžืžืฉืง List ื”ืžืืคืฉืจืช ืœืš ืœื™ืฆื•ืจ ืจืฉื™ืžื” ื—ื“ืฉื” ืžื—ืœืง ืžืจืฉื™ืžื” ืงื™ื™ืžืช. ืขื ื–ืืช, ื”ืจืฉื™ืžื” ื”ื—ื“ืฉื” ืฉื ื•ืฆืจื” ื”ื™ื ืจืง ืชืฆื•ื’ื” ืขื ื”ืคื ื™ื” ืœืจืฉื™ืžื” ื”ืžืงื•ืจื™ืช. ืฉื™ื˜ืช Sublist() ื‘-Java: ArrayList ื•- List - 1ืœื“ื•ื’ืžื”, ืงื— ืืช ื”ืจืฉื™ืžื” ืฉืœ [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 ืฉืœืš.
ื”ืขืจื•ืช
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION