1

In my program i passed an array to a method called setArray().But still i am getting 0 value from the array..

class Test{
    void setArray(int arr[]){
        Scanner in=new Scanner(System.in);
        int size;
        System.out.print("\nEnter the size : ");
        size=in.nextInt();
        arr=new int[size];
        System.out.print("\nEnter the elements one by one : ");
        for(int i=0;i<size;i++){
            arr[i]=in.nextInt();
        }
    }   
    void create(){
        int a[]={0};
        setArray(a);
        System.out.print("output : ");
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }   
    }

    public static void main(String[] args)throws IOException{
        Test t=new Test();
        t.create(); 

    }
}

Enter the size : 4

Enter the elements one by one : 1 2 3 4

output : 0

Ganesh kumar S R
  • 541
  • 1
  • 6
  • 10

7 Answers7

1

The array reference is passed by value, which means that if you change what the reference is pointing at (using assignment through =) it won't change the contents of the array you passed into the function because that original array reference will still be pointing at the original array contents. The assignment only changed what the copy of the original reference is pointing at.

However, your code can be easily fixed by simply returning the array from the function:

int[] getArray(){        
    Scanner in=new Scanner(System.in);
    int size;
    System.out.print("\nEnter the size : ");
    size=in.nextInt();
    int[]arr=new int[size];
    System.out.print("\nEnter the elements one by one : ");
    for(int i=0;i<size;i++){
        arr[i]=in.nextInt();
    }
    return arr;
}

Or if you know the size of the array beforehand you can change the array contents by accessing them through the passed-by-value array reference. Here is an example of a function which will modify the array in a way that will be visible to the callers of the function:

void setValue(int[]arr, int pos, int val) {
    arr[pos] = val;
}

So the problem in your code is the fact that you are calling arr=new int[size]; inside your function.

cyon
  • 9,340
  • 4
  • 22
  • 26
0

Halfway through setArray, you're replacing the value of the parameter arr with a brand new array, which you're then setting values in. But the old array still exists - it's just not referenced by the parameter arr any more. However, the old array is still referenced by the variable a in create - so it is the one that you eventually print out.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

That's because when you say arr = new int[size], you are no longer using the original reference. You can check it if you print your array just before you exit out of the setArray method: internally it was created, but externally your old reference hasn't been changed.

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
0

Java only supports passing parameters by value. You cannot change what arr is referencing outside the scope of setArray. You could alternatively return the newly created array.

Tarik
  • 10,810
  • 2
  • 26
  • 40
0

This is classic example to demonstrate that Java is 'pass by value' and not 'pass by reference'. :)

What you are passing to setArray method is copy of reference a .

This copy is pointing to new object created inside method setArray. But original reference is still pointing to old object.

You need to either return new object created inside method setArray or initialize it first and then pass it to method, so that copy of reference also points to same object and your changes are reflected.

Pranalee
  • 3,389
  • 3
  • 22
  • 36
0

You are just sending primitive type value which is passed to setArray(..) method by value. Add return statement to setArray or change the type of array to Integer. Hope this help.

ktk
  • 297
  • 4
  • 13
0

Here is the error, the values you are storing in arr[] are not set into the original a[]. so just take the array arr[] into a[] by changing return type of setArray() method to int[] and change line,

setArray(a);

to

a=setArray(a);

class Test{
    int[] setArray(int arr[]){
        Scanner in=new Scanner(System.in);
        int size;
        System.out.print("\nEnter the size : ");
        size=in.nextInt();
        arr=new int[size];
        System.out.print("\nEnter the elements one by one : ");
        for(int i=0;i<size;i++){
            arr[i]=in.nextInt();
        }
    }   
    void create(){
        int a[]={0};
        a=setArray(a);
        System.out.print("output : ");
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }   
    }

    public static void main(String[] args)throws IOException{
        Test t=new Test();
        t.create(); 

    }
}