简体   繁体   中英

Java - Keeping data in ArrayList and HashMap between classes without using static

Hello! I'm trying to learn Java coming from PHP, and I'm trying to make a program which needs to share information stored in an ArrayList and HashMaps between classes.

This is how I declare them:

public static List<String> board = new ArrayList<String>();
public static HashMap<String, Boolean> usedPawns = new HashMap<String, Boolean>();
public static HashMap<String, String> boardValues = new HashMap<String, String>();

Now if I remove static the moment I create a new instance the data will be gone from the array's so the following happens:

/*
 * Constructor
 */
public Board() {
    this.usedPawns();
    this.boardValues();
}

private void usedPawns() {
    if (usedPawns.isEmpty()) {
        for (String letter : this.letters) {
            Board.usedPawns.put(letter, false);
        }
    }
}

private void boardValues() {
    if (boardValues.isEmpty()) {
        for (String letter : this.letters) {
            Board.boardValues.put(letter, letter);
        }
    }
}

And my ArrayList and HashMap reset.

How can I keep the data between my classes without using the static keyword? Please explain your answer like I'm 5. If you need more information please feel free to ask!

You can create an object to share the data:

public class Shared {

    public List<String> board = new ArrayList<String>();
    public HashMap<String, String> boardValues = new HashMap<String, String>();

    public List<String> getBoard() {return board;}
    public HashMap<String, String> getBoardValues() {return boardValues;}

    // instantiate the shared object in the main method
    public static void main(String[] args) {
        Shared shared = new Shared();
        List<String> mySharedList = shared.getBoard();
        Map<String, String> mySharedMap = shared.getBoardValues();
    }
}

Static information is information all instances of the class can access. If it is not static, each instance has it own objects.

Or better to say, it own references. This means you can have shared information between your instances without static like this:

public Board(Board b) {
    this.board = b.board;
    this.usedPawns = b.usedPawns;
    this.boardValues = b.boardValues;
}

Then when you change a list in one board, you change them all.

If on the other hand you want independent instances, try

public Board(Board b) {
    this.board = new ArrayList<String>(b.board);
    this.usedPawns = new HashMap<String, Boolean>(b.usedPawns);
    this.boardValues = new HashMap<String, String>(b.boardValues);
}

Then when you change one instance (by calling usedPawns() for example) others will not change.

EDIT: Of course you can keep your first constructor. Then you can instantiate them like this:

Board first = new Board();
Board second = new Board(first);
Board third = new Board(second); // could also be Board(first)
Board forth = new Board(first); // could also be Board(second) or Board(third)

By the way, if you do something like

...
usedPawns = null;
...

in the Board class (ie assign other lists or maps), the boards instantiated from this one won't change at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM