subList (int fromIndex,� int toIndex) method of List Interface in java.

subList (int fromIndex,� int toIndex) method of List Interface in java.


Posted in : Core Java Posted on : January 13, 2011 at 8:29 PM Comments : [ 0 ]

In this section we will discuss how can we find sub elements of a list using subList (int fromIndex, int toIndex) method of list interface in java.

subList (int fromIndex, int toIndex) method of List Interface in java.

In this section we will discuss how can we find sub elements of a list using subList (int fromIndex, int toIndex) method of list interface in java.

Syntax

public List subList (int fromIndex, int toIndex)

This method returns a list which contains sub part of current list.

In this method you can find the sub elements of a list. This method returns the elements between the specified indexes of a list. i.e. elements between the start and last index of a list. Start index is the position from where you want to find the value and the last index is the position at which place you want to find the value. A sub list includes the start index value but it does  not include last index value i.e. start index is inclusive but last index is exclusive from the list. Any operation that we perform in a list can be performed on subList. Any modification we will do in sublist the list will also be effected. 

Parameter Description

int fromIndex : It takes an argument as integer value for subList from which index position do you want to start a list. This index position value is included in list. 

int toIndex :  It takes an argument as integer value for subList at which index position do you want to end a list. This index position value is excluded from list i.e. it prints the just before value of last index position.

Example of subList (int fromIndex, int toIndex) method

In this example we will show you how does subList (int fromIndex, int toIndex) method works in List interface. This example will help you to understand how can you find sub elements of a  list. Through this example we will show you adding of element to the list, show the sublist of a list between the specified index as between fromIndex--toIndex, size of list /sublist, removing the elements from sublist, also we will show after removing element from sublist how original list will be get effected.

Example:-


package Devmanuals.com;
import java.util.List;
import java.util.ArrayList;
public class ListSubList {
public static void main (String args []){
int size; List ls;ArrayList al=new ArrayList();
al.add("Rose");
al.add("India");
al.add("Networks");
al.add("Dev");
al.add(".com");
al.add("Network");
if (al.isEmpty()==false) {
System.out.println("List is : "+al);
size= al.size();
System.out.println("Size of list : "+size);
ls= al.subList(1, 4);
System.out.println("Sub list is : "+ls);
ls.remove(0);
System.out.println("After removing element from sublist list = "+ls);
System.out.println("Size of sub list : "+ls.size());
System.out.println("Now List is : "+al);
al.set(1, "India and");
al.set(3, "Manuals");
System.out.println("After modifying in sublist then new list = "+al);
System.out.println("Size of list : "+al.size());
}
else
System.out.println("List is empty");
}
}

Output :

List is : [Rose, India, Networks, Dev, .com, Network]

Size of list : 6

Sub list is : [India, Networks, Dev]

After removing element from sub list : [Networks, Dev]

Size of sub list : 2

Now List is : [Rose, Networks, Dev, .com, Network]

After modifying in sublist then new list = [Rose, India and, Dev, Manuals, Network]

Size of list : 5

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics