1

I am working on a small game where the player clicks a button whichs value is from a 2-dimensional Array.This is how the game looks In case the player wants to revert his action I want to save the steps the player took in a List, and when he clicks the "revert"-button the last item of the list gets reverted. Problem is I don't know how to the save the index of a 2-dimensional Array to a list.

My code is far for the array is following

for (int i = 0; i < buttons.length; i++) {
        for (int j = 0; j < buttons[i].length; j++) {
            counter++;
            buttons[i][j] = new JButton("" + counter);

            buttons[i][j].setBackground(Color.white);
            buttons[i][j].addActionListener(new ButtonPress());
            fenster.add(buttons[i][j]);

And the code for the ActionListener is here:

public void actionPerformed(ActionEvent e) {

        amountMoves++;
        moves.setText("Moves: " + amountMoves);

        int x = 0;
        int y = 0;
        for (int i = 0; i < buttons.length; i++) {
            for (int j = 0; j < buttons[i].length; j++) {
                if (((JButton) e.getSource()).equals(buttons[i][j])) {
                    x = i; 
                    y = j;
                    break;
                }
            }
        }
    }

Lists aren't my strongpoint so so far I only have this:

    ArrayList<Integer> steps = new ArrayList<Integer>();

I know I can add a element with steps.add() and delete the last element with steps.remove(steps.size() - 1);, but that's all so far

Any help is greatly appreciated

EDIT: With the help of @Johnny Mopp and @SSP I figured out what to do. First I created a List of the type String ArrayList<String> steps = new ArrayList<String>(); (you can alternativly use a List of the type JButton) and add it in the Actionlister with steps.add(buttons[i][j].getText());

4
  • By reverted, do you mean that the last item in the List gets deleted, or that it becomes equal to the one before it? Commented Sep 24, 2019 at 19:20
  • I mean the last item of the list gets deleted Commented Sep 24, 2019 at 19:25
  • There are lots of options. You might store the actual button (ArrayList<Button>)? Or use an array of Pair..... Commented Sep 24, 2019 at 19:28
  • That helped me, I figured it out, big thanks Commented Sep 24, 2019 at 19:36

1 Answer 1

1

create a Lists of list :-

ArrayList<ArrayList<Integer>> steps = new ArrayList<>();

if you get row 2 and column 2 data to be saved, Save value like below:-

steps.get(1).add(1{index of column},2{value to be stored});

If you are adding element for first time, You need to instantiate object like below:-

if(steps.get(1) == null){
    steps.get(1) = new ArrayList<>();
}

Accessing data

 steps.get(1).get(2);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.