This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Arrays of Arrays  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody explain this arrrays of arrays, i am getting confused with the nested for loops,
and what should i know in arrays of arrays as am preparing for the SCJP
class arr {
public static void main (String args[]) {
int twoD[][] = new int[2][3];
int i, j, k = 0;
for(i=0; i<2; i++)
for(j=0; j<3; j++) {
twoD[i][j] = k;
k++;
System.out.println(twoD[i][j]);
}
}
}
Thank you very much
 
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case we have a bi-dimensional array. It is kind of like a matrix 2x3 (2 rows - 3 columns).
int twoD[][] = new int[2][3];
The nested for loops are needed in order to access each cell of the matrix.
for(int i=0;i<2;i++) will traverse the first and second row.
for(int j=0;j<3;j++) will traverse the first, the second and the third column.
Each cell is assigned k (incremented at each iteration). The content of the bi-dimensional array after its initialization is:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!