• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2D Arrays

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I came across this question.
Which of the following are legal declarations of a two-dimensional array of integers?
a) int[5][5]a = new int[][];
b) int a = new int[5,5];
c) int[]a[] = new int[5][5];
d) int[][]a = new[5]int[5];
From what I've learnt, the syntax for declaring a 2-D array is:
int[][] twoDArray = new int[5][6];
None of the given choices fits the bill. Is there something wrong with the question or are there alternative ways to declare a 2-D array? Appreciate your help.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your example is correct, as well as the answer C, those are the ways of expressing different dimensions of arrays.
 
Gaia Nathan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So both:
int[][] twoDArray = new int[5][3];
int[] twoDArray[] = new int[5][3];
are correct ways of declaring a 2-D array. Thanks Larry.
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic