A List is an ordered sequence of elements stored together to form a collection. A list can contain duplicate as well as null entries. A list allows us to perform index-based operations, that is additions, deletions, manipulations, and positional access. Java provides an in-built interface <<java.util>> to perform list as well as other class-based functions.
Methods:
- The naive approach by maintaining the start and end index.
- Using subList() method.
Method 1
- Elements are accessed at the required indexes of the list.
- Add them to a new empty list created.
- The start and end index values are used to access the part of the list required. By default, the indexes are assumed to start at 0.
This approach requires an additional space of maintaining a new list to store the sublist desired.
Example
Java
import java.util.*;
import java.io.*;
import java.util.Iterator;
public class GFG {
public static void main(String[] args)
{
List<Integer> list1 = new ArrayList<Integer>();
list1.add( 1 );
list1.add( 7 );
list1.add( 8 );
list1.add( 2 );
list1.add( 11 );
list1.add( 3 );
list1.add( 66 );
list1.add( 30 );
System.out.println( "The original list contents : " );
Iterator iterator = list1.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " " );
}
List<Integer> new_list = new ArrayList<Integer>();
int i = 0 ;
int start_indx = 2 , end_indx = 5 ;
while (i < list1.size()) {
if (i >= start_indx && i <= end_indx) {
new_list.add(list1.get(i));
}
i++;
}
System.out.println();
System.out.println( "The sublist contents : " );
iterator = new_list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " " );
}
}
}
|
Output
The original list contents :
1 7 8 2 11 3 66 30
The sublist contents :
8 2 11 3
Time Complexity: O(n)
Space Complexity: O(n) where n is the size of the list.
Method 2: Java provides us with an in-built method sublist() to access the elements belonging to the specified range of index values. The method is provided by the ArrayList package.
Syntax :
public List subList(int fromIndex, int toIndex)
Parameters:
- fromIndex = start index
- toIndex = endIndex
Return Type: A list of the required elements. The method access all the elements in the range fromIndex to toIndex-1. If fromIndex is equal to toIndex, an empty list is returned.
Example:
Java
import java.util.*;
import java.util.Iterator;
import java.io.*;
class AccessSublist {
public static void main(String[] args)
{
List<String> list1 = new ArrayList<String>();
list1.add( "Are" );
list1.add( "you" );
list1.add( "working!" );
list1.add( "hard" );
list1.add( "Geeeks?" );
System.out.print( "The original list contents : " );
Iterator iterator = list1.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " " );
}
List<String> new_list = list1.subList( 0 , 3 );
System.out.println();
System.out.print( "The sublist contents : " );
iterator = new_list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " " );
}
}
}
|
Output
The original list contents : Are you working! hard Geeeks?
The sublist contents : Are you working!
Time Complexity: O(n) when n is no of elements in the list
Auxiliary Space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
12 Sep, 2022
Like Article
Save Article