1import java.util.ArrayList;
2import java.util.List;
3public class ArrayListSubListMethodExample
4{
5 public static void main(String[] args)
6 {
7 try
8 {
9 ArrayList<String> al = new ArrayList<String>();
10 al.add("orange");
11 al.add("apple");
12 al.add("strawberry");
13 al.add("banana");
14 al.add("mango");
15 System.out.println("Given ArrayList: " + al);
16 // get subList using subList() method
17 System.out.println("End point indices are out of order (fromIndex > toIndex): ");
18 List<String> li = al.subList(6, 2);
19 // printing subList
20 System.out.println("Sublist of ArrayList: " + li);
21 }
22 catch(IndexOutOfBoundsException e)
23 {
24 System.out.println("Exception: " + e);
25 }
26 catch(IllegalArgumentException ex)
27 {
28 System.out.println("Exception: " + ex);
29 }
30 }
31}
1import java.util.ArrayList;
2import java.util.List;
3public class ArrayListSubListMethodExample
4{
5 public static void main(String[] args)
6 {
7 try
8 {
9 ArrayList<String> al = new ArrayList<String>();
10 al.add("orange");
11 al.add("apple");
12 al.add("strawberry");
13 al.add("banana");
14 al.add("mango");
15 System.out.println("Given ArrayList: " + al);
16 // get subList using subList() method
17 List<String> li = al.subList(2, 4);
18 // printing subList
19 System.out.println("Sublist of ArrayList: " + li);
20 }
21 catch(IndexOutOfBoundsException e)
22 {
23 System.out.println("Exception: " + e);
24 }
25 catch(IllegalArgumentException ex)
26 {
27 System.out.println("Exception: " + ex);
28 }
29 }
30}
1import java.util.ArrayList;
2import java.util.List;
3public class ArrayListSubListMethodExample
4{
5 public static void main(String[] args)
6 {
7 try
8 {
9 ArrayList<String> al = new ArrayList<String>();
10 al.add("orange");
11 al.add("apple");
12 al.add("strawberry");
13 al.add("banana");
14 al.add("mango");
15 System.out.println("Given ArrayList: " + al);
16 // get subList using subList() method
17 System.out.println("End index value is out of range: ");
18 List<String> li = al.subList(2, 6);
19 // printing subList
20 System.out.println("Sublist of ArrayList: " + li);
21 }
22 catch(IndexOutOfBoundsException e)
23 {
24 System.out.println("Exception: " + e);
25 }
26 catch(IllegalArgumentException ex)
27 {
28 System.out.println("Exception: " + ex);
29 }
30 }
31}