Java ArrayList subList() Method |
-- Learning not just technology, but dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
Java Tutorial
- Java Tutorial
- Java Introduction
- Java Development Environment Setup
- Java Basic Syntax
- Java Comments
- Java Objects and Classes
- Java Basic Data Types
- Java Variable Types
- Java Variable Naming Rules
- Java Modifier Types
- Java Operators
- Java Loop Structures β for, while and doβ¦while
- Java Conditional Statements β ifβ¦else
- Java switch case Statement
- Java Number & Math Class
- Java Character Class
- Java String Class
- Java StringBuffer and StringBuilder Class
- Java Arrays
- Java Date and Time
- Java Regular Expressions
- Java Methods
- Java Constructors
- Java Stream, File and IO
- Java Scanner Class
- Java Exception Handling
Java Object-Oriented Programming
- Java Inheritance
- Java Override/Overload
- Java Polymorphism
- Java Abstract Class
- Java Encapsulation
- Java Interfaces
- Java Enums
- Java Packages
- Java Reflection
Java Advanced Tutorial
- Java Data Structures
- Java Collections Framework
- Java ArrayList
- Java LinkedList
- Java HashSet
- Java HashMap
- Java Iterator
- Java Object
- Java NIO Files
- Java Generics
- Java Serialization
- Java Networking
- Java Sending Email
- Java Multithreading
- Java Applet Basics
- Java Documentation Comments
- Java Examples
- Java 8 New Features
- Java MySQL Connection
- Java 9 New Features
- Java Quiz
- Java Common Libraries
Java ArrayList subList() Method
The subList() method is used to extract and return a part of the ArrayList.
The syntax of the subList() method is:
arraylist.subList(int fromIndex, int toIndex)
Note: arraylist is an object of the ArrayList class.
Parameter Description:
- fromIndex - The starting position of the elements to be extracted, including the element at this index position.
- toIndex - The ending position of the elements to be extracted, excluding the element at this index position.
Return Value
Returns the extracted part of the given ArrayList.
If fromIndex is less than 0 or greater than the length of the array, an IndexOutOfBoundsException is thrown.
If the value of fromIndex is greater than toIndex, an IllegalArgumentException is thrown.
Note: The ArrayList contains elements starting from the fromIndex position until the element index position is toIndex-1, and the element at index position toIndex is not included.
Example
Extract a sublist from an ArrayList:
Example
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// Create an ArrayList
ArrayList<String> sites = new ArrayList<>();
sites.add("Google");
sites.add("");
sites.add("Taobao");
sites.add("Wiki");
System.out.println("Website List: " + sites);
// Elements from position 1 to 3
System.out.println("SubList: " + sites.subList(1, 3));
}
}
Output of the above program:
Website List: [Google, , Taobao, Wiki]
SubList: [, Taobao]
In the example above, we used the subList() method to get elements from index 1 to 3 (excluding 3).
Split an ArrayList into two ArrayLists:
Example
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// Create an array
ArrayList<Integer> ages = new ArrayList<>();
// Add some elements to the array
ages.add(10);
ages.add(12);
ages.add(15);
ages.add(19);
ages.add(23);
ages.add(34);
System.out.println("Age List: " + ages);
// Under 18
System.out.println("Under 18: " + ages.subList(0, 3));
// Over 18
System.out.println("Over 18: " + ages.subList(3, ages.size()));
}
}
Output of the above program:
Age List: [10, 12, 15, 19, 23, 34]
Under 18: [10, 12, 15]
Over 18: [19, 23, 34]
In the example above, we created an array named ages. The subList() method split this array into two arrays: ages under 18 and ages over 18.
Please note that we used the ages.size() method to get the length of the array. To learn more about the size() method, visit Java ArrayList size().
YouTip