1

I need to create 2d array with 5 rows and 6 columns in the first row and then the rest is 5 columns

so that would look like this

{ 0 0 0 0 0 0
  0 0 0 0 0 
  0 0 0 0 0 
  0 0 0 0 0
  0 0 0 0 0   } 

is there any way to do so?

as of now i have only this, it creates 6 rows and 5 columns:

private static JButton[][] b = new JButton[6][5];

updtae: I am using Java for this.

6
  • 1
    You might want to tag this question for the language you're using Commented Apr 16, 2014 at 0:09
  • 1
    What's the "use case" for this? It might logically lend itself to two arrays: one that is 1x6, and another that's 5x5. Managing it may be simpler as well. Commented Apr 16, 2014 at 0:11
  • well, it will be simpler to manage one array in my case. Commented Apr 16, 2014 at 0:15
  • @lurker Actually the other would be 4x5, not 5x5. But I agree with the idea of separation. Commented Apr 16, 2014 at 0:30
  • @Joffrey yes, sorry, I miscounted. :p Commented Apr 16, 2014 at 1:31

4 Answers 4

2

You can initialize your array in 2 steps:

// initialize the first dimension
private static JButton[][] b = new JButton[5][];

// initialize the second dimension
static {
    b[0] = new JButton[6];
    for (int i = 1; i < b.length; i++) {
        b[i] = new JButton[5];
    }
}

When you initialize only the first dimension, it creates an array of rows, in which each row is null.

During the second step, you create each row of the desired length.

Sign up to request clarification or add additional context in comments.

Comments

0

This isn't possible.

You can however, create a 6x6 array and not use the 6th column in every row you don't want to.

{ 0 0 0 0 0 0
  0 0 0 0 0 null
  0 0 0 0 0 null
  0 0 0 0 0 null
  0 0 0 0 0 null  }

1 Comment

Yes it is possible. Look at my answer.
0

Would something like this suit?

JButton[][] s = new JButton[5][];
for (int i = 0; i< 5; i++){
    if (i == 0) {
        s[i] = new JButton[6];
    } else {
        s[i] = new JButton[5];
    }
}

3 Comments

Both branches of that if are doing the same thing.
You did not consider the static context of the variable. Moreover, there is a typo where you probably meant 5 instead of 6.
Fixed for typo but the other answer deals with the static context.
0

I actually figured it out. Here is my way of doing it:

private static JButton[][] b = {{new JButton(), new JButton() , new JButton() , new JButton() , new JButton() , new JButton() },
        {new JButton(), new JButton() , new JButton() , new JButton() , new JButton()}, 
        {new JButton(), new JButton() , new JButton() , new JButton() , new JButton()},
        {new JButton(), new JButton() , new JButton() , new JButton() , new JButton()},
        {new JButton(), new JButton() , new JButton() , new JButton() , new JButton()}};

and then I just add them to a panel

for (int i = 0; i < b.length; i++) {
            for (int j = 0; j <b[i].length; j++) {

                panel.add(b[i][j]);

           }

        } 

1 Comment

"your way" is really painful to use, what if you had a 256*256 array? Copy paste?

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.