• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

probleme in array

Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
My question is why this three
lines give compile time error
please anyone explain me;
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...
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Don't listen to Steve. Just read this tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic