Solved Storing 3 columns in Java?

Discussion in 'Plugin Development' started by Wizardo367, Aug 31, 2013.

Thread Status:
Not open for further replies.
  1. Hi, I'm simply wondering what would be the best way to store a player, the block they've broken and an integer in an array or some sort of object. Any help is appreciated. :D
     
  2. Offline

    adam753

    Well, a HashMap can store two columns (it sounds like you already knew this), but three is a bit harder. There are a few solutions for this, but my favourite is using a "wrapper class", like so:
    Code:
    public class myWrapper {
        public Block block;
        public int integer;
    }
     
    //When declaring the HashMap:
    public HashMap<String, myWrapper> hashmap;
    
    That way, you can store the player's name on the left column, and the Block and integer in the right column. (Note: I didn't write this in an IDE, so there might be some missing keywords or something, but you get the idea.)

    Incidentally, notice how I've used a String object rather than a Player object in the HashMap. You should never store Player objects because it can cause memory leaks. Instead, you should store their name, which is much more elegant.
     
    Wizardo367 likes this.
  3. Offline

    kbunkrams97

    adam753 With a wrapper how is it expected to get block and integer out of the myWrapper. Wouldn't you have to make an object and have return methods inside like:
    Code:java
    1. public class Custom
    2. {
    3. private Block block;
    4. private int integer;
    5.  
    6. public Custom(Block b, int i)
    7. {
    8. block = b;
    9. integer = i;
    10. }
    11.  
    12. public Block getBlock()
    13. {
    14. return block;
    15. }
    16.  
    17. public int getInt()
    18. {
    19. return integer;
    20. }
    21. }

    I have never used a wrapper before and these are just assumptions. If I am wrong feel free to explain it a little more for me.
     
  4. Offline

    bobacadodl

    Either way works :) Although the way you are doing it is preferred to prevent unwanted variable access, it doesnt matter in a simple situation like this.

    In the first example, you can just do myWrapperObject.block to get the block.
    In your example, you can't do that since the variables are private. So instead you do myWrapperObject.getBlock()
     
    kbunkrams97 likes this.
  5. Offline

    kbunkrams97

    bobacadodl I see that now. For some reason before I thought the variables were private but thank you for you explanation.
     
  6. Offline

    rsod

    why use get..() set..() if you can just make variables public?
     
  7. Offline

    bobacadodl

    Here, I'll quote someone else. As I can't think of a good way to explain it :)

     
    rsod likes this.
  8. Hmm i'll give it a go thanks, also I noticed your signature is "If I have solved your problem, I would ask that you turn on the "solved" tag in your thread title." it annoys me when people don't do that as well. :mad:


    I ended up using the wrapper to store all 3 columns and used an ArrayList to store the object, I couldn't use a HashMap because i needed to use all 3 columns. Thanks again for your help :D , I baked you a cake to express my gratitude. [cake]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
    adam753 likes this.
Thread Status:
Not open for further replies.

Share This Page