File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 19693
    
  20

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.