1import java.util.ArrayList;
2public class ArrayListGetExample
3{
4 public static void main(String[] args)
5 {
6 ArrayList<String> al = new ArrayList<String>();
7 al.add("lion");
8 al.add("tiger");
9 al.add("deer");
10 al.add("cheetah");
11 al.add("elephant");
12 al.add("fox");
13 al.add("hyenas");
14 al.add("jackal");
15 System.out.println("Get second element of ArrayList: " + al.get(1));
16 System.out.println("Get third element of ArrayList: " + al.get(2));
17 System.out.println("Get fifth element of ArrayList: " + al.get(4));
18 System.out.println("Get sixth element of ArrayList: " + al.get(5));
19 }
20}
1import java.util.ArrayList;
2import java.util.Arrays;
3
4public class ArrayListExample
5{
6 public static void main(String[] args)
7 {
8 ArrayList<String> list = new ArrayList<>(Arrays.asList("alex", "brian", "charles", "dough"));
9
10 String firstName = list.get(0); //alex
11 String secondName = list.get(1); //brian
12
13 System.out.println(firstName);
14 System.out.println(secondName);
15 }
16}
17