How to implement our own Dynamic Array class in Java?
Given task is to implement a class in Java which behaves just like the Dynamic array using ArrayList.
ArrayList is same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
- ArrayList elements are placed in contiguous storage so that they can be accessed and traversed using iterators.
- In ArrayList, data is inserted at the end.
- Inserting at the end takes differential time, as sometimes there may be a need of extending the array.
- Removing the last element takes only constant time because no resizing happens.
- Inserting and erasing at the beginning or in the middle is linear in time.
Functions to be implemented in the Dynamic array class:
Certain functions associated with the ArrayList that we will implement are:
- void push(int data): This function takes one element and inserts it at the last. Amortized time complexity is O(1).
- void push(int data, int index): It inserts data at the specified index. Time complexity is O(1).
- int get(int index): It is used to get the element at the specified index. Time complexity is O(1).
- void pop(): It deletes the last element. Time complexity is O(1).
- int size(): It returns the size of the ArrayList i.e, number of elements in the ArrayList. Time complexity is O(1).
- int getcapacity(): It returns the capacity of the ArrayList. Time complexity is O(1).
- void print(): It is used to print array elements. Time complexity is O(N), where N is the size of the ArrayList.
Implementation of the Dynamic array class:
Below is the implementation of our own ArrayList class.
import java.util.*;
class ArrayListClass {
private int arr[];
private int capacity;
private int current;
public ArrayListClass()
{
arr = new int [ 1 ];
capacity = 1 ;
current = 0 ;
}
public void push( int data)
{
if (current == capacity) {
int temp[] = new int [ 2 * capacity];
for ( int i = 0 ; i < capacity; i++)
temp[i] = arr[i];
capacity *= 2 ;
arr = temp;
}
arr[current] = data;
current++;
}
void push( int data, int index)
{
if (index == capacity)
push(data);
else
arr[index] = data;
}
int get( int index)
{
if (index < current)
return arr[index];
return - 1 ;
}
void pop()
{
current--;
}
int size()
{
return current;
}
int getcapacity()
{
return capacity;
}
void print()
{
for ( int i = 0 ; i < current; i++) {
System.out.print(arr[i] + " " );
}
System.out.println();
}
public static void main(String args[])
{
ArrayListClass v
= new ArrayListClass();
v.push( 10 );
v.push( 20 );
v.push( 30 );
v.push( 40 );
v.push( 50 );
System.out.println( "ArrayList size: "
+ v.size());
System.out.println(
"ArrayList capacity: "
+ v.getcapacity());
System.out.println(
"ArrayList elements: " );
v.print();
v.push( 100 , 1 );
System.out.println(
"\nAfter updating 1st index" );
System.out.println(
"ArrayList elements: " );
v.print();
System.out.println(
"Element at 1st index: "
+ v.get( 1 ));
v.pop();
System.out.println(
"\nAfter deleting the"
+ " last element" );
System.out.println(
"ArrayList size: "
+ v.size());
System.out.println(
"ArrayList capacity: "
+ v.getcapacity());
System.out.println(
"ArrayList elements: " );
v.print();
}
}
|
Output:
ArrayList size: 5
ArrayList capacity: 8
ArrayList elements:
10 20 30 40 50
After updating 1st index
ArrayList elements:
10 100 30 40 50
Element at 1st index: 100
After deleting the last element
ArrayList size: 4
ArrayList capacity: 8
ArrayList elements:
10 100 30 40
Last Updated :
23 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...