Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array declaration.

 
Kabir Shah
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible in anyway to declare an array like this.

static int arr[]=new int[2];

arr={0,0};
and later on in same program
arr={0,1};

The compiler is giving me error "illegal start of expression".

Now why is this so,
as we can declare array like this
static int arr[]=new int[2];

and then initialize it
arr[0]=0;
arr[1]=0;

then may be at some time of the program reinitialize it to
arr[0]=0;
arr[1]=1;

So when the length of an array is too large, its inconvenient to use the second method.
So how can we use the first method?
 
Kabir Shah
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it, this problem is solved.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first method can only be used when you initialize an array at the same moment you declare it. Any other time you can use something similar - it just needs the type as well:
Instead of the length you specify the elements; the length will be the number of elements.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic