How to Implement a Resizable Array in Java?
Last Updated :
14 Feb, 2024
In Java, Arrays store the data and manipulate collections of elements, The fixed size of traditional arrays can be limiting in dynamic scenarios where the size of the collection may change over time.
This Article Explores the concept of Resizable Arrays and Demonstrates how to implement Resizable Arrays in Java.
Resizable Array
A Resizable Array Unlike other arrays, which have fixed size, resizable arrays automatically adjust their capacity to accommodate more elements as needed.
Benefits Of Resizable Arrays
- Dynamic Size: Resizable arrays are adapted to the number of elements they contain and eliminate the need for predefined sizes.
- Memory Usage: Only Efficient Memory is allocated based on the actual number of elements, reducing wasted space.
- Flexibility: Resizable arrays can be easily adapted to changing requirements.
Program to Implement a Resizable Array in Java
Below is the Program for ResizableArray Class Implementation:
Java
package ResizableArrayPackage;
import java.util.Arrays;
public class ResizableArray<T> {
private static final int DEFAULT_CAPACITY = 10 ;
private Object[] array;
private int size;
public ResizableArray() {
this .array = new Object[DEFAULT_CAPACITY];
this .size = 0 ;
}
public void add(T element) {
ensureCapacity();
array[size++] = element;
}
public void remove( int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException( "Array Index OutOfBound Exception occurred" );
}
System.arraycopy(array, index + 1 , array, index, size - index - 1 );
array[--size] = null ;
}
public int size() {
return size;
}
private void ensureCapacity() {
if (size == array.length) {
int newCapacity = array.length * 2 ;
array = Arrays.copyOf(array, newCapacity);
}
}
@Override
public String toString() {
return Arrays.toString(Arrays.copyOf(array, size));
}
}
|
Program Using the Created Resizable Array in Java
1. Adding Elements to ResizableArray
Below is the implementation of Adding elements to Resizable Array in Java:
Java
package ResizableArrayPackage;
public class AddElementsExample {
public static void main(String[] args) {
ResizableArray<Integer> dynamicArray = new ResizableArray<>();
dynamicArray.add( 100 );
dynamicArray.add( 150 );
dynamicArray.add( 200 );
System.out.println( "ResizableArray after adding elements: " + dynamicArray);
System.out.println( "Current size: " + dynamicArray.size());
}
}
|
Output: 2. Removing an Element from ResizableArray
Below is the implementation of Removing element from ResizableArray:
Java
package ResizableArrayPackage;
public class RemoveElementExample {
public static void main(String[] args) {
ResizableArray<String> dynamicArray = new ResizableArray<>();
dynamicArray.add( "Apple" );
dynamicArray.add( "Banana" );
dynamicArray.add( "Orange" );
System.out.println( "ResizableArray before removal: " + dynamicArray);
dynamicArray.remove( 1 );
System.out.println( "ResizableArray after removal: " + dynamicArray);
System.out.println( "Current size: " + dynamicArray.size());
}
}
|
Output:
3. Resizing the Resizable Array
Below is the implementation of Resizing the Resizable Array:
Java
package ResizableArrayPackage;
public class ResizeArrayExample {
public static void main(String[] args) {
ResizableArray<Double> dynamicArray = new ResizableArray<>();
for ( int i = 0 ; i < 15 ; i++) {
dynamicArray.add(Math.random());
}
System.out.println( "ResizableArray after resizing: " + dynamicArray);
System.out.println( "Current size: " + dynamicArray.size());
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...