This discussion is archived
3 Replies Latest reply: Jan 28, 2007 6:49 PM by 807607 RSS

Array of arrays

807607 Newbie
Currently Being Moderated
New to java, trying to make an array of an array of ints
Error for semicolon on first line of code:
Syntax error on token ";", { expected after this token
 int [][] puzzles = new int[9][];
       puzzles[0] = new int[] {"0", "3", "7", "2", "0"};
       puzzles[1] = new int[] {"0", "0", "1", "0", "6"};
       puzzles[2] = new int[] {"7", "0", "1", "0", "0"};
       puzzles[3] = new int[] {"0", "3", "0", "0", "0"};
       puzzles[4] = new int[] {"1", "0", "8", "0", "0"};
       puzzles[5] = new int[] {"0", "0", "0", "0", "0"};
       puzzles[6] = new int[] {"0", "0", "8", "0", "0"};
       puzzles[7] = new int[] {"0", "9", "0", "0", "2"};
       puzzles[8] = new int[] {"1", "0", "8", "6", "0"};
  • 1. Re: Array of arrays
    796440 Guru
    Currently Being Moderated
    Those assignment lines--anything but declarations--have to be inside a method, constructor, or initializer block.


    Also, "0," etc., are not ints. They're Strings. 0 (no quotes) is an int.
  • 2. Re: Array of arrays
    796254 Newbie
    Currently Being Moderated
    Well, for starters you're adding Strings into a matrix of ints. That doesn't seem very smart.

    This works nicely:
    package cruft;
    
    public class ArrayOfArrays
    {
       public static void main(String[] args)
       {
          int [][] puzzles =
          {
             {0, 3, 7, 2, 0},
             {0, 0, 1, 0, 6},
             {7, 0, 1, 0, 0},
             {0, 3, 0, 0, 0},
             {1, 0, 8, 0, 0},
             {0, 0, 0, 0, 0},
             {0, 0, 8, 0, 0},
             {0, 9, 0, 0, 2},
             {1, 0, 8, 6, 0},   
          };
    
          for (int i = 0; i < puzzles.length; ++i)
          {
             for (int j = 0; j < puzzles.length; ++j)
    {
    System.out.print(puzzles[i][j] + ",");
    }
    System.out.println();
    }
    }
    }


    %
  • 3. Re: Array of arrays
    807607 Newbie
    Currently Being Moderated
    thanks for the help!