File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes loop problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "loop problem" Watch "loop problem" New topic
Author

loop problem

ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54

In the above code why can't i use the for loop to add in data??
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
What error do you get?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
the error i get is indexoutofbound.....
so,where should i modify in my code???
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: May 17, 2006
Posts: 54
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: May 17, 2006
Posts: 54
now i have change to use arraylist, but i still not sure why i can't use it..
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
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

Joined: Feb 02, 2006
Posts: 2023

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

Joined: May 17, 2006
Posts: 54
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?
 
 
subject: loop problem
 
Similar Threads
Doubt in this()
doubt from ranch roundup
JComboBox
Help with Constructor!
why can't my arraycopy work???