The Artima Developer Community
Sponsored Link

Java Answers Forum
ArrayList problems

1 reply on 1 page. Most recent reply: Mar 17, 2003 2:23 AM by Jiri Fabian

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Jesse Morrison

Posts: 2
Nickname: trueneutra
Registered: Mar, 2003

ArrayList problems Posted: Mar 15, 2003 7:29 PM
Reply to this message Reply
Advertisement
I have a problem using ArrayLists. I am storing scores of games in a class called Game. Each "Game" class holds three scores and a date. The three scores are in an ArrayList.

Each game belongs to a class called Player. Each "Player" has multiple games.

Now when I want to display the scores of any given Player, I get the same result which is "9". 9 is the very first score that is entered into my program. When I take a look at the contents of the ArrayLists, I find that EVERY score ever entered is in one big ArrayList, instead of separated into ArrayLists of three scores each.

Here's an example of my code:

while (st.hasMoreTokens())
{
strTemp = st.nextToken(); //gets a score
scoreArray.add(strTemp); //stores a score into a ScoreArray
}
gameTmp = new Game(dateTmp, scoreArray); //Inserts a date, and the three scores from the ScoreArray
//scoreArray.clear(); //I tried clearing the scoreArray, but that causes my program to crash
playerTmp.addGame(gameTmp); //Adds the new game to a new player


But for some reason, ALL players use only one ArrayList of ALL the scores ever entered. Does anyone see the problem?


Jiri Fabian

Posts: 11
Nickname: filemon
Registered: Mar, 2003

Re: ArrayList problems Posted: Mar 17, 2003 2:23 AM
Reply to this message Reply
Oh, man!

You pass to all of them reference on scoreArray. So all of Game objects share this one instance. Capis?
You must create new list for every player.

fil

Flat View: This topic has 1 reply on 1 page
Topic: trying to update this applet Previous Topic   Next Topic Topic: append doc files

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use