jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes array declaration. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array declaration." Watch "array declaration." New topic
Author

array declaration.

Kabir Shah
Ranch Hand

Joined: Aug 04, 2009
Posts: 125
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

Joined: Aug 04, 2009
Posts: 125
I got it, this problem is solved.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array declaration.
 
Similar Threads
Question about multidimensional arrays
Simple problem
Difference between array of references and single reference
2 dimensional array
array assignment