Programs

Creating a Dynamic Array in Java

What is an Array?

The homogeneous data structures that are implemented as objects in Java are called arrays. An array can store multiple values of a particular data type. An array provides an indexed address to store these different data types. Thus, to access any element in the array, the index is used.

What is Dynamic Array in Java?

A dynamic array has the inherited property of growing by itself whenever any insertion is made if there is no space left of the incoming data. This growth is up to double area size.  Hence, a dynamic array overcomes the challenges of a simple array. It is a variable size list data structure.

Dynamic array in Java allows the addition or removal of elements. At run time, memory is allocated using the heap concept. At the same time, the size of the dynamic array can be changed.

It is easy to construct a dynamic array in Java. You can start by allocating a fixed-sized array that is larger than the number of elements required immediately. 

Check out our free technology courses to get an edge over the competition.

Explore our Popular Software Engineering Courses

How Dynamic Array in Java Works?

The elements in a dynamic array are stored in the closest proximity from the starting of the array. Hence, after storage, the remaining spaces are left unused. Thus, this unused space is the limit to which elements can be added in a dynamic array.

There is a need to extend this fixed-sized array when the reserved space is consumed to full. There are two ways to achieve this. 

Firstly, before adding the element, a bigger array is introduced. The elements from the fixed-sized previous array are copied to this new array. The system returns to this new array before adding the element.

In the second way, a function is created. This function creates a new array of double size, copies all the elements from the old array, and returns to the new array.

Both these ways can be implemented to shrink the size of any dynamic array in Java.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

In-Demand Software Development Skills

What is the Size and Capacity of a Dynamic Array in Java?

Being a data structure, the size and capacity of a dynamic array are not the same. The initialization of any dynamic array creates a fixed-sized array. When the number of elements is added to the array, they start taking their place just like A, B, C, D, and E in the image. The last five places are still empty. Thus, the length of this dynamic array size is five, but it can accommodate ten elements.

This is the difference between the size and capacity of a dynamic array in Java.

Source

Read our Popular Articles related to Software Development

How Dynamic Array Works in Real-Time?

The following illustration explains how the elements are stacked in real-time in a dynamic array. Here, the size of the dynamic array is three, and the array’s capacity is six.

Source

The following are the average case and worst-case real-time scenarios and how the instructions change accordingly.

Source

How to Initialize a Dynamic Array in Java?

In Java, a dynamic array is initiated like the same as a static array. Thus, it can be understood with the help of the following illustration:

Input:

Source

Output:

Source

What are the Features of Dynamic Array?

The features of a dynamic array in Java are:

  • Adding an Element

Any element can be added simply at the end of a dynamic array if it has space. If there is no space, then you need to extend the size of the array. After size extension, the element and index are added at the end of the original array.

This copy takes O (n) time, where n stands for the number of elements in the array. Hence, in the fixed-length array, the element only requires O (1) time. Thus, the time taken according to adding the element differs from O (1) to O (n).

If you need to add some more elements in a fixed-sized array, the following approach can be implemented.

Source

  • Deleting an Element

Any element can be deleted from the dynamic array using the remove () or removeAt (i) method. The default remove () method stores zero at the last index after deleting the array element. The removeAt (i) method can delete the element at a specific index. The removeAt (i) is called that shifts all the elements on the left side from the given index “i.”

 

Source

  • Resizing the Array

Resizing an array is essential when you need to add a new element in the dynamic array whose entire space has been utilized. In a case where a dynamic array has zero data at the right side and takes unrequited memory, srinkSize () method is used. It frees the extra memory. In other cases, resizing is achieved by allocating a bigger array, copying the elements from the old array, and adding the new element. The following illustration helps to understand the resizing of a dynamic array in Java.

Source

  • Built-in Dynamic Arrays in Java

Java has the following built-in dynamic arrays:

  • ArrayList

The ArrayList is a resizable and non-synchronized array implementation of the interface ‘List’. It allows all items, including null, and implements all optional list operations. It also offers methods to alter the array size that is internally used to store the list. The following program illustrates several methods supported by ArrayList in Java:

Input:

Source

Output:

Source

  • LinkedList

It is a doubly linked non-synchronized list implementation of the deque and list interfaces. It implements all non-compulsory list operations and allows all elements, including the null. The following program illustrates several methods supported by LinkedList in Java:

Input:

Source

Output:

Source

  • CopyOnWriteArrayList

It is an ArrayList’s thread-safe variant in which all operations like addition, setting, etc., are implemented by making a fresh copy of the previous array. It is highly efficient when the traversals operations exceed the addition, setting, etc. Like ArrayList, null is also permitted in CopyOnWriteArrayList. 

It can employ a snapshot style iterator method that uses a reference to the array’s state at the point where the iterator was created. The interface is possible due to the fact that the array never changes during the iterator’s lifetime. The following program illustrates several methods supported by CopyOnWriteArrayList in Java:

Input:

Source

Output:

Source

  • Vector

It contains the components that can be accessed using an integer index. It implements a growable array of objects. After creating a vector, its size can grow or shrink according to the addition or removal of the elements. It is not a thread-safe implementation but is synchronized. It is the best replacement for the ArrayList. The following program illustrating several methods supported by Vector in Java:

Input:

Source:

Output:

Source

Programs: Dynamic Array in Java

  • Program showcasing the copying of all elements to a new double-size dynamic array when it becomes full.

Input:

Source

Output:

Source

Advantages of Dynamic Array in Java

The benefits of a dynamic array in Java are:

  • No need to determine size ahead of time

The dynamic array in Java can be expanded according to the addition of more elements in real-time. Thus, there is no need to determine the size of the dynamic array ahead of the time of execution. In other words, the dynamic array has a capacity for automatic resizing.

  • Variable size:

The dynamic array in Java has variable size. Thus, you can insert as many elements as you want to. The dynamic array expands accordingly to accommodate all the elements.

  • Efficient utilization of cache:

Dynamic array in Java places elements next to each other. Hence, it is cache-friendly.

  • Fast Lookup:

Dynamic array in Java takes O (1) time to retrieve the element at a given index. Just like other arrays, the dynamic array has a quick lookup.

Limitations of Dynamic Array in Java

In Java, there are certain limitations of dynamic arrays. These are:

  • Additions are slow in some cases:

In standard cases, the addition of a new element at the end of a dynamic array in Java takes O (1) at one instance. However, if the dynamic array doesn’t have indices left for more elements, it requires expanding. Thus, further O (n) time is taken by the dynamic array.

  • Costly insertion and deletes:

Dynamic arrays in Java add elements in a sequential manner. Hence, there are minor issues about adding them at the end. But in case, you need to insert or delete some element from the center of the array, it is not easy. You have to push the elements in this case which requires O (n) at a time.

Points to Remember- Dynamic Array in Java

  • Dynamic arrays in Java are also called ArrayLists commonly.
  • There is a fixed length of each standard Java array.
  • The size of the dynamic array can be changed at run time.
  • There are two ways to add elements in any dynamic array.
  • The size and capacity of a dynamic array in Java differ according to the number of elements.
  • Dynamic array is started just like a static array in Java.
  • Adding, deleting, and resizing are the main features of a dynamic array in Java.
  • Java has built-in dynamic arrays namely ArrayList, LinkedList, CopyOnWriteArrayList, Vector, etc.
Want to share this article?

Plan your Software Development Career Now!

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks