array list add in java code example

Example 1: insert element into arraylist java

ArrayList<String> arrayList = new ArrayList<>();
// Adds element at the back of the list
arrayList.add("foo");
arrayList.add("bar");
// Result = ["foo", "bar"]

// Adds element at the specified index
arrayList.add(1, "baz");
// Result = ["foo", "baz", "bar"]

Example 2: arraylist add method

public void add(int index, E element)	// no return value

Example 3: append to arraylist by index java

arrlist.add(2,25);
// .add(index, content)