First simple rule: never use the String(String) constructor, it is absolutely useless (*).

So arr.add("ss") is just fine.

With 3 it's slightly different: 3 is an int literal, which is not an object. Only objects can be put into a List. So the int will need to be converted into an Integer object. In most cases that will be done automagically for you (that process is called autoboxing). It effectively does the same thing as Integer.valueOf(3) which can (and will) avoid creating a new Integer instance in some cases.

So actually writing arr.add(3) is usually a better idea than using arr.add(new Integer(3)), because it can avoid creating a new Integer object and instead reuse and existing one.

Disclaimer: I am focusing on the difference between the second and third code blocks here and pretty much ignoring the generics part. For more information on the generics, please check out the other answers.

(*) there are some obscure corner cases where it is useful, but once you approach those you'll know never to take absolute statements as absolutes ;-)

Answer from Joachim Sauer on Stack Overflow
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ ArrayList.html
ArrayList (Java Platform SE 8 )
October 20, 2025 - The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation. Each ArrayList instance has a capacity.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_add.asp
Java ArrayList add() Method
The add() method adds an item to the list. If an index is provided then the new item will be placed at the specified index, pushing all of the following elements in the list ahead by one.
Discussions

java - Adding values to Arraylist - Stack Overflow
Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... ArrayList... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Is there a way to add elements to an Array List all at once?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/javahelp
6
1
November 2, 2021
java - Add object to ArrayList at specified index - Stack Overflow
I think it's a fairly simple question, but I can't figure out how to do this properly. I've got an empty arraylist: ArrayList list = new ArrayList (); I've got some obje... More on stackoverflow.com
๐ŸŒ stackoverflow.com
arrays - Java ArrayList how to add elements at the beginning - Stack Overflow
I need to add elements to an ArrayList queue whatever, but when I call the function to add an element, I want it to add the element at the beginning of the array (so it has the lowest index) and if... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-util-arraylist-add-method-java
Java ArrayList add() Method with Examples - GeeksforGeeks
December 10, 2024 - This method inserts the specified element at a given position in the ArrayList. It shifts the current element at that position and subsequent elements to the right. ... Exception: Throws IndexOutOfBoundsException if the specified index is out ...
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arraylist.asp
Java ArrayList
An ArrayList keeps elements in the same order you add them, so the first item you add will be at index 0, the next at index 1, and so on.
Find elsewhere
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ java/util โ€บ java arraylist add() method with index
Java ArrayList add() Method with Index
September 1, 2008 - The java.util.ArrayList.add(int index, E elemen) method inserts the specified element E at the specified position in this list.It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to ...
๐ŸŒ
Vultr
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ util โ€บ ArrayList โ€บ add
Java ArrayList add() - Add Element | Vultr Docs
November 29, 2024 - The add() method of the Java ArrayList class is essential for dynamically adjusting the contents of lists during runtime. By adding elements either at the end of the list or at specific indices, you accommodate diverse programming needs.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ List.html
List (Java Platform SE 8 )
October 20, 2025 - The List interface provides a special iterator, called a ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides.
Top answer
1 of 16
431

List has the method add(int, E), so you can use:

list.add(0, yourObject);

Afterwards you can delete the last element with:

if(list.size() > 10)
    list.remove(list.size() - 1);

However, you might want to rethink your requirements or use a different data structure, like a Queue

EDIT

Maybe have a look at Apache's CircularFifoQueue:

CircularFifoQueue is a first-in first-out queue with a fixed size that replaces its oldest element if full.

Just initialize it with you maximum size:

CircularFifoQueue queue = new CircularFifoQueue(10);
2 of 16
41

Using Specific Datastructures

There are various data structures which are optimized for adding elements at the first index. Mind though, that if you convert your collection to one of these, the conversation will probably need a time and space complexity of O(n)

Deque

The JDK includes the Deque structure which offers methods like addFirst(e) and offerFirst(e)

Deque<String> deque = new LinkedList<>();
deque.add("two");
deque.add("one");
deque.addFirst("three");
//prints "three", "two", "one"

Analysis

Space and time complexity of insertion is with LinkedList constant (O(1)). See the Big-O cheatsheet.

Reversing the List

A very easy but inefficient method is to use reverse:

 Collections.reverse(list);
 list.add(elementForTop);
 Collections.reverse(list);

If you use Java 8 streams, this answer might interest you.

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Looking at the JDK implementation this has a O(n) time complexity so only suitable for very small lists.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-add-element-in-java-arraylist
How to Add Element in Java ArrayList? - GeeksforGeeks
July 23, 2025 - Element can be added in Java ArrayList using add() method of java.util.ArrayList class.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 67655905 โ€บ if-i-have-an-empty-arraylist-can-i-add-an-element-in-any-position-or-do-i-have
java - If i have an empty arraylist, can i add an element in any position or do i have to start off with the 0 index? - Stack Overflow
So I guess it is not correct to say that you can insert at any point. Let's say you have array of size n. add will allow you to insert at any point from 0 to n. n+1 and so on will through an exception. ... No, it's not possible in the way you try to do it. When declaring an ArrayList, you only create an object of an ArrayList.
๐ŸŒ
QBasic on Your Computer
chortle.ccsu.edu โ€บ javaLessons โ€บ chap85 โ€บ ch85_11.html
Adding Elements to an ArrayList
boolean add( E elt ) ; ... This method returns a reference to an object in the list, which is of type E. Here is our example program, where type E is String. import java.util.* ; public class ArrayListEgTwo { public static void main ( String[] args) { // Create an ArrayList that holds references ...
๐ŸŒ
Codekru
codekru.com โ€บ home โ€บ arraylist add() method in java
ArrayList add() method in Java - Codekru
August 12, 2022 - add() method is used to add the elements into the ArrayList. There are two overloaded implementations of the add method for the ArrayList class.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ java-arraylist-addint-index-e-element-example
Java ArrayList add(int index, E element) example
August 21, 2014 - This method adds the element at the given index. package beginnersbook.com; import java.util.ArrayList; public class AddMethodExample { public static void main(String[] args) { // ArrayList of String type ArrayList<String> al = new ArrayList<String>(); // simple add() methods for adding elements at the end al.add("Hi"); al.add("hello"); al.add("String"); al.add("Test"); //adding element to the 4th position //4th position = 3 index as index starts with 0 al.add(3,"Howdy"); System.out.println("Elements after adding string Howdy:"+ al); //adding string to 1st position al.add(0, "Bye"); //Print System.out.println("Elements after adding string bye:"+ al); } }
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ add multiple items to an java arraylist
Add Multiple Items to an Java ArrayList | Baeldung
January 8, 2024 - One of them is addAll, which needs a destination list and the items to be added may be specified individually or as an array. Here itโ€™s an example of how to use it with individual elements: List<Integer> list = new ArrayList<>(); Collections.addAll(list, 1, 2, 3, 4, 5);