0

So I'm playing around with a number-sorting program, trying to write the most efficient way possible to order a random list of numbers from lowest to highest.

I'm having trouble after the first number sorting method has been run, the ArrayList passed to the second method is empty. I figure it has something to do with the first methods sorting technique; which finds the lowest value, places it in a new ArrayList, removes it from the mixed one, then starts again.

What I don't understand is that I pass the mixedArray to sortingMethod1, which is in an entirely different class, yet somehow it still edits the local variable in the main method... help please?

P.S. All of this uses ArrayLists, even though I reference array.

Inside The Main Method:

List<Integer> mixedArray = sorter.generateNumbers(numbers, range);
sorter.printArray(mixedArray); //The Random Numbers
sorter.printArray(sorter.sortingMethod1(mixedArray)); //This prints them out ordered
sorter.printArray(sorter.sortingMethod2(mixedArray)); //This receives a null array

Here is sortingMethod1:

public List<Integer> sortingMethod1(List<Integer> mixedArray){  //Finds the lowest, places it into a new array.
    List<Integer> sortedArray = new ArrayList<Integer>();
    while (!mixedArray.isEmpty()) {
        int lowest = mixedArray.get(0);
        for (int i = 1; i < mixedArray.size(); i++) {
            if (mixedArray.get(i) < lowest) {
                lowest = mixedArray.get(i);
            }
        }
        sortedArray.add(lowest);
        mixedArray.remove(mixedArray.indexOf(lowest));
    }
    return sortedArray;

As requested, sortingMethod2, however it is still untested

 public List<Integer> sortingMethod2 (List<Integer> mixedArray) {   // Finds the lowest, places it at beginning
        for (int i = 0; i < mixedArray.size(); i++) {
            int lowest = mixedArray.get(i);
            for (int x = i; x < mixedArray.size(); x++) {
                if (mixedArray.get(x) < lowest) {lowest = mixedArray.get(x);}
            }
            Collections.swap(mixedArray, i, mixedArray.indexOf(lowest));
        }
        return mixedArray;
    }
Scott
  • 279
  • 3
  • 14
  • 2
    You are passing around a reference to your `mixedArray` array list. Read up on the concept of pass-by-reference. Your `sortingMethod2` should not receive a null array list. But the array list is empty as you cleared it in `sortingMethod1`. – Ralf Apr 29 '16 at 10:20
  • No. The reason is the fact that you are passing around a reference to the array list. Not the "value" of the array list. I.e. what gets duplicated when you call `sortingMethod1` and `sortingMethod2` is not the array list (and its content) but the reference to the array list object. – Ralf Apr 29 '16 at 10:23
  • @Scott No, that's not it. The issue is just what Ralf described - when passing your array to `sortingMethod1`, you're not giving it a copy of the list, but rather the list itself via reference. So when you remove stuff from it in your sort, you remove it for good, not just for that method. – Ordous Apr 29 '16 at 10:24
  • You are pasing mixedArray as pass by reference. So when you remove all the elements from the mixedArray in sortingMethod1, this clears the list. To avoid this, you can make a copy of mixedArray inside sortingMethod1, and access this new arraylist for operations wherever you want. For eg. - List newMixedArray= new ArrayList(mixedArray);.... Now you can continue your operations with the newMixedArray list. – Manish Apr 29 '16 at 10:26
  • Ok thanks guys, this is a lot to get my head around but I think I understand enough for now. Does this mean, if I had a method to set the values of an object, it doesn't matter where this method is in my code; as long as I pass it the object, it can do all of the manipulation within it's own method, because it now has access to the pointer to the actual object, rather than just a duplicate? And it doesn't matter what name I give the object within this method because all that matters is that it has received the pointer to it's actual location? – Scott Apr 29 '16 at 10:40
  • @Scott Yes. Java does not copy an object for every single method that needs it. Variable names don't matter for anything except code readability, unless they clash. – Ordous Apr 29 '16 at 11:04

0 Answers0