Initialize 2D array

前端 未结 5 903
我寻月下人不归
我寻月下人不归 2020-12-02 16:33

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.



        
相关标签:
5条回答
  • 2020-12-02 16:51

    Shorter way is do it as follows:

    private char[][] table = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
    
    0 讨论(0)
  • 2020-12-02 16:51

    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.

    0 讨论(0)
  • 2020-12-02 16:58

    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);
    
    0 讨论(0)
  • 2020-12-02 17:10

    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.

    0 讨论(0)
  • 2020-12-02 17:13

    Easy to read/type.

      table = new char[][] {
          "0123456789".toCharArray()
        , "abcdefghij".toCharArray()
      };
    
    0 讨论(0)
提交回复
热议问题