Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Resizing array??

807569May 30 2006 — edited May 30 2006
How 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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 27 2006
Added on May 30 2006
5 comments
289 views