23. List subList(int fromIndex, int toIndex)

23. List subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified fromIndex,
inclusive, and toIndex, exclusive.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.spy.arraylist.method;
 
import java.util.ArrayList;
import java.util.List;
 
public class Sublist
{
 
	public static void main(String[] args) {
 
 
		ArrayList<String> list=new ArrayList<String>();
 
 
		//Adding element into Arraylist
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek");
 
			list.add("milan");
 
 
		  List<String>  sublist= list.subList(1, 3);
 
		  System.out.println("Retrieving sub list");
		  for (String s : sublist) 
		  {
 
			  System.out.println(s);
 
		   }
 
 
 
		}
}

Output:

1
2
3
4
 
Retrieving sub list
Abhi
vivek

Leave a Reply

Your email address will not be published. Required fields are marked *