Home > Software design >  Change value of array without overwriting the old array
Change value of array without overwriting the old array

Time:04-05

Here i am trying to rotate the array to 90deg clockwise, I assigned the value of 'mat' to 'new array'(line:9). When i am trying to overwrite the value of 'new array' (line:13) the value of the 'mat' array is also getting overwritten resulting changing of the actual value and giving error on the result , suggest me some edits to stop overwriting of the 'mat' array

class maxpop {
public static void main(String[] args) {
    Rotation();
}
static void Rotation(){
    int[][] mat = {{0,0,0},{1,1,1},{2,2,2}};
    int[][] newarray = new int[mat.length][mat.length];
    int p;
    newarray=mat;
    for (int i = 0; i < newarray.length; i  ) {
        p=mat.length-1;
        for (int j = 0; j <newarray.length; j  ,p--) {
            newarray[i][j] = newarray[p][i];
        }
        
    }

    for (int i = 0; i < newarray.length; i  ) {
        for (int j = 0; j < newarray.length; j  ) {
            System.out.print(newarray[i][j]);
        }
        //p--;
        System.out.println();
    }}}

CodePudding user response:

when you are doing newarray=mat both array is pointing the same object , change in one will alter the other to stop overwriting the mat array and to rotate 90deg

class maxpop {
public static void main(String[] args) {
    Rotation();
}
static void Rotation(){
    int[][] mat = {{0,0,0},{1,1,1},{2,2,2}};
    int[][] newarray = new int[mat.length][mat.length];
    int p;
    int k=0;
 while (k<no of times you want to rotate) {
    for (int i = 0; i < newarray.length; i  ) {
        p =mat.length-1;
        for (int j = 0; j <newarray.length; j  ,p--) {
            newarray[i][j] = mat[p][i];
            System.out.print(newarray[i][j]);
        } 
        System.out.println();   
    }
   int[][] newarrray_2=new int[newarray.length] 
   [newarray.length];/*everytime the loop runs it will create 
    newobject with 0 
    values, if you don't create a new array each time 
    newarry,newarray_2 
    will 
   point the same object,and newarray will not get initialised to 
   0 later*/
   mat=newarray;//mat will get overwrite 
   newarray=newarrray_2;/*a new 0 value will be initialised to 
    new array*/
   k  ;
}
}}

CodePudding user response:

In this code:

int[][] mat = {{0,0,0},{1,1,1},{2,2,2}};   // (A)
int[][] newarray = new int[mat.length][mat.length];   // (B)
int p;
newarray=mat;

The line newarray=mat; assigns a reference to the old array to the newarray variable. The fresh array you created and assigned to newarray in (B) will no longer be accessible. Both mat and newarray will point to the same array as declared in (A). You need to remove that line. You also need to copy from the mat array.

public class MaxPop {

    public static void main(String[] args) {
        rotation();
    }

    static void rotation() {
        int[][] mat = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 } };
        int[][] newarray = new int[mat.length][mat.length];
        int p;

        for (int i = 0; i < newarray.length; i  ) {
            p = mat.length - 1;
            for (int j = 0; j < newarray.length; j  , p--) {
                newarray[i][j] = mat[p][i];
            }

        }

        for (int i = 0; i < newarray.length; i  ) {
            for (int j = 0; j < newarray.length; j  ) {
                System.out.print(newarray[i][j]);
            }
            System.out.println();
        }
        System.out.println();

        for (int i = 0; i < mat.length; i  ) {
            for (int j = 0; j < mat.length; j  ) {
                System.out.print(mat[i][j]);
            }
            System.out.println();
        }
    }
}

Prints:

210
210
210

000
111
222

  • Related