Access and change item in Java ArrayList
By using the get() method we can access an element in the ArrayList, which is assigned to the index number:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> mobiles = new ArrayList<String>();
mobiles.add("Samsung");
mobiles.add("Apple");
mobiles.add("Nokia");
mobiles.add("Sony");
System.out.println(mobiles.get(0));
}
}
Change an Item in Java ArrayList
To reorganize an element, use the set() method and refer to the index number:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> mobiles = new ArrayList<String>();
mobiles.add("Samsung");
mobiles.add("Apple");
mobiles.add("Nokia");
mobiles.add("Sony");
mobiles.set(0, "LG");
System.out.println(mobiles);
}
}
Keywords: