Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList Problem

 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone:

I am writing a program so that it creates a 52 card deck and then shuffles it. However, I am having trouble with getting Java write the "cards" (or Strings), into each element of the ArrayList. I know it isn't doing it because every time I test the program with the isEmpty() method, it keeps saying TRUE. By the time the methods have been invoked the computer should have an ArrayList all set to be used for the simulation.

Can anyone tell me where I went wrong?? My code is below.



Any suggestions on how to fix this problem?

Thanks in advance !

S.T.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the setRanks() method, you have for (i=0; i< _ranks.size (); i++). what does _ranks.size() return the first time through the loop, why, and how does that Cause your problem?
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve:

I think I figured one part of the problem. I changed the loop in the setRanks() method from a for to a while. It actually returned the right number this time. However, am still having issues with the setDeck() method...

I am thinking its the same problem. I changed the code a little to see what would happen and I am still getting the same error from the compiler.

Here is the revised code below:



Thanks again to you and everyone in advance.

S.T.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam,

I think you are jumping ahead of yourself, making changes based on guesses rather than understanding the problem. The problem wasn't that you used a for loop, nor is it better to use a while loop with a magic-number-constant. The problem was that you were looking at the size of the wrong collection (you were calling size() on the list you just created instead of looking at the size of the array which was the source of the information).

What you need to do is stop coding for a while. Take a pencil and paper and start writing down the steps you need to perform to do what you want. Don't write in Java, write in plain english. Then make each statement smaller, creating more sentences, each more specific as to exactly how the step should be done. Repeat until the code required to solve the issue is obvious.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic