0

Is it possible to call 2D array in a method?

JButton[][] buttons = new JButton[3][3];
int number = 0;

for (int i = 0; i < buttons.length; i++) {
    for (int j = 0; j < buttons.length; j++){
        buttons[i][j] = new JButton(); // create buttons
        frame.add(buttons[i][j]);
        buttons[i][j].setText(".");
        buttons[i][j].setName("button" + number);
        number++;
    }
}

I have tried this but it isn't working.

private void test(buttons[i][j]) {

}

edited: And also, would I need to use the same way to call all the buttons in other places, say for example I have this.

JButton[] button = {//call the array here of buttons here //};
    Random rand = new Random();
    JButton newbutton = button[rand.nextInt(button.length)];

I want to call all the buttons in the parameter of the button variable. the piece of code above will select one button at random by number of given buttons. for example, if i have;

JButton[] button = {button1, button2, button3, button4, button5, button6};

given the code above, it will select from one of the buttons in the list and then put the chosen button in the variable newbutton. but since I have used an array, I can't have button1, button2, button and so on in the random selection. So I was wondering if it is possible to somehow call the array inside this array of button. I have tried this but it doesn't work.

JButton[] button = {buttons[i][j]};

4 Answers 4

2

The correct signature would be:

 public void doSomething( JButton[][] myArray )
 {
        //doSomething
 }

Passing any variable with that signature will work throughout your code.

To get one of the JButton array's out, you would

 JButton[][] doubleArray = new JButton[1][1];
 JButton[] singleArray = doubleArray[0];
Sign up to request clarification or add additional context in comments.

Comments

1

You can call as:

test(new JButton[3][3]);

And your methods will looks like this:

private void test(JButton[][] buttons){...}
  • Updated

your method will be the following:

JButton getRandomButton(JButton[] buttons) {
    Random rand = new Random();
    return buttons[rand.nextInt(buttons.length)];
}

And if you wand the 2D array above, you can use it as following:

for (int i = 0; i < buttons.length; i++) {
    JButton randomButton = getRandomButton(buttons[i]);
}

8 Comments

I think about it, but don't understand what you want exactly. Can you describe your added part more?
I have added more detail, hope it explains it better.
Ye, I think that should work. one question though, so when I call the method, I will need to replace JButton[] buttons with my original array of button, however, when I put in as parameter the original array it gives me an error.
Why? can provide error itself, what he wants? Is the problem in difference of parameters buttons and button?
Sorry, I was doing it wrong, I had instead of [3][3], [i][j]. is it possible to have your example above with no parameters?
|
1
private void test(JButton[][] buttons){
}

receives a 2D array such as

test(buttons)

and

private void test(JButton button){
}

receive a JButton such as

test(buttons[i][j])

and

private void test(JButton[] buttons){
}

receives a 1D array such as

test(buttons[i])

Comments

0

It would be: private void test(JButton[][] button)

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.