• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

What am I missing here? (ArrayList check)

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a simple lotto game where the player enters 10 numbers and is stored in an ArrayList (pNums), the computer generates 20 numbers also stored in an ArrayList (cNums), then finally places the matches into a third ArrayList (mNums). The problem I am having is with checking, removing existing, then replacing the cNums arraylist.

I'm new to ArrayLists so my problem could be with the logic or my usage of ArrayLists. Anyways, heres the code:


The problem is that my program will still generate matching numbers inside of that loop. I've looked at it over and over again and can't figure out why it isn't doing the check if(cNums.contains(cNums.get(i)), removing the number (cNums.remove(i), then adding a newly generated number (cNums.add((Integer)rGen.nextInt(MAX_VALUE)).

Thanks in advance for any help. I'll continue to look over it and walk through it to see if I can find what I'm missing.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason,

I'm not sure I follow what you're trying to do, but wouldn't it make more sense to simply generate numbers and check if they're in the arraylist first, before adding them?



Finally, note that "contains()" is expensive when called on a long list -- it has to look at each item in the list one at a time. Sets like HashSet or TreeSet are much better at checking for a value, and they do it automatically -- a Set, by definition, can't contain duplicates. So you could simplify this further by adding random numbers to a HashSet until it was the right size, and then constructing an ArrayList to contain the result. It's just a few lines of code, but I'll leave it up to you.
 
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But it IS doing that, because that's the code you wrote. I think the real question is, why doesn't that code do what you expected it to do?

Unfortunately I can't comment on that because (a) I can't tell what you expected it to do, and (b) I can't guess what it was meant to do.

First of all you have it inside a for-loop whose index is named "j", but you don't use the variable "j" anywhere inside that loop. That seems a bit odd to me.

Second, you have this code:

The boolean expression there will always be true, because the cNums list will always contain an element which was selected from the cNums list.

And then if it is true (which is is) then you remove that element from the cNums list, and then you add a new element at the end of the cNums list.

So when i=0, for example, which would be the first time through the outer loop, you first add a random element to cNums. Then you repeatedly (CNUMS times) remove that random element and add a new random element. What's the point of that?

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

 
Jason Sgalla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:Hi Jason,

I'm not sure I follow what you're trying to do, but wouldn't it make more sense to simply generate numbers and check if they're in the arraylist first, before adding them?



Finally, note that "contains()" is expensive when called on a long list -- it has to look at each item in the list one at a time. Sets like HashSet or TreeSet are much better at checking for a value, and they do it automatically -- a Set, by definition, can't contain duplicates. So you could simplify this further by adding random numbers to a HashSet until it was the right size, and then constructing an ArrayList to contain the result. It's just a few lines of code, but I'll leave it up to you.



I decided to go back to the drawing board with Java and start from the beginning again to re-learn everything. When I originally created this program I used Swing elements to draw a GUI but couldn't get the ScoreBoard panel to update after pressing play (everything else worked behind the scenes except for that).

The game starts by asking for 10 numbers, gets those numbers, checks for duplicates, shows them to the player, then asks for P to play Q to quit. After pressing "P" I had the code I provided that was supposed to: generate a random number, check for duplicates, then display them to the user. The program shows the number of matches to the user and which numbers actually matched up. That's it.

That is exactly what I was trying to do! I don't know anything about HashSets or TreeSets yet (although I have heard of HashSets) but I will look into those.

Why I wrote the code I wrote:
1. The for loop was to fill the arraylist with random numbers
2. The second for loop was supposed to cycle through the array and check if that newly created element was already used.
-- I see where I went wrong here. I'm not sure why I didn't see that when I was going over it, I'm still new to all this. Of coarse it is going to contain that number because it just created it
3. Remove the element if it was a duplicate
4. Create a new random number.

**NOTE
This was something I had already wrote and had working with normal arrays, but I wanted to move on try to use ArrayLists. To be honest, I'm not sure how I had this working with regular arrays (I started over from scratch) but somehow it was. Or perhaps, I was just getting lucky with my generated numbers.

Either way, thanks again for the input.

You guys/gals are great, as always.
 
Paul Clapham
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This sounds familiar, because I've gone through this process many times before. Here's the process I mean:

(1) Hey, let's change this code to use this new feature!

(2) What? Why doesn't it work any more?

(3) OMG! How could I be so stupid?

So yeah. It happens to all of us. Keep up the good work!
 
reply
    Bookmark Topic Watch Topic
  • New Topic