• Post Reply Bookmark Topic Watch Topic
  • New Topic

probleme in array  RSS feed

 
jaysingh solanki
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:
class Pass
{
public static void main(String arg[])
{
int[][] i={{1,2,3},new int[]{3,4,5},{333},{},{6,7,8}};//line1 ok
int[][] i1={{1,2,3},new int[][]{1,2,3},{4,4,3}}; //line2notok
int i2[]={{1,2,3},new int[][]{2,3,4},{4,5,6}};//line3notok
int i3[]={{1,2,3},new int[]{1,2,3}};//linenot5
System.out.println();
}
}
My question is why this three
lines give compile time error
please anyone explain me;
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the compiler error that you're seeing?
I'm moving this to the Beginner's forum...
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaysingh,
When you create a primitive array you can only populate it with those primitives. In your case, you can only place integers into the array (not another array). The first line is valid because the compiler is converting the line for you to make it valid:
int[][] i={{1,2,3},new int[]{3,4,5},{333},{},{6,7,8}};
is actually the same as:
int[][] i = { {1,2,3}, {3,4,5}, {333}, {}, {6,7,8} };
which is populating each value with an integer (valid!).
On the other lines you are trying to place an array in an integer placeholder, which, as you now realize, is not possible.
Regards,
Manfred.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!