0

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.

Ross Drew
  • 8,163
  • 2
  • 41
  • 53
Sam
  • 181
  • 2
  • 3
  • 13

2 Answers2

3

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.

Ross Drew
  • 8,163
  • 2
  • 41
  • 53
  • Got it clearly. One more doubt as i am curious to know, what if I define my variables i and p also along with arr[] and k as accessible members? Can I use them directly in my swap method without being passed in my method call? – Sam Oct 30 '15 at 10:52
  • 2
    Yes but I would say they are better passed in, they are of no use anywhere but inside the loops and inside the method(s) called inside the loops. – Ross Drew Oct 30 '15 at 10:54
  • I just tried that (what i asked above), interestingly my program did not give me any compile error but then the output was not correct! I gave 5 different numbers and my output returned the numbers in the order I inputted them. If you don't mind, I would like to ask why that happened. – Sam Oct 30 '15 at 10:59
  • Works for me. `Enter the numbers for sorting`, `10 2 4 68 8`, output=`2 4 8 10 68` – Ross Drew Oct 30 '15 at 11:08
  • I got it, I need to use obj.i and obj.p where ever I am using them in that case. Got it, please ignore my above question :) – Sam Oct 30 '15 at 11:09
  • Oh, I see. If you are moving `i` and `p`, yes. Please accept my answer also =D – Ross Drew Oct 30 '15 at 11:10
0

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

Alternatively, provide the necessary values as parameters to swap.

Tomas
  • 1,315
  • 10
  • 17