Re-sizable Array in Java

Neil Holkar
4 min readOct 20, 2019

--

We all have came across arrays concept at-least once in java during learning of how to store similar data together.Lets look an example how a simple array looks in java:

Here I have created one integer array of size 10.Since index of array starts from 0, hence I have looped the array till its length and added the element at each index in the array.We can print this array in the similar way.
Similarly we can use arrays of primitive as well as user-defined data types also.But there is a major issue with arrays approach……

It looks completely fine to me…What’s the issue with it man??

If you notice closely, the restriction of using the array lies in its declaration only.We ought to tell java the size of the array we are going to create.This creates a fuss when we are not sure of the size of array we actually require to create.For example:
You are required to write a java program to take inputs from user and store it in an array and simultaneously returns the current sum of elements in array.
For solving above problem, you can never create an array of fixed size as the size of array is user bound.

To solve this problem with arrays of fixed size, we’ll use a very simple approach: An array to manipulate an array or in much simpler terms
Re-sizable arrays.

To demonstrate the concept of re-sizable array, I have created one class called MyArray and in that class I have created three private fields.One is for the array and the other two are for size/capacity of array and no of elements in the array.Constructors are also created for initializing the array.

MyArray class

Note: The parameterized constructor is used to define the initial capacity of the array.Although,our array is going to be re-sizable, but still it must have some initial capacity.The initial default capacity of the array is 10.

Now to add elements in the array, we will create one method called add().

add method

This add method takes one integer as input and insert it into our array along with incrementing the count of elements in array if the count of elements in array doesn’t reaches the current capacity of the array.Once the capacity of array is reached, it will call one method called resize to increase the size of array.

resize method

In this method, we will create first a temporary array of size twice the current size of the array.Then we’ll loop through our current array and copy each element in this temporary array. Atlast, we will give reference of our current array to this temporary array.

Hurrayy!!!The magic is done…………… but wait!Lets first test it.

I have also created one method called display to print each element in our array on the console.

display method

Now coming to our main method, where all the magic is going to be done.I have created one object of MyArray class with default size of 5.

main method

Using the object, I have first inserted 5 elements into our array using the add method and displayed them on the screen.Here is the output upto line- 58.

Now,when I try to add the another element to this class using the add method, it will call the resize method and created new array with size -10 (twice the initial size) and copy all the elements into the new array and also add our new element into it.When I try to display the array after adding the sixth element, here is the output:

Ohhh!!I can’t believe it got added into the array…This really is magic!

Hence by this way, we can manipulate our array to be of the size we desire it to be.Now guys go on and try this concept for your array problems and make your solutions more dynamic.That’s it for now..!

--

--