77

I am trying to initialize a 2D array, in which the type of each element is char. So far, I can only initialize this array in the follow way.

public class ticTacToe 
{
private char[][] table;

public ticTacToe()
{
    table[0][0] = '1';
    table[0][1] = '2';
    table[0][2] = '3';
    table[1][0] = '4';
    table[1][1] = '5';
    table[1][2] = '6';
    table[2][0] = '7';
    table[2][1] = '8';
    table[2][2] = '9';
}
}

I think if the array is 10*10, it is the trivial way. Is there any efficient way to do that?

0

5 Answers 5

265

Shorter way is do it as follows:

private char[][] table = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
5
  • 1
    No, this way is not working in Eclipse. it warms array constants can only be used in initializers
    – Justin
    Dec 12, 2012 at 4:50
  • 2
    Fixed it. It has go in the declaration statement. Dec 12, 2012 at 5:00
  • 3
    NB : to complete the answer, when refering table[i][j] : i refer to the first array level and j the second level, zero based .. for instance in current example table[2][0] refer to '7' Aug 16, 2013 at 8:06
  • Thats what, I need!!
    – Arsal Imam
    Jun 16, 2018 at 6:00
  • I would break after 3 and 6, making it look like a matrix. Nov 3, 2018 at 2:12
29

How about something like this:

for (int row = 0; row < 3; row ++)
    for (int col = 0; col < 3; col++)
        table[row][col] = (char) ('1' + row * 3 + col);

The following complete Java program:

class Test {
    public static void main(String[] args) {
        char[][] table = new char[3][3];
        for (int row = 0; row < 3; row ++)
            for (int col = 0; col < 3; col++)
                table[row][col] = (char) ('1' + row * 3 + col);

        for (int row = 0; row < 3; row ++)
            for (int col = 0; col < 3; col++)
                System.out.println (table[row][col]);
    }
}

outputs:

1
2
3
4
5
6
7
8
9

This works because the digits in Unicode are consecutive starting at \u0030 (which is what you get from '0').

The expression '1' + row * 3 + col (where you vary row and col between 0 and 2 inclusive) simply gives you a character from 1 to 9.

Obviously, this won't give you the character 10 (since that's two characters) if you go further but it works just fine for the 3x3 case. You would have to change the method of generating the array contents at that point such as with something like:

String[][] table = new String[5][5];
for (int row = 0; row < 5; row ++)
    for (int col = 0; col < 5; col++)
        table[row][col] = String.format("%d", row * 5 + col + 1);
1
  • 1
    thanks! it's working, but why "(char) ('1' + row * 3 + col)" works?
    – Justin
    Dec 12, 2012 at 4:57
15

Easy to read/type.

  table = new char[][] {
      "0123456789".toCharArray()
    , "abcdefghij".toCharArray()
  };
6

You can use for loop if you really want to.

char table[][] table = new char[row][col];
for(int i = 0; i < row * col ; ++i){
     table[i/row][i % col] = char('a' + (i+1));
}

or do what bhesh said.

1
  • i think there is an mismatch error, cannot assign int to type of char array
    – Justin
    Dec 12, 2012 at 4:53
2

You can follow what paxdiablo(on Dec '12) suggested for an automated, more versatile approach:

for (int row = 0; row < 3; row ++)
for (int col = 0; col < 3; col++)
    table[row][col] = (char) ('1' + row * 3 + col);

In terms of efficiency, it depends on the scale of your implementation. If it is to simply initialize a 2D array to values 0-9, it would be much easier to just define, declare and initialize within the same statement like this: private char[][] table = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

Or if you're planning to expand the algorithm, the previous code would prove more, efficient.

Not the answer you're looking for? Browse other questions tagged or ask your own question.