Java ArrayList sublist


 

Java ArrayList sublist

This section demonstrates how to use the Java Arraylist sublist

This section demonstrates how to use the Java Arraylist sublist

  • It returns the part of the ArrayList as List reference.
  • part is the given range between two indexes.


Java Arraylist Sublist Example
import java.util.*;

public class List1 {

public static void main(String[] args) {
String  ar[]={"india","pakistan","United Kingdom","Japan","Korea"};
ArrayList list=new ArrayList();
list.add(ar[0]);
list.add(ar[1]);
list.add(ar[2]);
list.add(ar[3]);
List list1=list.subList(1,3);

System.out.println(list+"   ");
System.out.println(list1+"   ");
}
}
Output
[india, pakistan, United Kingdom, Japan] 
[pakistan, United Kingdom]


Ads