• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

loop problem

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the above code why can't i use the for loop to add in data??
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error do you get?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the error i get is indexoutofbound.....
so,where should i modify in my code???
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now i have change to use arraylist, but i still not sure why i can't use it..
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic