简体   繁体   中英

Java copy array - reference

I have a array int[3][3] with this initial position:

(A)

block[0][0] = 1;
block[0][1] = 1;
block[0][2] = 1;
block[1][0] = 1;
block[1][1] = 1;
block[1][2] = 1;
block[2][0] = 1;
block[2][1] = 0;
block[2][2] = 1;

//Visually
1 1 1
1 1 1
1 0 1

I have a method that takes the "0" element and change with the upper element, so the next state is:

(B)

1 1 1
1 0 1
1 1 1

But the problem is, I want to create a NEW array (called aux) with this positions and DO NOT OVERIDE the block array (A)

Here is my code:

//Creating array B
int[][] aux = new int[3][3];
aux = block; //load the values of original array
aux[line][row] = aux[line-1][row];
aux[line-1][row] = 0;

But when I check the ouput, I have also overridden the original array (block).

So I have the array BLOCK and the array AUX with the same values, and DO NOT kept the array BLOCK with the original data.

===========================================================================

EDIT:

I tried to use the clone and copyOf functions, but the result also affected the original array. Here is my code:

System.out.println("INITIAL BLOCK VALUES");
            System.out.println(
                            bloco[0][0] +""+
                            bloco[0][1] +""+
                            bloco[0][2] +
                            "\n"+
                            bloco[1][0]+
                            bloco[1][1]+
                            bloco[1][2]+
                            "\n"+
                            bloco[2][0] +
                            bloco[2][1] +
                            bloco[2][2]
                    );



            //Cloning original array to a new one
            int[][] aux = bloco.clone();

            //Here what I'm doing is changing the first element of AUX, just for test
            aux[0][0] = 9;




            // Block array shouldnt be affected! Lets check it out:

            System.out.println("FINAL BLOCK VALUES");
            System.out.println(
                                    bloco[0][0] +""+
                            bloco[0][1] +""+
                            bloco[0][2] +
                            "\n"+
                            bloco[1][0]+
                            bloco[1][1]+
                            bloco[1][2]+
                            "\n"+
                            bloco[2][0] +
                            bloco[2][1] +
                            bloco[2][2]
                    );

And the OUPUT is:

INITIAL BLOCK VALUES
111
111
101
FINAL BLOCK VALUES
911
111
101

any thoughts?

OBS: This code is inside a function of a class called "Node" which contructor is:

public Nodo( int[][] blocos )
        {
            this.bloco = blocos;
        }

This Program show The original array is copy into new array without changing original array values

 class Array
 {
public static void main (String[] args) {

   int[][] block = new int[3][3];
 //load the values of original array

  block[0][0] = 1;
  block[0][1] = 1;
  block[0][2] = 1;
  block[1][0] = 1;
  block[1][1] = 1;
  block[1][2] = 1;
  block[2][0] = 1;
  block[2][1] = 0;
  block[2][2] = 1;
  int[] [] aux=new int[3][3];
  for(int i=0;i<3;i++)
  {
for(int j=0;j<block[i].length;j++)
     {
  aux[i][j]=block[i][j];
     }
 System.out.print("\n");
  }
 aux[1][1]=0;//replace array value 
 aux[2][1]=1;//replace array value 1
 System.out.print("Old   Array \n");
 for(int i=0;i<3;i++)
 {
for(int j=0;j<block[i].length;j++)
   {
 System.out.print(block[i][j]);
   }
 System.out.print("\n");
 }
 System.out.print("New  Array \n");
 for(int i=0;i<3;i++) 
 {
for(int j=0;j<block[i].length;j++)
    {
  System.out.print(aux[i][j]);
    }
       System.out.print("\n");
   }
  }
 }

outpur:Array of aux is

   Old  Array 
    111
    111
    101
   New Array 
    111
    101
    111

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM