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]
Storing Java Object In ArrayList
How to count number of element in ArrayList
How to insert element in arraylist
How to use retailAll in 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:
How to count number of element in ArrayList
How to insert element in arraylist
How to use retailAll in arraylist
No comments:
Post a Comment