Thursday, November 14, 2013

Dynamic Array Example in Java


DynamicArrayExample.java

package com.ranga;

/**
 *
 * This program demonstrates the creating dynamic arrays.
 * User: ranga
 * Date: 11/14/13
 * Time: 4:40 PM
 */

class DynamicArray {
 
  // the variable data[] is used to store the array elements.
  private int data[];
  // the variable size is used to specify the number of array elements.
  // the variable capacity is used to specify the how many elements we can   store.
  private int size, capacity;

  // Constructs an empty DynamicArray
  public DynamicArray() {
    size = 0;
    capacity = 10;
    data = new int[capacity];
  }

// Constructs an empty DynamicArray with the specified initial capacity.
  public DynamicArray(int capacity) {
    if(capacity < 0)
      throw new NegativeArraySizeException("NegativeArraySizeException - capacity is "+capacity);
    else
      this.capacity = capacity;
    size = 0;
    data = new int[capacity];
  }

// fetching the array size
  public int size() {
    return size;
  }
// fetching the array element based on index
  public int get(int index) {
    validateIndex(index);
    return data[index];
  }
// setting array the element at the end of the array
  public void set(int item) {
    if(capacity <= size) {
      ensureCapacity();
    }
    data[size++] = item;
  }
// checking the capacity if array elements size exceeds the default capacity //then increasing the capacity

  private void ensureCapacity() {
    capacity = (capacity * 2);
    int oldData[] = data;
    data = new int[capacity];
    for(int i =0; i< oldData.length; i++) {
      data[i] = oldData[i];
    }
  }
// validating the array index
  private void validateIndex(int index) {
    if (index < 0 || index > size)
      throw new ArrayIndexOutOfBoundsException("ArrayIndexOutOfBoundsException - Index is " + index + ", Size is " + size);
  }

// removing the all array elements
  public void removeAll() {
    size = 0;
    capacity = 10;
  }

// checking the array is empty or not
  public boolean isEmpty() {
    return size == 0;
  }

}

public class DynamicArrayExample
{
  public static void main (String[] args)
  {
    DynamicArray dynamicArray = new DynamicArray();
    for(int i=0; i< 25; i++) {
      dynamicArray.set(i*2);
    }

    System.out.println("Array Elements : ");
    for(int i =0; i< dynamicArray.size(); i++) {
      System.out.println("Index : "+ ( i + 1 )+" Element : " + dynamicArray.get(i));
    }
    System.out.println("Is Array Empty: "+dynamicArray.isEmpty());

    System.out.println("Fetching the array element : "+dynamicArray.get(6));

    System.out.println("Clear the Array Elements");
    dynamicArray.removeAll();

    System.out.println("After Clearing Array Elements : ");
    for(int i =0; i< dynamicArray.size(); i++) {
      System.out.println("Index : "+ ( i + 1 )+" Element : " + dynamicArray.get(i));
    }

    System.out.println("Is Array Empty: "+dynamicArray.isEmpty());
  }
}

Output:

Array Elements :
Index : 1 Element : 0
Index : 2 Element : 2
Index : 3 Element : 4
Index : 4 Element : 6
Index : 5 Element : 8
Index : 6 Element : 10
Index : 7 Element : 12
Index : 8 Element : 14
Index : 9 Element : 16
Index : 10 Element : 18
Index : 11 Element : 20
Index : 12 Element : 22
Index : 13 Element : 24
Index : 14 Element : 26
Index : 15 Element : 28
Index : 16 Element : 30
Index : 17 Element : 32
Index : 18 Element : 34
Index : 19 Element : 36
Index : 20 Element : 38
Index : 21 Element : 40
Index : 22 Element : 42
Index : 23 Element : 44
Index : 24 Element : 46
Index : 25 Element : 48
Is Array Empty: false
Fetching the array element : 12
Clear the Array Elements
After Clearing Array Elements :
Is Array Empty: true

0 comments: