How to Get Substring Items Within Arraylist in Java? Last Updated : 02 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, ArrayList is the pre-defined class of the Java collection framework, and it is part of java.util package. ArrayList can be used to add or remove an element dynamically in a Java program. It can be dynamically based on the elements added or removed from the ArrayList. In this article, we will be discussing how to get substring items within ArrayList in Java. Methods to get the substrings items within ArrayList Two methods are mostly used to implement to get the substrings items within ArrayList in the Java program. add(): It is a pre-defined method of the ArrayList that can be used to add the elements to the ArrayList.substring(startIndex, endIndex): It is also an in-built method, and it can be used to generate the substring with the specified starting index and ending index parameter of the method.Java Program to Get the Substrings Items within ArrayList Java // Java Program to Get Substrings from an ArrayList of Strings import java.util.ArrayList; public class GFGSubstringArrayList { // Main method public static void main(String[] args) { // Create the ArrayList of Strings named as mainList ArrayList<String> mainList = new ArrayList<>(); // Adding values to mainList mainList.add("Programming"); mainList.add("Java"); mainList.add("Spring"); mainList.add("SpringBoot"); // Define the startIndex and endIndex of the substring int startIndex = 3; int endIndex = 5; // Get substrings from the ArrayList ArrayList<String> substringList = getSubstrings(mainList, startIndex, endIndex); // Print the result System.out.println("Original ArrayList: " + mainList); System.out.println("Substrings ArrayList: " + substringList); } // Method to get substrings from an ArrayList of Strings private static ArrayList<String> getSubstrings(ArrayList<String> list, int startIndex, int endIndex) { ArrayList<String> substringList = new ArrayList<>(); for (String str : list) { // Check if the string is long enough for the specified indices if (str.length() >= endIndex) { substringList.add(str.substring(startIndex, endIndex)); } else { // Handle the case where the string is shorter than the specified indices substringList.add("No SubString"); } } return substringList; } } OutputOriginal ArrayList: [Programming, Java, Spring, SpringBoot] Substrings ArrayList: [gr, No SubString, in, in] Explanation:In the above program, we are getting the substring item within the ArrayList. We have created the ArrayList and added the elements into the ArrayList.After that defined one more method to convert the substrings with the specified starting index and ending of the parameters of the substring in the program. Comment More infoAdvertise with us Next Article How to Get Substring Items Within Arraylist in Java? K kadambalamatclo Follow Improve Article Tags : Java Java Programs Java-ArrayList substring Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Like