This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Array declaration  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've a pgm like :
class Example {
public static void main(String args[]) {
int a[][] = new int[5][5];
a[0][] = {1,2,3,4,5};
for (int i=0,j=0;i<5;++i,++j)
System.out.println(a[i][j]);
}
}
when i compile I get an illegal start of expression.....why is that?
I've declared int a[][] = new int [5][5]; (a 2 dimensional array)
then i've initialised the first row of the array as {1,2,3,4,5}, i don't understand why there is any error in the above statment.
Could anyone please clarify.
Thanx,
Vijay
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay
When you create an array you can either use the form new int a[] = new int[5]
or the form
a[] = {1,2,3,4,5};
The first form explicitly creates an array of ints that has five elements. The second way implicitly does the same thing. The problem with your example is that your trying to recreate the same array. Once you've declared the size of the array the only way to get data into it is to specifically access each element and put a value into it.
hope that helps
 
Sheriff
Posts: 9094
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works.
 
Marilyn de Queiroz
Sheriff
Posts: 9094
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijay g,
Please read http://www.javaranch.com/name.jsp and edit your profile to change your display name. Thanks.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Marilyn can't get your code working.
This is my suggestion to populate and (optionally) print it:

Erik Dark
[ January 28, 2002: Message edited by: Erik Dark ]
[ January 28, 2002: Message edited by: Erik Dark ]
 
Marilyn de Queiroz
Sheriff
Posts: 9094
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hmmmm. When I run this I get:

1
java.lang.NullPointerException
at Example1.main(Example1.java:9)
Exception in thread "main"

********* reason ********

When i=0 and j=0, the output is one
After i is incremented to 1 and j is incremented to 1, since anything after a[0] is undefined (due to the empty bracket in the declaration -- new int[5][] ), you get a NullPointerException.

Now if you created the array as:
int a[][] = new int[5][5];
you would no longer get an exception because all the elements would be initialized (to zeros).

The point I was making is that you no longer get an illegal start of expression if you declare the array correctly.
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!