Our social:

Friday, 25 December 2015

Growable Array Alogrithm programming Example

Tags: How To Create Growable Array, How To Insert Element In Array,How To Insert Add In Array,How To Remove Element In Array,How To Replace Element In Array, Growable Array Algorithm Program.


This is growable Array algorithm program that is able to slove size limitation and type limitation     problem of Array. In this progran we can increase array size if required dynamically" increaseCapacity()" method is responsible to do this work. Here various methods are defined to insert, add, remove and replace to modifying array element


import java.io.*;
class ArrayCollection
{
private Object[] arrayObj=new Object[5];
private int index=0;
public int capacity(){
return arrayObj.length;
}

public int size(){
return index;
}

//Add elements to array
public void add(Object ele){
if(this.size()==this.capacity()){
increaseCapacity();
}
this.arrayObj[index]=ele;
index++;
}

//increase capacity method for arrayObject 

public void increaseCapacity(){
int newCapacity=this.capacity()+(this.capacity()/2);
Object[] tempObj=new Object[newCapacity];

for(int i=0;i<arrayObj.length; i++){
tempObj[i]=arrayObj[i];
}
arrayObj=tempObj;
}

//retriving element from collection

public Object get(int index){
if(index<size()){
return arrayObj[index];
}else{

throw new ArrayIndexOutOfBoundsException(""+index);
}
}

//replacing logic in replace method

public void replace(int index, Object ele){
if(index>=this.size()){
throw new ArrayIndexOutOfBoundsException(""+index);
}else{
this.arrayObj[index]=ele;

}
}

//code removing elements from collection

public void remove(int index){
if(index >=this.size()){
throw new ArrayIndexOutOfBoundsException(""+index);
}else{
for(;index<this.size()-1; index++){
arrayObj[index]=arrayObj[index+1];
}
arrayObj[size()-1]=null;
index--;
}
}

//inserting values in Collection

public void insert(int index, Object ele){
if(index>=size()){
throw new ArrayIndexOutOfBoundsException(""+index);
}
if(size()==capacity()){
increaseCapacity();
}
//System.out.println("hiii");
for(int i=size();i>=index;i--){
arrayObj[i+1]=arrayObj[i];
}
arrayObj[index]=ele;
this.index++;
}
// this section for testing purpose........
public static void main(String[] args) 
{
ArrayCollection ar=new ArrayCollection();
System.out.println("Capacity of this array "+ar.capacity());
System.out.println("Size of this array "+ar.size());

ar.add(1);
ar.add(2);
ar.add(3);
ar.add(4);
ar.add(5);
System.out.println("Size of this array "+ar.size());
//System.out.println("value "+ ar.get(2));
//ar.replace(2,8);
//System.out.println("after replacing index 2 value is"+ ar.get(2));
//ar.remove(2);
//System.out.println("after removing index 2 value is"+ ar.get(2));
//System.out.println(ar.size());
ar.insert(2,9);
System.out.println(ar.get(2));
System.out.println(ar.capacity());
System.out.println(ar.size());
}
//you can also test program according to requirment....
}

You can execute this program directly by just copy from here and paste in notepad and save this file with class name with .java extension. And then simply execute from command prompt...
                                                                                    Thank you(Smartyy Sunny)
                                                                                                         Happy Coding......

0 comments:

Post a Comment