Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why is this array code creating error  RSS feed

 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class demov
{
public static void main(String args[])
{
int a[][]=new int[][100];
}
}


what strange error is this??
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:class demov
{
public static void main(String args[])
{
int a[][]=new int[][100];
}
}


what strange error is this??


The error I got was:

Cannot specify an array dimension after an empty dimension

Pretty straight forward, actually. You basically can't say int a[][] = new int[][100], because you can't specifiy the [100] dimension without specificying the dimension of the bracket right before it. Some like int a[][] = new int[3][100] would work.
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class demov
{
public static void main(String args[])
{
int a[][]=new int[100][];
}
}


then how is it runnning successfuly
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:class demov
{
public static void main(String args[])
{
int a[][]=new int[100][];
}
}


then how is it runnning successfuly


Because you have delcared the dimensions on the first bracket. You can do the first dimension, both dimensions, or neither. If it were a 3D array such as inta[][][]=new int[][][] you could put something in the first bracket, first and second bracket, or all three brackets, or none.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to complete the chapter on Arrays that you are reading; believe me a few pages later you would get the answer of your question.
You will also learn or realize the fact that:
1. two dimensional arrays are actually arrays of objects
2. how you can simulate sparse matrices in Java

For your reading:
Arrays
2D Arrays

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!