Arraylist sublist java: In the previous article we have discussed about Java ArrayList set() Method with Example
In this article we are going to see the use Java ArrayList subList() method along with suitable examples.
Java ArrayList subList() Method with Example
subList():
Java arraylist sublist: This java.util.ArrayList.subList()
method is used to extracts a part of the arraylist. It returns all the elements of the specified range within the arrayList.
Example:
Arraylist sublist: If there is subList(2,7) then it returns all the elements from index 2 to index 6 (7-1)
If the fromIndex to toIndex is greater than or less than the size of the arrayList then it shows IndexOutOfBoundException.
Syntax:
arrayListName.subList(int fromIndex, int toIndex)
Where,
arrayListName
refers to the name of your ArrayList.fromIndex
refers to the start index of the arrayList for sub ArrayList.toIndex
refers to the end index of the arrayList sub ArrayList.
Let’s see different examples to understand it more clearly.
Method-1: Java ArrayList subList() Method – Example with String Type ArrayList
Approach:
- Create a new ArrayList of type String.
- Add string elements into the ArrayList using the add() method.
- Display the ArrayList elements
- Then create new elements from the arrayList using
subList()
method - Print the new ArrayList.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create an ArrayList of string datatype ArrayList<String> arr = new ArrayList<String>(); // Adding some elements to the ArrayList arr.add("apple"); arr.add("banana"); arr.add("orange"); arr.add("grapes"); arr.add("guava"); // Prints the ArrayList elements System.out.println("The elements of ArrayList are: "+arr); // Prints a new sub list of the previous ArrayList System.out.println("The elements of subList are: "+arr.subList(2, 4)); } }
Output: The elements of ArrayList are: [apple, banana, orange, grapes, guava] The elements of subList are: [orange, grapes]
Method-2: Java ArrayList subList() Method – Example with Integer Type ArrayList
Approach:
- Create a new ArrayList of type String.
- Add Integer elements into the ArrayList using the add() method.
- Display the ArrayList elements
- Then create new elements from the arrayList using
subList()
method - Print the new ArrayList.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create an ArrayList of Integer datatype ArrayList<Integer> arr = new ArrayList<Integer>(); // Adding some elements to the ArrayList arr.add(2); arr.add(12); arr.add(22); arr.add(32); arr.add(42); // Prints the ArrayList elements System.out.println("The elements of ArrayList are: "+arr); // Prints a new sub list of the previous ArrayList System.out.println("The elements of subList are: "+arr.subList(2, 4)); } }
Output: The elements of ArrayList are: [2, 12, 22, 32, 42] The elements of subList are: [22, 32]
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Related Java Programs: