Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loop problem

 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the above code why can't i use the for loop to add in data??
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error do you get?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember, array indexing starts at zero. So if the array size is "fishAmount," then an index equal to fishAmount will be out of bounds...

numberOfFish<=fishAmount
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the error i get is indexoutofbound.....
so,where should i modify in my code???
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ming ming:
the error i get is indexoutofbound.....
so,where should i modify in my code???

You're out of bounds when the index equals fishAmount. So instead of less than or equal to fishAmount, you just want less than fishAmount...
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it still can't work...stil the same error....
i'm doing things like set the size of the array is 2 then when i add more it will automatic increase the size of it...
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is easier if you use the length field of the array in the condition rather than a number you will have to change.

You can't change the size of an array once it's created. However, you can create another array whose size is larger than the first, copy the contents of the first into the array, and then change the original reference to point to the new array.
[ May 27, 2006: Message edited by: Keith Lynn ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ming ming:
...i'm doing things like set the size of the array is 2 then when i add more it will automatic increase the size of it...

That's the behavior of an ArrayList -- not an array. As Keith pointed out, you can't change the size of an array once it's instantiated.
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i have change to use arraylist, but i still not sure why i can't use it..
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i do not want to use arraylist because of some reason, i have succesfully resize it but now i can't access the data inside it, when i tried to use getName it will show NullPointerException..What happened? i dun see any object is null
Mayb someone can point it out 4 me..

 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public void setFish1(String name,int age){
y++;
setSize();
setFish2(name,age);
}
public void setSize(){
fish=new Fish[y];
}

You redefine the Fish array every time. In the end, you only keep one Fish instance in your new array (the last one in your array).
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i solve that if i put fish=new Fish[y]; this into instance variable it doesn't allow me to do it...any better solution for this?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic