This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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

vijay gardiner
Greenhorn

Joined: Jan 26, 2002
Posts: 14
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
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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


Dave
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
This works.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
vijay g,
Please read http://www.javaranch.com/name.jsp and edit your profile to change your display name. Thanks.
Erik Dark
Ranch Hand

Joined: Jan 28, 2002
Posts: 107
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

Joined: Jul 22, 2000
Posts: 9043
    
  10

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array declaration
 
Similar Threads
Array
Array help
array declaration and creation
Arranging members of array in a ascending order?
multi dimensional arrays