Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Initilization

 
siva krishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am getting the error "illegal start of expression" while initializing the array at Line 7. Code is given below.

Am i missing any basic rule? Please help.

class ArrayTest {

public static void main(String [] args)

{
int[][] A1D = new int [2][];
byte b = 10;
char c = 'C';
A1D[0] = new int[]{1,2,3};
A1D[1] = {1,2}; //Line 7

int [][] B1D ;
B1D = A1D;

for(int i = 0;i<B1D.length;i++)
for (int j = 0;j<B1D[i].length;j++)
{
System.out.println("element in the array A :" + B1D[i][j]);
}


}

}
 
vydhehi paruchuri
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva:

If you are using array initialize list,{1,2} to initialize an array then array should be declared in the same statement.

AID[1]= {1,2} //will not work causes a compile time error.

int[1] newArray={1,2} //this will work

Hope this helps.

Thanks,

Vydhehi
 
Mahesh Bhatt
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi siva,

When you are using these type of arrays, the syntax you would have to use to initialize your array would have to be something like this :



hope it helps you .
[ September 20, 2004: Message edited by: prashant bhogvan ]
 
Louie van Bommel
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by siva krishnan:

int[][] A1D = new int [2][];
byte b = 10;
char c = 'C';
A1D[0] = new int[]{1,2,3};
A1D[1] = {1,2}; //Line 7


Jls:
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#11358


An array is created by an array creation expression (�15.10) or an array initializer (�10.6).
...
An array initializer may be specified in a declaration, or as part of an array creation expression (�15.10), creating an array and providing some initial values...


An array initializer can only be part of a declaration. You already have a declaration in the first line (of the code) above. That's the declaration. If you want to use an initializer, it has to be in its declaration.

Trying to modify your example by throwing in an "int[]" won't help either:

This will compile:


So you'd think you could apply that to A1D[1] instead, but alas:


The reason in this example is that the compiler is expecting a declaration statement which could have int[]b = {<something>}; or int[][]c = {<something else>};

In your case, the array named A1D has already been declared and can't be declared again.

It's interesting to look at this (which works):


but you have to see that this is not really an initializer. You are creating an anonymous array, and then assigning it to a variable. It's kinda equivalent to:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic