Java-static arrays

29.3k Views Asked by At

i have a class called (User) and i want to make a multidimensional array of it i have wrote

static User [][] userlist=new User[6][];

and i have a compiler error that is : illegal start of expression

thanks a lot .

4

There are 4 best solutions below

5
Mark Peters On BEST ANSWER

Here's an example of a 5x5 2-dimensional array:

private static int[][] matrix = new int[5][5];

//set index 1, 2 to 5
matrix[1][2] = 5;

The static part really makes no difference; just declare the member as static.

3
Jigar Joshi On
static int[][] arr = new int[2][4] ;
arr[0][0]=1;
arr[0][1]=2;
.
.
arr[0][3]=4;
0
Peter Lawrey On

Similar to @Mark solution, you can initialise a multi-dimensional array

private static int[][] matrix = {
    { 1,2,3,4,5 },
    { 6,7,8,9,10 }
};
0
AudioBubble On

There are a number of nice answers, so this is more informative than anything:

The problem is that all dimensions must be specified explicitly or implicitly -- and the new X[..][..][..] etc, (explicitly specified dimensions) form requires that each dimension (..) is a (non-negative) integer expression. The javac compiler is throwing that error because it found the last ] but was expecting an integer expression.

new User[6][/*you need an integer expression here*/];

Happy coding.