![]() |
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 ArrayA 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
Program to Implement a Resizable Array in JavaBelow is the Program for ResizableArray Class Implementation:Java
Program Using the Created Resizable Array in Java1. Adding Elements to ResizableArrayBelow is the implementation of Adding elements to Resizable Array in Java: Java
Output:
|
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()); } } |
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()); } } |
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |