Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes I know its goofy but Im having problems with it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I know its goofy but Im having problems with it" Watch "I know its goofy but Im having problems with it" New topic
Author

I know its goofy but Im having problems with it

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
I am to write statements to declare an ArrayList named myPets and randomly add 5 Pig�s or Duck�s into myPets. I am choosing Ducks as my object. I get out of bounds exceptions errors. I get a different one everytime because of the random number generator filling the arraylist.
one example of an error:
java.lang.IndexOutOfBoundsException: Index: 4, Size: 3
at java.util.ArrayList.add(ArrayList.java:367)
at project9_1.Test.main(Test.java:19)
Exception in thread "main"
Thanks in advance all
Hook me up Mike
here is the code:

[ April 17, 2003: Message edited by: Steve Wysocki ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
ArrayList.add "Throws:
IndexOutOfBoundsException - if index is out of range (index < 0 || index > size())"
The exception is thrown if you try to add the element beyond the current (last+1)th element of the ArrayList. So you cannot just randomly add the elements to an empty list. The constructor of the ArrayList specifies its capacity, not its inital size.
[ April 17, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Steve,
try this...

this shd work..
the reason ur code is not working properly is,
what u require is,
1. "add five random ducks to the list" instead,
2. "add ducks numbered 1,2,3,4,5 at five random locations"...isnt it??
u see what i mean? ur code is trying the second option where as my code is doing the first option.
hope i understood ur question correctly...
regards
maulin.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hey Barry,
Ok, I understand what you are saying, but that is why I added that tracker boolean array to check to see if that random number had actually been put in to the specified index.
Im still not grasping your theory. Could you expand more (in English )
Thanks bro
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
also,
the reason u r getting ArrayIndexOutOfBounds is that..
in ur code ur not making tracking[num1] = true once u fill that position in the ArrayList. so what happens is, when the next time the same random number generated and stored in num1 it will add to the array list and shift the rest of the members to right as per the API of the ArrayList (look at the add(index,Object) method)...
e.g.
once u got num1 = 3 and u added an object at list[3] say..(not considering 0th index and all those stuff for simplicity). now u didn't make tracker[3] = true, right? so next time "IF" 3 is generated again via nextInt() then u essentially do,
num1 = 3 and againa list.add(3,object) which shifts the existing objects to right and so u increase the size of the object...
eventually all indexes will be filled and u don't have a space to insert more elements AND thats when u get ArrayIndex..Exception.. mind well, this is totally a random behavior meaning "one time u'll get ArrayIndex..Exception at index 3 but second time when u run , u might get the exception at index 0..."
i'm not sure about one thing. the API for ArrayList says "it will increase the size dynamically once we add more elements" but it doesn't seem to do it as we get ArrayIndex..Exception..any input from enlighteneds???

hope i'm able to explain the problem..
regards
maulin
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
One problem I see is you have an extra '}' about line 25 just below your "System.out.println( myPets ) ;" statement.
The other thing is I believe you put a System.out.println( num1 ) : statement in your loop after your initialization of num1, you will see some interesting things. I do not yet understand some of the objects you are using but for instance using ....nextInt( 5 ) prints out 0, 1, 2, 4 and skips 3 and gives an error for index 4, size 3. If you use ...nextInt( 4 ) it prints 1 only and gives an error for index 4, size 0. Could it be that your object is not zero based and your for loop is?
I will have to research Random() & ArrayList()a little more. Like I said I don't understand 'yet' but maybe this will help if you do.
HTH
doco


doco
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Look at your error: "java.lang.IndexOutOfBoundsException: Index: 4, Size: 3"
So you must have already had array elements 0,1,2 (size is 3). Your random generator then generated 4 and you tried to insert a duck at index 4, which is off the end of the current list. If you had generated the number 3 it would have been OK.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
i guess my 1st and 2nd post addresses the issues raised by Barry...
regards
maulin
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401

If you had generated the number 3 it would have been OK.

So, just how would one 'force' a sequential number from a 'random' event (so much for random walk theory)? This would lead me to ask the question: is this approach taken the best one?
Trying to understand.
doco
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hey Maulin, I was going to try that, but I thought I could be slick and use the arraylist to do it for me. I think with that method, I wont even need to check the random number. I can just add the num1 to the end of the duck object.
Well maybe not, since I am still using the random number gen as the int for the arraylist.
K, thanks for the reply
I'll keep yall posted
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Ok, now Im confused Barry. I totally understand what you are saying but could you throw me a bone on this one.
[ April 17, 2003: Message edited by: Steve Wysocki ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It looks like the random number is only used to create a "unique" name for the duck. Am I correct making this assumption? The name, and thus the random number, is independent of the index where it is stored in the ArrayList. I don't see that you need to "'force' a sequential number from a 'random' event". Also, you probably SHOULDN'T use the random number as an index into the ArrayList. Maulin's example above should illustrate the concpet that I am trying to emphasize here.


Java API Documentation
The Java Tutorial
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hey Maulin, I was going to try that, but I thought I could be slick and use the arraylist to do it for me. I think with that method, I wont even need to check the random number. I can just add the num1 to the end of the duck object.

Thats what I said before. But Is there another way of doing it ?(with the arraylist I mean) I suppose its pretty complicated. And if I do it that way, I wont need the tracker.
[ April 17, 2003: Message edited by: Steve Wysocki ]
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Yeah, with the tracker, it doesn't print out all of the ducks as per Barry's posts. But I would like to do what the problem asks for. I don't want to do what Maulin suggests. Although it works, it just puts the same duck in more than once because the random num gen can gen the same number and I could conceiveably (sic) have all of the same ducks. (lol, I cant believe we are talking about ducks)
But if this is all that I can do, it will work for now, unless anybody has any suggestions on how to add (the object) into a random index or vice versa.
Thanks for the help all.
Steve
[ April 17, 2003: Message edited by: Steve Wysocki ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Steve
my code shdn't put one duck in more than 1 buck ever. its sort of infinite loop u know.
i've count< 5 but i ONLY increase count if tracker[num1] == false and i make tracker[num1] = true appropriately...
so if the num1 repeats itself then i don't put it second time => the duck doesn't get duplicated ever...
did u observed the changed code that i have-
myPets.add(count, "Duck" + num1);
its not myPets.add(num1, "Duck" + count); but the otherway round...
regards
maulin
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You can still use a boolean array to check if you previously generated a given number. (I think this is what you are calling "the tracker?") The only difference is that this random number is NOT the index into the ArrayList where the ducks are stored. Therefore, you should continue to generate a random number, but not increment the index into the ArrayList. In pseudo-code, it could look something like this:

HTH
Layne
p.s. It looks like this is very similar to what you are doing already. However, you should use a while loop instead of an if statement in order to continuously generate a new random number until you get one that you haven't used yet.
[ April 17, 2003: Message edited by: Layne Lund ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Layne
i'm trying to do exactly what u suggest in my code..
i wish if one could pay attention to the code i've...
regards
maulin
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Sorry, it looks like you did address the problem of duplicating ducks. However, if you DO have a duplicate duck, your code doesn't generate a replacement for it. With a single duplicate, your loop would only create 4 ducks instead of the 5 needed.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Donald R. Cossitt:

So, just how would one 'force' a sequential number from a 'random' event (so much for random walk theory)? This would lead me to ask the question: is this approach taken the best one?

I'm just trying to point out where the exception is coming from. You cannot, of course, "predict" the next number to be generated. The approach taken to solve this problem is not, in my view, the best one either.
Sorry, it's after midnight, got to get my beauty sleep...
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Here's a duck generator I dreamt up...

Now I can get to sleep...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I know its goofy but Im having problems with it