slice a list java
new ArrayList(input.subList(0, input.size()/2))
slice a list java
new ArrayList(input.subList(0, input.size()/2))
ArrayList subList(int fromIndex, int toIndex) method in java
import java.util.ArrayList;
import java.util.List;
public class ArrayListSubListMethodExample
{
public static void main(String[] args)
{
try
{
ArrayList<String> al = new ArrayList<String>();
al.add("orange");
al.add("apple");
al.add("strawberry");
al.add("banana");
al.add("mango");
System.out.println("Given ArrayList: " + al);
// get subList using subList() method
System.out.println("End index value is out of range: ");
List<String> li = al.subList(2, 6);
// printing subList
System.out.println("Sublist of ArrayList: " + li);
}
catch(IndexOutOfBoundsException e)
{
System.out.println("Exception: " + e);
}
catch(IllegalArgumentException ex)
{
System.out.println("Exception: " + ex);
}
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us