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;
2public class ArrayListSetMethodExample
3{
4 public static void main(String[] args)
5 {
6 ArrayList<String> names = new ArrayList<String>(5);
7 names.add("vinay");
8 names.add("ajay");
9 names.add("vijay");
10 names.add("bharat");
11 names.add("dinesh");
12 System.out.println("ArrayList before using set() method: " + names);
13 // change vijay name to chandan
14 System.out.println("ArrayList after using set() method: ");
15 names.set(2, new String("chandan"));
16 for(int a = 0; a < 5; a++)
17 {
18 System.out.println(names.get(a).toString());
19 }
20 }
21}
1List<String> cities = new ArrayList<String>();
2cities.add("Tuguegarao City");
3cities.add("Tabuk City");
4cities.add("Davao City");
5
6System.out.println(cities.get(1)); //prints "Tabuk City"
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