Pregunta

When I run this code, it prints 0 , 1, 2, but I have no idea why. could you explain me?

public void run() {
        int[] arr =  new int [3];
        for(int i=0; i<arr.length;i++){
            arr[i]=i;
        }
        domore(arr);
        for(int i=0; i<arr.length;i++){
            println(arr[i]);
        }
    }

    private void domore(int[] arr) {
        // TODO Auto-generated method stub
        int [] att = new int [3];
        for(int i=0; i<att.length;i++){
            att[i]=77;
        }
        arr=att;
    }
¿Fue útil?

Solución 5

When you are passing any parameter value in function, scope of that will be local to that function only.

And another thing is, In java you are calling a function by value not by reference.

If you want to use modified value, you have to return that from function.

public void run() {
            int[] arr =  new int [3];
            for(int i=0; i<arr.length;i++){
                arr[i]=i;
            }
            // arr is in run() method
            domore(arr);
            for(int i=0; i<arr.length;i++){
                System.out.println(arr[i]);
            }
        }

        private void domore(int[] arr) {
            // TODO Auto-generated method stub
            int [] att = new int [3];
            for(int i=0; i<att.length;i++){
                att[i]=77;
            }
            arr=att; // here scope of arr is local for domore() method only
            // so arr in run() method will not modified.
        }

Otros consejos

In Java - "References to objects are passed by Value". Any change you make in the doMore() method will not be reflected in your original array because you are reassigning the passed reference there... ( Please use camelCase for naming methods/fields. i.e, use doMore() instead of domore).

public void run() {
        int[] arr =  new int [3];   // arr points to an integer array of size 3
         doMore(arr);
        } 
 private static void doMore(int[] arrNew) {   // arrNew is copy of arr so it  also points to the same integer array of size 3. So, any changes made by arrNew are as good as changes made by arr (and yes data of arr is changed..)        
        // TODO Auto-generated method stub
        int [] arrNew = new int [3];           // you are making arrNew point to new integer array of size 3. So, now arrNew points to a new object and not to the one pointed by arr. 
        for(int i=0; i<arrNew.length;i++){
            arrNew[i]=77;
        }

    }

In the method domore int[] arr is passed as an argument, meaning a copy of the reference is passed (aka by value). In the method a new int[] is created and then assigned to the copy of the reference (int[] arr). This will never change the initial int[] created in the run method. It will only assign the passed argument to the newly created array within domore().

Because you are putting a reference of your arr-Array to the method domore. Inside it you are cutting this reference by setting arr=att. The outer method run is not able to recognize this newly set reference and will stay with your old array reference where you have put the values 0, 1, 2.

Try returning the array arr in domode method

public void run() {
    int[] arr =  new int [3];
    for(int i=0; i<arr.length;i++){
        arr[i]=i;
    }
    arr = domore(arr);
    for(int i=0; i<arr.length;i++){
        println(arr[i]);
    }
}

private int[] domore(int[] arr) {
    // TODO Auto-generated method stub
    int [] att = new int [3];
    for(int i=0; i<att.length;i++){
        att[i]=77;
    }
    return(att);
}

domore is a void function. If you wan't your Values to change you'll have to return something.

private int[] domore (int[] arr){
int [] att = new int [3];
    for(int i=0; i<att.length;i++){
        att[i]=77;
    }
    return att;
}

And you call it by:

arr = domore(arr);

You are working with an array of primitives. If you change int to Integer (from primitives to objects) you should not have that problem.

int a=5;
int b=a;
b=10;

->a will still be 5. if you want to use primitives, then just return the int array and assign it to the initial array

arr=doMore(arr);

use arr = (int[]) att.clone(); or you can use Array.copyOf method too!!!

instead of arr=att

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top