Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
by (7k points)

How do I append an element to the end of an ArrayList in Java? Here is my code:

public class Stack {

    private ArrayList<String> stringList = new ArrayList<String>();

    RandomStringGenerator rsg = new RandomStringGenerator();

    private void push(){

        String random = rsg.randomStringGenerator();




“randomStringGenerator” is a method that generates a random string at the end of the ArrayList, much like a stack.

1 Answer

0 votes
by (13.1k points)

Here is the syntax, along with some other methods you might find useful:

   //add to the end of the list


    //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


    //remove all elements from the list


Want to learn java? Check out the core Java certification from Intellipaat. 

Browse Categories