• 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Initializing a 2D array

 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • 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???
 
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.
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Netbeans IDE Chrome Java
  • 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..
 
Sheriff
Posts: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Marshal
Posts: 64666
225
  • 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]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!