No you can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size. When it does you'll have to allocate a new one and copy the data from the old to the new:

int[] oldItems = new int[10];
for (int i = 0; i < 10; i++) {
    oldItems[i] = i + 10;
}
int[] newItems = new int[20];
System.arraycopy(oldItems, 0, newItems, 0, 10);
oldItems = newItems;

If you find yourself in this situation, I'd highly recommend using the Java Collections instead. In particular ArrayList essentially wraps an array and takes care of the logic for growing the array as required:

List<XClass> myclass = new ArrayList<XClass>();
myclass.add(new XClass());
myclass.add(new XClass());

Generally an ArrayList is a preferable solution to an array anyway for several reasons. For one thing, arrays are mutable. If you have a class that does this:

class Myclass {
    private int[] items;

    public int[] getItems() {
        return items;
    }
}

you've created a problem as a caller can change your private data member, which leads to all sorts of defensive copying. Compare this to the List version:

class Myclass {
    private List<Integer> items;

    public List<Integer> getItems() {
        return Collections.unmodifiableList(items);
    }
}
Answer from cletus on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ creating-a-dynamic-array-in-java
Dynamic Array in Java - GeeksforGeeks
November 13, 2024 - Arrays are linear data structures, and similar types of elements will be inserted in continuous memory locations. Now as we know there is an issue with arrays that size needs to be specified at the time of declaration or taken from the user in Java. Hence, there arise dynamic arrays in Java in which entries can be added as the array increases its size as it is full.
Top answer
1 of 16
192

No you can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size. When it does you'll have to allocate a new one and copy the data from the old to the new:

int[] oldItems = new int[10];
for (int i = 0; i < 10; i++) {
    oldItems[i] = i + 10;
}
int[] newItems = new int[20];
System.arraycopy(oldItems, 0, newItems, 0, 10);
oldItems = newItems;

If you find yourself in this situation, I'd highly recommend using the Java Collections instead. In particular ArrayList essentially wraps an array and takes care of the logic for growing the array as required:

List<XClass> myclass = new ArrayList<XClass>();
myclass.add(new XClass());
myclass.add(new XClass());

Generally an ArrayList is a preferable solution to an array anyway for several reasons. For one thing, arrays are mutable. If you have a class that does this:

class Myclass {
    private int[] items;

    public int[] getItems() {
        return items;
    }
}

you've created a problem as a caller can change your private data member, which leads to all sorts of defensive copying. Compare this to the List version:

class Myclass {
    private List<Integer> items;

    public List<Integer> getItems() {
        return Collections.unmodifiableList(items);
    }
}
2 of 16
32

In java array length is fixed.

You can use a List to hold the values and invoke the toArray method if needed See the following sample:

import java.util.List;
import java.util.ArrayList;
import java.util.Random;

public class A  {

    public static void main( String [] args ) {
        // dynamically hold the instances
        List<xClass> list = new ArrayList<xClass>();

        // fill it with a random number between 0 and 100
        int elements = new Random().nextInt(100);  
        for( int i = 0 ; i < elements ; i++ ) {
            list.add( new xClass() );
        }

        // convert it to array
        xClass [] array = list.toArray( new xClass[ list.size() ] );


        System.out.println( "size of array = " + array.length );
    }
}
class xClass {}
Discussions

How can I make a dynamic array in Java? - Stack Overflow
Quick overview of our assignment: User needs to enter grades received. We do not know how many grades user needs to enter. If the user enters "-1" thats when we know the user is done entering grade... More on stackoverflow.com
๐ŸŒ stackoverflow.com
I can't wrap my head up around Dynamic Arrays
Java's ArrayList is a dynamic array. More on reddit.com
๐ŸŒ r/learnjava
9
0
August 12, 2022
Dynamic array in java - Stack Overflow
Can't i just keep it dynamic? the length is variable ยท No. Arrays lenght should be fixed while initializing it. Look into Collection's in java. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How is my implementation of a dynamic array in Java, and especially the unit tests? I know it's a bit much to ask, but a quick glance and some feedback would mean a lot!
generally looks like it makes sense! a few small-ish points many other implementations use the word capacity instead of allocatedLength in doubleMemoAllocation you calculate the new capacity twice - assign it to a variable & reuse it so you've got one source of truth and the code is easier to maintain + reason about rather than having if (count == allocatedLength) doubleMemoAllocation(); in two places i'd create a method called ensureCapacity() which contains that logic & then in your inset methods you can call it. this happens to be what the java arraylist does insert is a special case of insertAt, so you can just call it with the final index. also group these methods next to each other i may be wrong, but is insertAt missing an increment to count? your test Array keeps resizing never actually confirms that it resizes, internally you could have just set the capacity to 100 & the test would be functionally the same. other languages will have a getCapacity() method which tells you how big the array list can get, but the java stdlib one doesn't reverse could be done without allocating a new array if you swap indices using a temporary variable print should be toString and should use a StringBuilder for more efficiency than adding strings together return strRepr == "[" you are lucky if this works since == in java checks object identity. use strRepr.equals("[") instead in the case someone passes an initial capacity of 0 you could avoid creating a 0 sized array and just leave that field as null More on reddit.com
๐ŸŒ r/learnprogramming
6
5
November 8, 2022
People also ask

Can a dynamic array in Java (specifically ArrayList) effectively store null elements?
Yes, an ArrayList, as a dynamic array in Java, permits null values as valid elements. This design choice offers flexibility in representing absent data. However, consuming code must explicitly handle these null references. Failing to validate fetched elements can lead to NullPointerException issues.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ blog โ€บ software development โ€บ creating a dynamic array in java
Dynamic Array in Java: Secret Behind High-Performance Code!
Is an ArrayList a dynamic array?
Yes. Java's ArrayList is a dynamic array, and so are Python's list, C++'s vector, and C#'s List. Java's LinkedList, on the other hand, is a doubly linked list.
๐ŸŒ
interviewcake.com
interviewcake.com โ€บ interview cake โ€บ dynamic array
Dynamic Array Data Structure Explained | Interview Cake
What is a dynamic array?
A dynamic array is an array that automatically resizes itself as elements are added or removed, unlike a fixed-size static array.
๐ŸŒ
interviewcake.com
interviewcake.com โ€บ interview cake โ€บ dynamic array
Dynamic Array Data Structure Explained | Interview Cake
๐ŸŒ
Interview Cake
interviewcake.com โ€บ interview cake โ€บ dynamic array
Dynamic Array Data Structure Explained | Interview Cake
2 weeks ago - In short: A dynamic array is an array that automatically resizes as you add elements, usually by doubling its capacity and copying everything over when it fills up. That gives O(1) index lookups and O(1) amortized appends, which is why it backs ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ dynamic array in java
Dynamic Array in Java - Scaler Topics
May 4, 2023 - In order to mitigate the sizing problem of the array, dynamic arrays came into the picture. ArrayList data structure is used to implement the concept of dynamic data structures in Java. As the name suggests, dynamic arrays have a dynamic size modifiable at runtime.
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ dynamic array in java | working, uses & more (+examples)
Dynamic Array In Java | Working, Uses & More (+Examples)
January 10, 2025 - A dynamic array in Java, like ArrayList, is a resizable array that can grow or shrink as needed, unlike fixed-size arrays, offering greater flexibility.
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ dynamic-array-java
What is Dynamic Array in Java? | How do they Work? | Edureka
June 17, 2021 - The dynamic array in Java is a type of an array with a huge improvement for automatic resizing. The only limitation of arrays is that it is a fixed size.
Find elsewhere
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ java tutorial โ€บ dynamic array in java
Dynamic Array in Java | How does Dynamic Array work in Java?
May 13, 2024 - Dynamic Array in Java means either stretched or shrank the size of the array depending upon user requirements. While an element is removed from an array, the array size must be shrunken, and if an element is added to an array, then the array ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Java Code Geeks
examples.javacodegeeks.com โ€บ home โ€บ java development โ€บ core java
Dynamic Array Java Example - Java Code Geeks
October 18, 2021 - DynamicArray class provides operations to add and remove items from an array. ... import java.util.Arrays; public class DynamicArray{ private int array[]; // holds the current size of array private int size; // holds the total capacity of array ...
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ blog โ€บ software development โ€บ creating a dynamic array in java
Dynamic Array in Java: Secret Behind High-Performance Code!
July 7, 2026 - Step 4: While an individual re-allocation is O(n) (linear time complexity), the amortized cost for a sequence of additions averages O(1). This makes the dynamic ยท array in Java highly efficient for most growth patterns.
๐ŸŒ
DEV Community
dev.to โ€บ ggorantala โ€บ dynamic-arrays-vs-traditional-arrays-with-code-examples-5dch
Dynamic Arrays vs Traditional Arrays, With Illustrations and Examples - DEV Community
June 30, 2024 - Converting an array from fixed size to resizing itself is the most common use-case for creating all complex data structures like ArrayList, growable arrays and many more. The other names of dynamic arrays are mutable arrays, resizable arrays etc.
Top answer
1 of 4
3

You can't make dynamic array in java.

For that you will have to use List or ArrayList.

We will have to provide the size of array before application run or at coding time, while arrayList gives us facility to add data while we need it, so it's size will automatically increased when we add data.

Example :

import java.util.*;

public class ArrayListDemo {
   public static void main(String args[]) {
      // create an array list
      ArrayList al = new ArrayList();
      System.out.println("Initial size of al: " + al.size());

      // add elements to the array list
      al.add("C");
      al.add("A");
      al.add("E");
      al.add("B");
      al.add("D");
      al.add("F");
      al.add(1, "A2");
      System.out.println("Size of al after additions: " + al.size());

      // display the array list
      System.out.println("Contents of al: " + al);
      // Remove elements from the array list
      al.remove("F");
      al.remove(2);
      System.out.println("Size of al after deletions: " + al.size());
      System.out.println("Contents of al: " + al);
   }
}

this example is from here.

UPDATE :

When you define your list as:

List myList = new ArrayList(); you can only call methods and reference members that belong to List class. If you define it as:

ArrayList myList = new ArrayList(); you'll be able to invoke ArrayList specific methods and use ArrayList specific members in addition to those inherited from List.

List is not a class it is an interface. It doesn't have any methods implemented. So if you call a method on a List reference, you in fact calling the method of ArrayList in both cases.

2 of 4
1

Using some kind of List is a better choice, as it basically does what you want (can grow and shrink), in fact, ArrayList is just that, a dynamic array.

You can hand roll your own if you can't use a List using System.arraycopy

For example, this will grow or shrink an array to match the size you provide...

public String[] updateArray(String[] src, int size) {

    String[] dest = new String[size];
    if (size > src.length) {

        System.arraycopy(src, 0, dest, 0, src.length);

    } else {

        System.arraycopy(src, 0, dest, 0, size);

    }

    return dest;

}

Again... List is easier...

๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ i can't wrap my head up around dynamic arrays
r/learnjava on Reddit: I can't wrap my head up around Dynamic Arrays
August 12, 2022 -

Recently I have been learning data structures I understand the concept but not 100% and in java I didn't understand the code that the guy in the video I have been watching to learn it so can any one help me with this (Sorry for my English)

Top answer
1 of 4
2
Java's ArrayList is a dynamic array.
2 of 4
1
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 - best also formatted as code block 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. 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/markdown editor: 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.
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ java-program-dynamic-array
Dynamic Array in Java - Sanfoundry
March 9, 2023 - Dynamic arrays in Java are implemented using the ArrayList class, which provides methods to add, remove, and modify elements in the array.
๐ŸŒ
Study.com
study.com โ€บ business courses โ€บ java programming tutorial & training
Java: Dynamic Arrays - Lesson | Study.com
October 12, 2018 - It doesn't take much overhead for the system to generate those extra buckets in the array. But what if the array is 2 and needs to grow to 20,000? Or what if we have an array of three dimensions and need to grow by ten-fold? All of a sudden there is a huge drain on system processing and memory usage to create the dynamic array. This is why Java doesn't natively support dynamic arrays.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ how-do-dynamic-arrays-work
How do Dynamic arrays work? - GeeksforGeeks
July 11, 2025 - A Dynamic array (vector in C++, ArrayList in Java) automatically grows when we try to make an insertion and there is no more space left for the new item. Usually the area doubles in size.
๐ŸŒ
Brilliant
brilliant.org โ€บ wiki โ€บ dynamic-arrays
Dynamic Array | Brilliant Math & Science Wiki
May 10, 2016 - It's important for the programmer to understand their program and whether or not it calls for an array or a dynamic array. In Java, the dynamic array is implemented using an ArrayList.
๐ŸŒ
HWS
math.hws.edu โ€บ eck โ€บ cs124 โ€บ javanotes6 โ€บ c7 โ€บ s3.html
Javanotes 6.0, Section 7.3 -- Dynamic Arrays and ArrayLists
In Java, every class is a subclass of the class named Object. This means that every object can be assigned to a variable of type Object. Any object can be put into an array of type Object[]. If we defined a DynamicArrayOfObject class, then we could store objects of any type.
๐ŸŒ
FinalRoundAI
finalroundai.com โ€บ blog โ€บ mastering-java-dynamic-arrays-an-in-depth-tutorial-for-developers
Mastering Java Dynamic Arrays: An In-Depth Tutorial for Developers
March 11, 2025 - These data structures are typically implemented through the ArrayList class, which provides a robust framework for storing collections of data as a java dynamic array efficiently. The initial capacity of an ArrayList is 10, a design choice that balances memory allocation with performance.
๐ŸŒ
Medium
medium.com โ€บ swlh โ€บ how-to-build-a-dynamic-array-by-using-a-static-array-in-java-5c455a69c7b4
How to Build a Dynamic Array By Using a Static Array in Java | by Vindya Gunawardana | The Startup | Medium
September 4, 2020 - A dynamic array automatically grows when we try to make an insertion and there is no space left for the new item. A simple dynamic array can be constructed, by using an array of fixed-size. The elements of the array are stored contiguously, for an instance, if it is an integer type array it will take 4 bytes of space per integer element.