How to get sub list from ArrayList

we can use sublist(fromindex,toindex) for get sublist from a arraylist.

import java.util.ArrayList;
import java.util.List;

public class GetSubList {

public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
// add element in arraylist
arr.add(“c”);
arr.add(“php”);
arr.add(“html”);
arr.add(“java”);
arr.add(“mysql”);
// print arraylist
System.out.println(“arrayList is = ” + arr);

/*
* Returns a view of the portion of this list between the specified
* fromIndex, inclusive, and toIndex, exclusive
*/

List<String> list=arr.subList(2,4);
System.out.println(“sub list is=”+list);

}

}

Output:

arrayList is = [c, php, html, java, mysql]

sub list is=[html, java]

 

Related Posts: 

 

Storing Java Object In ArrayList

 

Leave a Reply

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

− 1 = 2