Java Collections Framework:ArrayList-5(Get/Search )
How to get sublist of an ArrayList with example
In this tutorial we will see how to get a sublist from an existing ArrayList. We will be doing it using the subList method of ArrayList class.
List subList(int fromIndex, int toIndex)
Here fromIndex
is inclusive and toIndex
is exclusive. There are few important points regarding this method which I have shared at the end of this post.
Example of getting sub-list from an ArrayList
Points to Note in the below example:
The subList method returns a list therefore to store the sublist in another ArrayList we must need to type cast the returned value in same way as I did in the below example. On the other side if we are storing the returned sublist into a list then there is no need to type cast (Refer the example).
package beginnersbook.com; import java.util.ArrayList; import java.util.List; public class SublistExample { public static void main(String a[]){ ArrayList<String> al = new ArrayList<String>(); //Addition of elements in ArrayList al.add("Steve"); al.add("Justin"); al.add("Ajeet"); al.add("John"); al.add("Arnold"); al.add("Chaitanya"); System.out.println("Original ArrayList Content: "+al); //Sublist to ArrayList ArrayList<String> al2 = new ArrayList<String>(al.subList(1, 4)); System.out.println("SubList stored in ArrayList: "+al2); //Sublist to List List<String> list = al.subList(1, 4); System.out.println("SubList stored in List: "+list); } }
Output:
Original ArrayList Content: [Steve, Justin, Ajeet, John, Arnold, Chaitanya] SubList stored in ArrayList: [Justin, Ajeet, John] SubList stored in List: [Justin, Ajeet, John]
Note:
The subList method throws IndexOutOfBoundsException
– if the specified indexes are out of the range of ArrayList (fromIndex < 0 || toIndex > size).IllegalArgumentException
– if the starting index is greater than the end point index (fromIndex > toIndex).
Java ArrayList lastIndexOf(Object 0bj) Method example
The method lastIndexOf(Object obj)
returns the index of last occurrence of the specified element in the ArrayList
. It returns -1 if the specified element does not exist in the list.
public int lastIndexOf(Object obj)
This would return the index of last Occurrence of element Obj in the ArrayList.
Example
In the below example we have an Integer ArrayList which has few duplicate elements. We are fetching the last index of few elements using lastIndexof
method.
package beginnersbook.com; import java.util.ArrayList; public class LastIndexOfExample { public static void main(String args[]) { //ArrayList of Integer Type ArrayList<Integer> al = new ArrayList<Integer>(); al.add(1); al.add(88); al.add(9); al.add(17); al.add(17); al.add(9); al.add(17); al.add(91); al.add(27); al.add(1); al.add(17); System.out.println("Last occurrence of element 1: "+al.lastIndexOf(1)); System.out.println("Last occurrence of element 9: "+al.lastIndexOf(9)); System.out.println("Last occurrence of element 17: "+al.lastIndexOf(17)); System.out.println("Last occurrence of element 91: "+al.lastIndexOf(91)); System.out.println("Last occurrence of element 88: "+al.lastIndexOf(88)); } }
Output:
Last occurrence of element 1: 9 Last occurrence of element 9: 5 Last occurrence of element 17: 10 Last occurrence of element 91: 7 Last occurrence of element 88: 1
Comments
Post a Comment