🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
ArrayList.subList(int fromIndex, int toIndex) method in Java is used to obtain a view of a portion of the ArrayList. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
subListMethod Syntax- Examples
- Getting a SubList
- Modifying the SubList
- Handling IndexOutOfBoundsException
- Conclusion
Introduction
The ArrayList.subList(int fromIndex, int toIndex) method is a member of the ArrayList class in Java. It allows you to obtain a view of a specified range within the ArrayList. The view is backed by the original list, so changes to the sublist are reflected in the original list and vice versa.
subList Method Syntax
The syntax for the subList method is as follows:
public List<E> subList(int fromIndex, int toIndex)
- fromIndex: The low endpoint (inclusive) of the sublist.
- toIndex: The high endpoint (exclusive) of the sublist.
The method returns a view of the specified range within the ArrayList.
Examples
Getting a SubList
You can use the subList method to obtain a sublist of a specified range from the ArrayList.
Example
import java.util.ArrayList;
import java.util.List;
public class SubListExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
list.add("Grapes");
list.add("Pineapple");
// Get a sublist from index 1 (inclusive) to index 4 (exclusive)
List<String> subList = list.subList(1, 4);
System.out.println("Original ArrayList: " + list);
System.out.println("SubList: " + subList);
}
}
Output:
Original ArrayList: [Apple, Banana, Orange, Grapes, Pineapple]
SubList: [Banana, Orange, Grapes]
Modifying the SubList
Changes made to the sublist are reflected in the original ArrayList and vice versa.
Example
import java.util.ArrayList;
import java.util.List;
public class SubListModificationExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
list.add("Grapes");
list.add("Pineapple");
// Get a sublist from index 1 (inclusive) to index 4 (exclusive)
List<String> subList = list.subList(1, 4);
System.out.println("Original ArrayList before modification: " + list);
System.out.println("SubList before modification: " + subList);
// Modify the sublist
subList.set(1, "Blueberry");
System.out.println("Original ArrayList after modification: " + list);
System.out.println("SubList after modification: " + subList);
}
}
Output:
Original ArrayList before modification: [Apple, Banana, Orange, Grapes, Pineapple]
SubList before modification: [Banana, Orange, Grapes]
Original ArrayList after modification: [Apple, Banana, Blueberry, Grapes, Pineapple]
SubList after modification: [Banana, Blueberry, Grapes]
Handling IndexOutOfBoundsException
Attempting to create a sublist with invalid indices (e.g., fromIndex is greater than toIndex, or either index is out of bounds) will throw an IndexOutOfBoundsException.
Example
import java.util.ArrayList;
import java.util.List;
public class SubListInvalidExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Attempt to create a sublist with invalid indices
try {
List<String> subList = list.subList(2, 1);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: " + e.getMessage());
}
// Attempt to create a sublist with out of bounds indices
try {
List<String> subList = list.subList(1, 4);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Error: fromIndex(2) > toIndex(1)
Error: toIndex = 4
Conclusion
The ArrayList.subList(int fromIndex, int toIndex) method in Java provides a convenient way to obtain a view of a portion of an ArrayList. By understanding how to use this method, you can efficiently work with subsets of your lists in Java applications. It's important to handle potential IndexOutOfBoundsException by ensuring that the specified indices are valid before attempting to create a sublist.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment