-1

How im seeing my issue, please tell me where im going wrong? Cheers

Book[] booksArray = new Book[10];
Book[] sortedBooks = new Book[10];

2 Different arrays of Book Objects, correct? (assume they have data, cbf posting all code)

sortedBooks = booksArray;

sortedBooks is now a duplicate of booksArray, yes?

sortBooksByPrice(sortedBooks, numOfBooks);

public static void sortBooksByPrice(Book[] sortedBooks, int numOfBooks)
    {
        Book objHolder;//For the purpose of swapping/sorting

        for(int i = 0; i < numOfBooks; i++)   
        {
            if(numOfBooks > (i +1))
            {
                if(Double.parseDouble(sortedBooks[i].getPrice()) > 
                    Double.parseDouble(sortedBooks[i+1].getPrice()))
                {
                    objHolder = sortedBooks[i];
                    sortedBooks[i] = sortedBooks[i+1];
                    sortedBooks[i+1] = objHolder;
                }
            }
        }
            displayAllBooks(sortedBooks, numOfBooks);
    }

displays sortedBooks as expected, but now if I call

displayAllBooks(booksArray , numOfBooks);

It will display sortedBooks instead. Whats going on here? D:

Snar3
  • 68
  • 1
  • 10

7 Answers7

1

The magic happens at this line:

sortedBooks = booksArray;

You are not making a copy of booksArray. What really is happening is that you are making sortedBooks refer to the same array that booksArray is referring to.

This means that no new array is created. You just have 2 names that refer to the same thing. This is why after you made changes to sortedBooks, the changes also occurs in booksArray.

If you want to really make a copy of an array, you can manually copy the elements from one array to another using a for loop:

for (int i = 0 ; i < booksArray.length ; i++) {
    sortedBooks[i] = booksArray[i];
}

or use the copyOf method:

sortedBooks = Arrays.copyOf(booksArray, booksArray.length);
Sweeper
  • 145,870
  • 17
  • 129
  • 225
1

When you do this statement:

sortedBooks = booksArray;

Your sortedBooks variable will point to the booksArray, not cloning the object.

You can use the Java Comparable interface to get a better result:

class BookPriceComparator implements Comparator<Book> {

    @Override
    public int compare(Book b1, Book b2) {
        return b1.getPrice().compareTo(b2.getPrice());
    }
}

And then:

Arrays.sort(booksArray, new BookPriceComparator());

Hope it helps.

0

sortedBooks is now a duplicate of booksArray, yes?

No. sortedBooks points to booksArray. You just have two links to one array.

dehasi
  • 2,014
  • 16
  • 23
0

Why don't you implement a Comparator to do the comparison (BookComaparator) and then call :

Arrays.sort(sortedBookks,new BookComaparator);
Simo
  • 195
  • 1
  • 15
0

sortedBooks is now a duplicate of booksArray, yes?

Actually no. Because you just reassigned sortedBooks reference to the same array object on which booksArray has reference. As result, you have two references to one array object. Second one is eligible for garbage collection.

  • So whats the solution here? I need to parse the array through a method and display results with another method without actually altering the array ? ahh – Snar3 May 31 '17 at 12:12
  • Use a copy of array. You can use System.arraycopy or Arrays.copyOf method to do this. And then sort this copy by Arrays.sort( array ); – arsen_adzhiametov May 31 '17 at 12:15
0

Yes, this is a classic java principle. As mentioned here https://en.wikipedia.org/wiki/Clone_(Java_method)

In Java, objects are manipulated through reference variables, and there is no operator for copying an object—the assignment operator duplicates the reference, not the object. The clone() method provides this missing functionality.

When you initialized 2 arrays, each object points to some memory location on the heap. Lets say booksArray is pointing to an object with memory location X and sortedBooks is pointing to an object with memory location Y.

Now when you did

sortedBooks = booksArray

you essentially pointed the memory reference of sortedBooks to point to the memory location of booksArray which is Y. In short, booksArray and sortedBooks are now pointing to the same object on the heap.

Any operations made to sortedBooks will change the data in the object which sortedBooks point to. As booksArray points to the same object, it will also reflect the same changes.

If you want to deep copy an object in java, use the Object.clone() method Details here- https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone()

Rohan Rayarikar
  • 212
  • 2
  • 5
-1
sortedBooks = Arrays.copyOf(booksArray, booksArray.length);
matt
  • 8,598
  • 3
  • 19
  • 30