• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array declaration.

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it, this problem is solved.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic