In class A I have a static vector. In class B I need to copy that vector into a separate vector and display it. The static vector changes in my program but the display does not. Is there anyway I can have class B get the new vector? Thanks
Printable View
In class A I have a static vector. In class B I need to copy that vector into a separate vector and display it. The static vector changes in my program but the display does not. Is there anyway I can have class B get the new vector? Thanks
Woah. more information is needed i think in what exactly you're trying to do. If you post for help like that its unlikely people will answer you.
1) why is the Vector static? Why not make it an instance variable?
2) It sounds like you don't really want to copy the data and that's it, you want to listen to the data for changes which suggests using the Observable design pattern. If using Swing, you may want to look into using a ChangeListener here.
Well I am making a card game. I have a static vector that is for the players hand. Then in a separate class I have a method for replacing all of the 11's 12's 13's and 1's with JQKA respectively. I copy the contents of the static vector to a different vector using a for loop. When the static vector changes (the one that is for keeping track of the hand) the display does not change. I know the original vector is changing but I dont think that the second class is getting a the changed vector, it just keeps the old one. Im sorry if that is confusing or too vague. I can post a bit of code if it would help. Thanks a bunch
When I dont have the vectors static i get out of bounds errors. I dont know why but it works like this :confused: What I wanted to do was copy the new data when the vector is changed, when I call the method.
Again, why are you making this a static variable? This sounds foolish at best and dangerous at worst. If you have more than one player, are they all sharing the same hand? Do you really want to throw OOPs out the window here?
Yep, you need to use the Observable pattern here. Again a ChangeListener may be all you need. You can read up on it on the Sun tutorials and the API.Quote:
Then in a separate class I have a method for replacing all of the 11's 12's 13's and 1's with JQKA respectively. I copy the contents of the static vector to a different vector using a for loop. When the static vector changes (the one that is for keeping track of the hand) the display does not change.
Ah, OK, now I see. Fixing a bug with a kluge is not a good reason, I'm afraid. The first thing you need to do is to fix this bug.Quote:
When I dont have the vectors static i get out of bounds errors. I dont know why but it works like this
Could that be because there are more than one vector and some of them are empty.Quote:
When I dont have the vectors static i get out of bounds errors
When its static, there is only one object.
What does your program design require? Can each class have its own version of the vector or do they all need to see the one and only version?
If each can have their own version, then it would be better if the names were unique.
Some actual code would be helpful. Perhaps the topic creator isn't properly instantiating objects that create these problems?
I am making a black jack game. I have two vectors phand for the player and dhand for the dealer. When I start the game both of these vectors are empty. I then use a method in a seperate class to add up totals and replace the facecards for the display. When I do not have the vectors as static, the adding class is passed a blank vector, and when I add cards to the hands the adding class does not see these changes.
so... you've got a bug. Time for some debugging then. I recommend you sprinkle your code with println's to check to see what's going on, either that or use a decent debugger.
I know why it is not passing it the right vector initially. When my adding class is created it gets its own version of each vector, which is blank at the time it gets it. When I try to call the vector later it calls its own version of the vector, which was blank.
Well I got it figured out. I just want to say thanks to all of the great people here and for the quick help they gave me. Godspeed.