• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

2 dimensional arrays

 
Ranch Hand
Posts: 821
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I have been spending 2 weeks doing a 2D 90 clockwise rotation and only able to do it after seeing the answer in Stackoverflow.

But, still I am not really able to grasp the concept on a specific part :



The specific part that I don't get it is :



This line of code could appear in the outer or inner loops.

Could someone kindly explain things to me ?

Tks a millioin.
 
Saloon Keeper
Posts: 8578
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Technically, Java doesn't have a 2D array. What it has is an array of arrays. With this it is possible for each of the rows to have a different number of columns. Practically speaking though, you can design your program if you want so that all rows have the same number of columns, or in effect, a 2D array.

Given int[][] grid
grid is an array of arrays
grid[0] is row '0' of the grid which returns int[] which is an array representing one row.
So grid[0].length is the number of columns in row '0', and if in our application all rows have the same length then this length would then be  assumed to hold true for all rows.
 
Bartender
Posts: 4632
182
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An alternative for having these nasty nested for-loops is to use the fact that 2D arrays in java are arrays of arrays.

Suppose you have the methods 'int[] reverse(int[] array)' and 'int[] getColumn(int[][] array, int index)' then you could do a 90 degree CCW rotation as follows:

using the fact that the last column of a becomes the first row, et cetera. And likewise for rotating 180 or 270 degrees or transposing.
 
reply
    Bookmark Topic Watch Topic
  • New Topic