1

so I'm trying to make a program that rotates a cube shaped grid 90 degree to the right.

The problem I encountered is that temp values (temp1, temp2, temp3, temp4) that I declared in the method keep changes during the process of method.

Assume there is a cube grid,

1 2 3
4 5 6
7 8 9

as I declared int[] temp1 = grid[0]; in the method

temp1 should be fixed as {1, 2, 3}.

However, when this part of code executes,

 //rotate 90 degree to right
        //row 0 ==
        for (int i = 0; i < grid[0].length; i++)
        {
            grid[0][i] = temp2[i];
        }

temp1's value changes as {7, 4, 1} which I didn't not expect

It is really confusing because temp value usually don't change.

Can anyone give me advice to fix this issue?

import java.util.Scanner;
public class a
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int numLine = Integer.parseInt(input.nextLine());
        int[][] grid = new int[numLine][numLine];
        String[] lines = null;
        for (int i = 0; i < numLine; i++)
        {
            lines = input.nextLine().split(" ");
            for (int j = 0; j < numLine; j++)
            {
                grid[i][j] = Integer.parseInt(lines[j]);
            }
        }
        rotate(grid);
        for (int[] i : grid)
        {
            for (int j : i)
            {
                System.out.print(j + " ");
            }
            System.out.println("");
        }
        input.close();
    }   
    public static void rotate(int[][] grid)
    {
        //row 0
        int[] temp1 = grid[0];

        //column 0
        int[] temp2 = new int[grid[0].length];
        int c = 0;
        for (int i = grid[0].length-1; i >= 0; i--)
        {
            temp2[c] = grid[i][0];
            c++;
        }
        c = 0;
        
        //row last
        int[] temp3 = grid[grid[0].length-1];
        
        //column last
        int[] temp4 = new int[grid[0].length];
        for (int i = grid[0].length-1; i >= 0; i--)
        {
            temp4[c] = grid[i][grid[0].length-1];
            c++;
        }
        c = 0;
        
        //rotate 90 degree to right
        //row 0 ==
        for (int i = 0; i < grid[0].length; i++)
        {
            grid[0][i] = temp2[i];
        }
        
        //column last ==
        for (int i = 0; i < grid[0].length; i++)
        {
            grid[i][grid[0].length-1] = temp1[i];
        }
        
        //row last ==
        for (int i = 0; i < grid[0].length; i++)
        {
            grid[grid[0].length-1][i] = temp4[i];
        }
        
        //column 0 ==
        for (int i = 0; i < grid[0].length; i++)
        {
            grid[i][0] = temp3[i];
        }
    }
}
Ryn
  • 21
  • 2
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Feb 15 '21 at 06:19
  • The expressions `temp1` and `grid[0]` reference the same array. If you want them to change independently see [Arrays.copyOf](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#copyOf(T%5B%5D,int)). – Piotr P. Karwasz Feb 15 '21 at 06:44
  • You're assigning values to `grid[0][i]`, which is assigning to subvalues of `grid[0]`, which is what you have `temp1` pointing to. So of course `temp1` is going to change. UPDATE: Only saw @PiotrP.Karwasz's comment after posting mine. He's saying the same thing. I agree with him...you want to be making a copy of `grid[0]` and assigning that to `temp1` so that it isn't changed by your later logic. – CryptoFool Feb 15 '21 at 06:47

0 Answers0