Why is My Java Code Not Rotating an Array 90 Degrees to the Left?

  • Context: Java 
  • Thread starter Thread starter physicsfun
  • Start date Start date
  • Tags Tags
    Arrays Java Rotating
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 4K views
physicsfun
Messages
10
Reaction score
0
I am supposed to write code to rotate an array 90 degrees to the left... so my question is, why is this code not rotating my array 90 degrees to the left?

thanks for any help!



public static int[][] rotateLeft(int[][] source) {
int[][] result = new int[source.length][source[0].length];
for (int i = 0; i < result.length; i++) {
for (int k = 0; k < result[0].length; k++) {
result[k] = source[source.length-1-i];}}
return result;}
 
Physics news on Phys.org
It would be helpful if you would tell us what your code turns out to be doing instead of rotating the array 90 degrees to the left.

However, this said:

1. You take in an array of dimensions M x N, and put out an array of dimensions M x N. If it was rotated wouldn't it be N x M?

2. Just look at this line of code:

result[k] = source[source.length-1-i];

What does this do? Why does i appear three times and k just one?