java arraylist get object at index code example
Example 1: ArrayList indexOf(Object o) method in java
import java.util.ArrayList;
public class ArrayListIndexOfMethodExample
{
public static void main(String[] args)
{
ArrayList<Integer> al = new ArrayList<Integer>(5);
al.add(8);
al.add(6);
al.add(5);
al.add(7);
al.add(9);
System.out.print("ArrayList values are: ");
for(Integer num : al)
{
System.out.print(num);
System.out.print(" ");
}
int position = al.indexOf(5);
System.out.println("\nElement 5 is at index: " + position);
}
}
Example 2: java arraylist access index
List<String> cities = new ArrayList<String>();
cities.add("Tuguegarao City");
cities.add("Tabuk City");
cities.add("Davao City");
System.out.println(cities.get(1));
Example 3: how to select a element in an arraylist java
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListExample
{
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<>(Arrays.asList("alex", "brian", "charles", "dough"));
String firstName = list.get(0);
String secondName = list.get(1);
System.out.println(firstName);
System.out.println(secondName);
}
}
Example 4: arraylist get value at index java
get(index);