Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Creating a Dynamic Array in Java

Updated on 28 November, 2022

8.52K+ views
12 min read

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.

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.

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.

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.
  • Amid increased conversations around crypto and Blockchain technology, if you wish to educate yourself professionally on the topic, then upGrad’s Executive Post Graduate Programme in Software Development – Specialisation in Blockchain under IIIT- Bangalore is the right choice for you!

RELATED PROGRAMS