2

I have a question about returning an array from one method back to main. It is seriously starting to annoy me and I cannot wrap my brain around this. It seems as if even though I have changed the array in my method, when I go to display it in main it displays the old array. I'm trying to remove the duplicate numbers in my array and I know it works because I have stepped through it in the debugger yet after I return it and go back to main, it displays the whole array again! I know it must be something easy that I am missing here. Can someone please point me in the right direction? Here is my code...

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] numbers = new int[10];

    System.out.print("Enter 10 numbers: ");

    for (int x = 0; x < numbers.length; ++x)
        numbers[x] = input.nextInt();

    eliminateDuplicates(numbers);

    for (int y = 0; y < numbers.length; ++y)
        System.out.print(numbers[y] + " ");
}


public static int[] eliminateDuplicates(int[] numbers) {
    int[] temp = new int[numbers.length];
    int size = 0;
    boolean found = false;

    for (int x = 0; x < numbers.length; ++x) {

        for (int y = 0; y < temp.length && !found; ++y) {
            if (numbers[x] == temp[y])
                found = true;
        }
        if (!found) {
            temp[size] = numbers[x];
            size++;
        }
    found = false;
    }   

    int[] result = new int[size];
    for (int z = 0; z < result.length; ++z)
        result[z] = temp[z];

    return result;
}
}
Eventh
  • 21
  • 2
  • 7

6 Answers6

5

Look at this call:

 eliminateDuplicates(numbers);

You're ignoring the return value. Perhaps you wanted:

 numbers = eliminateDuplicates(numbers);

I sometimes wish that Java had a way of indicating that a method's return value shouldn't be ignored. It would save a lot of questions around InputStream.read as well...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

It should be: numbers = eliminateDuplicates(numbers);

You were essentially ignoring the returned result from the method.

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
  • Haha I knew it was something stupid. Thank you very much for the speedy answer... I feel quite dumb now. Self teaching is kind of rough sometimes – Eventh Aug 13 '12 at 06:21
1

Do like this:

int[] result =eliminateDuplicates(numbers);
for (int y = 0; y < result.length; ++y)
    System.out.print(numbers[y] + " ");

Also, In your eliminateDuplicates() method, use result array directly by removing temp array. Or rename temp to result and return it. Following code is unnecessary :

int[] result = new int[size];
    for (int z = 0; z < result.length; ++z)
        result[z] = temp[z];
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
  • Yes but then wouldn't the size still be 10 and I would have 0's if there were numbers not included from the original array? – Eventh Aug 13 '12 at 06:24
0

In your main, try this:

numbers = eliminateDuplicates(numbers);
Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
geffchang
  • 3,279
  • 2
  • 32
  • 58
0

Method calling should be like this -

numbers = eliminateDuplicates(numbers);

You are not catching the result returning by eliminateDuplicates function.

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
0

In Java, parameters are passed by value, so you can't use numbers as an in/out parameter.

e.g. if you C# you could have done

 eliminateDuplicates(ref numbers);

But as others have pointed out, the most obvious issue is that you forgot to assign the result :)

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285