• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializing a 2D array  RSS feed

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initializing a 1D array as below woks fine.


but, a 2D array,which is actually supposed to be an array of arrays is not accepting the same type of assignment for its elements(1D arrays)


Does anyone know why???
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just guessing here, but try it without the indices on the right side of the assignments - that looks wrong.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When you declare an array, you can specify the size, or you can provide a list of the elements. You can't do both.

Henry
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah...removing that works!!
I must have watched that!
Oh.. specifying both the initializing length and the elements is not allowed?! Yeah anyways, if we know the elements then why do we need to specify the size at all!

Hey..thanks..
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also initialize the entire array of arrays in one statement:
You can also add "new int[][]" just after the = but it's not necessary.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Pedantic mode]There is no such thing as a 2D array in Java. Only an array of arrays.[/Pedantic mode]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!