Resizing array??
807569May 30 2006 — edited May 30 2006How can i keep on resizing an array until it reaches the maximum element an array can handle. Below is my code:
package firstPackage;
public class FishClass {
int numberOfFish=0;
protected int fishAmount=5;
Fish[] fish=new Fish[fishAmount];
public void setFish(String name,int age){
if(full()){
resize();
}
fish[numberOfFish]=new Fish();
fish[numberOfFish].setName(name);
fish[numberOfFish].setAge(age);
numberOfFish++;
}
private boolean full(){
return numberOfFish==fishAmount;
}
private void resize(){
Fish [] newFish=new Fish[fishAmount*2];
for(int x=0;x<fish.length;x++){
newFish[x]=fish[x];
}
fish=newFish;
}
public Fish getFish(int x){
return fish[x];
}
}
I wish to keep on looping because if i set the fishAmount*2 and when it still can't get the correct element that an array can accept it will throw ArrayOutOfBoundException.
For your information i do not want to use ArrayList and Vector.