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 ); // 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:
Java
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()); } } |
Output:
How to Implement a Resizable Array in Java?
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.