How to append elements at the end of ArrayList in Java?
append, arraylist, java
Solution
Here is the syntax, along with some other methods you might find useful:
//add to the end of the list
stringList.add(random);
//add to the beginning of the list
stringList.add(0, random);
//replace the element at index 4 with random
stringList.set(4, random);
//remove the element at index 5
stringList.remove(5);
//remove all elements from the list
stringList.clear();
Problem
I am wondering, how do I append an element to the end of an ArrayList in Java? Here is the code I have so far: ``` public class Stack { private ArrayList<String> stringList = new ArrayList<String>(); RandomStringGenerator rsg = new RandomStringGenerator(); private void push(){ String random = rsg.randomStringGenerator(); ArrayList.add(random); } } ``` `randomStringGenerator` is a method which generates a random String. I basically want to always append the random string at the end of the ArrayList, much like a stack (Hence the name "push").