Results 1 to 13 of 13
  1. #1
    calicocal is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default Static Vector Problem

    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

  2. #2
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    calicocal is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    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

  5. #5
    calicocal is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by calicocal View Post
    Well I am making a card game. I have a static vector that is for the players hand.
    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?

    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.
    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    When I dont have the vectors static i get out of bounds errors. I dont know why but it works like this
    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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    When I dont have the vectors static i get out of bounds errors
    Could that be because there are more than one vector and some of them are empty.
    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.

  9. #9
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    Some actual code would be helpful. Perhaps the topic creator isn't properly instantiating objects that create these problems?

  10. #10
    calicocal is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    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.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  12. #12
    calicocal is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    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.

  13. #13
    calicocal is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    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.

Similar Threads

  1. Vector problem
    By Ace_Of_John in forum New To Java
    Replies: 1
    Last Post: 01-27-2008, 08:53 PM
  2. vector problem
    By mambo_jumbo in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 10:44 PM
  3. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  4. Replies: 1
    Last Post: 08-01-2007, 09:25 PM
  5. Replies: 1
    Last Post: 07-16-2007, 04:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •