Weirdness in Equated Java Arrays: References vs. Pointers

后端 未结 4 1532
囚心锁ツ
囚心锁ツ 2020-12-11 21:48

Having a problem understanding what\'s going on n the code below. The behavior of arrays c and d is what I would expect. But what\'s going on with

4条回答
  •  孤城傲影
    2020-12-11 22:00

    There is an important point of arrays that is often not taught or missed in java classes. When arrays are passed to a function, then another pointer is created to the same array ( the same pointer is never passed ). You can manipulate the array using both the pointers, but once you assign the second pointer to a new array in the called method and return back by void to calling function, then the original pointer still remains unchanged.

    You can directly run the code here : https://www.compilejava.net/

    import java.util.Arrays;
    
    public class HelloWorld
    {
        public static void main(String[] args)
        {
            int Main_Array[] = {20,19,18,4,16,15,14,4,12,11,9};
            Demo1.Demo1(Main_Array);
            // THE POINTER Main_Array IS NOT PASSED TO Demo1
            // A DIFFERENT POINTER TO THE SAME LOCATION OF Main_Array IS PASSED TO Demo1
    
            System.out.println("Main_Array = "+Arrays.toString(Main_Array));
            // outputs : Main_Array = [20, 19, 18, 4, 16, 15, 14, 4, 12, 11, 9]
            // Since Main_Array points to the original location,
            // I cannot access the results of Demo1 , Demo2 when they are void.
            // I can use array clone method in Demo1 to get the required result,
            // but it would be faster if Demo1 returned the result to main
        }
    }
    
    public class Demo1
    {
        public static void Demo1(int A[])
        {
            int B[] = new int[A.length];
            System.out.println("B = "+Arrays.toString(B)); // output : B = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
            Demo2.Demo2(A,B);
            System.out.println("B = "+Arrays.toString(B)); // output : B = [9999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
            System.out.println("A = "+Arrays.toString(A)); // output : A = [20, 19, 18, 4, 16, 15, 14, 4, 12, 11, 9]
    
            A = B;
            // A was pointing to location of Main_Array, now it points to location of B
            // Main_Array pointer still keeps pointing to the original location in void main
    
            System.out.println("A = "+Arrays.toString(A)); // output : A = [9999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
            // Hence to access this result from main, I have to return it to main
        }
    }
    public class Demo2
    {
        public static void Demo2(int AAA[],int BBB[])
        {
            BBB[0] = 9999;
            // BBB points to the same location as B in Demo1, so whatever I do
            // with BBB, I am manipulating the location. Since B points to the
            // same location, I can access the results from B
        }
    }
    

提交回复
热议问题