简体   繁体   中英

JAVA Pass by reference error in method

I was trying to perform sorting of integers in an array and it worked fine. But when i try to modify the program by including a "pass by reference" concept via a method, it is throwing error "cannot find symbol".

I am new to JAVA and learning by my own, Please help me with what I am doing wrong here.

import java.util.*;
import java.io.*;

public class Sort {

    public static void main(String[] args) {

        Sort obj = new Sort();

        Scanner in = new Scanner(System.in);
        int i, p, k, arr[];
        arr = new int[10];
        System.out.println("Enter the numbers for sorting \n");
        for (i = 0; i < 5; i++) {
            arr[i] = in.nextInt();
        }

        for (i = 0; i < 5; i++) {
            for (p = 0; p < 5; p++) {
                if (arr[i] < arr[p]) {
                    /*
                     * moving the below block for swapping to a new method. k =
                     * arr[i]; arr[i]= arr[p]; arr[p]= k;
                     */

                    obj.swap(obj);
                }

            }
        }
        System.out.println("\n");
        for (i = 0; i < 5; i++)
            System.out.println(arr[i]);

    }

    public void swap(Sort m) {
        m.k = m.arr[i];
        m.arr[i] = m.arr[p];
        m.arr[p] = m.k;

    }

}

The error I am getting is :

"Sort.java:44: error: cannot find symbol
      m.k = m.arr[i];
       ^
"

Similarly 10 such errors for other variables as well.

You are trying to use index variables ( i and p ) that don't exist in the context you are trying to use them (inside swap() method body) as well as members of Sort ( k and arr ) which don't exist. The scope of all these, you have limited to the method body of main() :-

public void swap(Sort m) {
    m.k = m.arr[i];      //No 'i' in swap(). No 'k' or 'arr' in 'm'(an instance of 'Sort')
    m.arr[i] = m.arr[p]; //No 'p' in swap()
    m.arr[p] = m.k;
}

Short-term Solution

Change your swap() method to

//Now accepting in i and p
public void swap(Sort m, int i, int p) {
    m.k = m.arr[i];      
    m.arr[i] = m.arr[p]; 
    m.arr[p] = m.k;
}

then call it like this

obj.swap(obj, i, p); //pass in i and p

and move your Sort variables to be accessible members of Sort

public class Sort {
  public static int k;                   //now accessible with m.k
  public static int[] arr = new int[10]; //now accessible with m.arr
...
}

Lastly, is it intentional that your array is 10 long but you only fill it with 5 numbers?

Pass-by-Reference

There is no "pass-by-reference" in Java. Everything is passed by value. The confusing thing is that what is passed by value is technically a reference to the object, meaning you get strange effects like you can edit the object but not reassign it.

Solution: move the stuff back from the swap method to where it was.

Alternatively, provide the necessary values as parameters to swap .

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