0% found this document useful (0 votes)
100 views

20 - Custom Collection Development Part 8 - Core Java Tutorial - Mr. Hari Krishna

The GrowableAlgorithForIncreasingArrayCapacity class implements a growable array that doubles the capacity when the element count reaches the current capacity. It contains methods like add(), increaseCapacity(), capacity(), size(), get(), replace(), remove(), and insert() to manage the underlying object array and dynamically grow the capacity as needed. The TestForGrowableAlgorithmForIncreArrayCap class tests the functionality of the growable array by adding elements, checking capacity and size, retrieving, replacing, removing, and inserting elements.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

20 - Custom Collection Development Part 8 - Core Java Tutorial - Mr. Hari Krishna

The GrowableAlgorithForIncreasingArrayCapacity class implements a growable array that doubles the capacity when the element count reaches the current capacity. It contains methods like add(), increaseCapacity(), capacity(), size(), get(), replace(), remove(), and insert() to manage the underlying object array and dynamically grow the capacity as needed. The TestForGrowableAlgorithmForIncreArrayCap class tests the functionality of the growable array by adding elements, checking capacity and size, retrieving, replacing, removing, and inserting elements.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

package hariKrishna;

public class GrowableAlgorithForIncreasingArrayCapacity {


Object[] objArray=new Object[10];
int elementCount=0;
public void add(Object obj){
if(elementCount==objArray.length){
increaseCapacity();
}
objArray[elementCount]=obj;
elementCount++;
}
public void increaseCapacity() {
int newCapacity=objArray.length*2;
Object[] nextArray=new Object[newCapacity];
for(int i=0;i<objArray.length;i++){
nextArray[i]=objArray[i];
}
objArray=nextArray;
}
public int capacity(){
return objArray.length;
}
public int size(){
return elementCount;
}
public Object get(int index){
if(index<0 || index>=size()){
throw new IndexOutOfBoundsException(""+index);
}
return objArray[index];
}

public void replace(int index,Object obj){


if(index<0 || index>=size()){
throw new IndexOutOfBoundsException(""+index);
}
objArray[index]=obj;
}

public void remove(int index){


if(index<0 || index>=size()){
throw new IndexOutOfBoundsException(""+index);
}
while(index<size()-1){
objArray[index]=objArray[index+1];
index++;
}
objArray[index]=null;
elementCount--;
}

public void insert(int index,Object obj){


if(index<0 || index>size()){
throw new IndexOutOfBoundsException(""+index);
}
if(size()==capacity()){
increaseCapacity();
}
for(int i=size()-1;i>=index;i--){
objArray[i+1]=objArray[i];
}
objArray[index]=obj;
elementCount++;
}
}

============Testing Inserting element in the Array Object============================

package hariKrishna;
public class TestForGrowableAlgorithmForIncreArrayCap {
public static void main(String[] args){
GrowableAlgorithForIncreasingArrayCapacity GATest=new
GrowableAlgorithForIncreasingArrayCapacity();
GATest.add("a");
GATest.add("b");
GATest.add("c");
GATest.add(5);
GATest.add(6.7);
GATest.add("a");
GATest.add(null);
GATest.add(null);
GATest.add(9);
GATest.add(10);
GATest.add(11);
GATest.add(12);
System.out.println("Capacity of Array Object :"+GATest.capacity());
System.out.println("Element Count or Size of Array
Object :"+GATest.size());
System.out.println("Elements stored in the Array Object :");
for(int i=0;i<GATest.objArray.length;i++){
System.out.println(GATest.objArray[i]);
}
Object obj=GATest.get(2);
System.out.println("Element stored in the index using get
method :"+obj);

GATest.replace(2, "B");
obj=GATest.get(2);
System.out.println("Element stored in the index using get
method :"+obj);

GATest.remove(2);
//System.out.println(GATest.get(3));
for(int i=0;i<GATest.size();i++){
System.out.println(GATest.get(i));
}
GATest.insert(2,"Z");
for(int i=0;i<GATest.size();i++){
System.out.println(GATest.get(i));
}
}
}

You might also like