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:
package ResizableArrayPackage;
import java.util.Arrays;
// ResizableArray Class public class ResizableArray<T> {
// Default capacity for the array
private static final int DEFAULT_CAPACITY = 10 ;
// Array to store elements
private Object[] array;
// Current size of the array
private int size;
// Constructor to initialize the array
// with default capacity and size
public ResizableArray() {
this .array = new Object[DEFAULT_CAPACITY];
this .size = 0 ;
}
// method to add an element to the array
public void add(T element) {
// ensure capacity before adding an element
ensureCapacity();
// add the element to the array and increment the size
array[size++] = element;
}
// method to remove an element at a specific index
public void remove( int index) {
// check if the index is valid
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException( "Array Index OutOfBound Exception occurred" );
}
// Use arraycopy to shift elements
// and remove the specified element
System.arraycopy(array, index + 1 , array, index, size - index - 1 );
// Set the last element to
// null and decrement the size
array[--size] = null ;
}
// Method to get the current size of the array
public int size() {
return size;
}
// Private method to ensure that
// the array has enough capacity
private void ensureCapacity() {
// If the size equals the
// array length, double the capacity
if (size == array.length) {
int newCapacity = array.length * 2 ;
// Use Arrays.copyOf to create a
// new array with the increased capacity
array = Arrays.copyOf(array, newCapacity);
}
}
// Override toString method to
// display the contents of the array
@Override
public String toString() {
// Use Arrays.toString to create
// a string representation of the array
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:
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:
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 ); // removing element at index 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:
package ResizableArrayPackage;
public class ResizeArrayExample {
public static void main(String[] args) {
// Create an instance of ResizableArray
ResizableArray<Double> dynamicArray = new ResizableArray<>();
// Add random elements to the dynamic array
for ( int i = 0 ; i < 15 ; i++) {
dynamicArray.add(Math.random());
}
// Print the dynamic array after resizing
System.out.println( "ResizableArray after resizing: " + dynamicArray);
// Print the current size of the dynamic array
System.out.println( "Current size: " + dynamicArray.size());
}
} |