/**
* @author lautturi.com
* Java example: add an element to arrayList in java
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
// create ArrayList
ArrayList<String> strs = new ArrayList<>();
// add() method without the index parameter
strs.add("Java");
strs.add("Python");
strs.add("Lautturi");
// add element at index 0
strs.add(0, "Lautturi.com");
// add element at index 3
strs.add(3, "world");
System.out.println("ArrayList: " + strs);
}
}
Output:
ArrayList: [Lautturi.com, Java, Python, world, Lautturi]