wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes arrays not equal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arrays not equal" Watch "arrays not equal" New topic
Author

arrays not equal

Michael D Watson
Greenhorn

Joined: Sep 29, 2011
Posts: 5
I am having a problem I need help with please. I am supposed to demonstrate the difference between shallow copy and deep copy but for some reason even though object wd prints out the same as wd2, the equals method still says they are not equal. Can anyone tell me or hint to me what i am doing wrong? Thank you.

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Below constructors that should possibly indicate the shallow and deep copy concept is never used in the program.



Moreover the equals method just checks for the array references - whether they refer to the same object. So when you create two different objects for the WrapperDeep class it is going to return false
Michael D Watson
Greenhorn

Joined: Sep 29, 2011
Posts: 5
The output is supposed to be

**** TESTING SHALLOW OBJECTS ****

inital shallow object contains
7 17 77
copy shallow object contains
7 17 77
inital shallow object changed to
13 14 15
copy shallow object not changed contains
13 14 15
WOOPS! ws.equals(ws2) is true

**** TESTING DEEP OBJECTS ****

inital deep object contains
2 3 4
copy deep object contains
2 3 4
inital deep object changed to
7 6 -5
copy deep object not changed contains
2 3 4
RIGHT! wd.equals(wd2) is false

Process completed.


or something like that. I don't understand how to implement the copy constructors in the assignment. Thank you for helping or hints.

Edit: I figured out my test for equality of the arrays. using Arrays.equals with java.util.Arrays import. We haven't learned it in class yet but a little independent research shouldn't be counted against I hope Still don't understand how to implement the copy constructors in this assignment though
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I will help out with the Shallow constructor and you can implement a similar thing for the Deep one.

Look at the below constructor code. It gets another WrapperShallow reference as its input.

So first step is to create two Wrapper Shallow objects

After you have created two references, then the next step is to change the array value of one of them like below.

Now check the values using the equals method. Combining all it should look like below.


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

but for some reason even though object wd prints out the same as wd2, the equals method still says they are not equal

Because, as John pointed out, both your constructors create a new array.

Off-topic, and at the risk of starting a heated discussion, I have a definite preference for using instanceof in equals() methods, viz (pattern):A few advantages are:
1. You don't need to check for null.
2. It generally works as you want in a class hierarchy; that is, it will also work if obj is a subclass of ThisClass.
3. It saves a call to getClass(), which (at least on my machine) is significantly heavier than instanceof. Now before anyone starts quoting Donald Knuth to me, I state this only because in most cases the call is unnecessary.

Obviously if the method should not work with a subclass of ThisClass, you should use the style you did; but I've rarely found it to be an issue.

Winston
Michael D Watson
Greenhorn

Joined: Sep 29, 2011
Posts: 5
Ok, I figured out the copy constructors now. Thank you very much for the help understanding them. The only issue I have though is that if I comment out the wd.setArray() method, the equals method says they are not equal even though the values are the same. I guess it might be intended that way since this is a comparison of deep and shallow object copies, not the equality of values. But most of the time, Array.equals is better for checking the equality of arrays, am I right? Thank you all very much.

As far as using instanceof in equals methods, we haven't learned that in class yet so I don't want to get too far ahead if what I am doing is working the way we are taught. Once we have learned about instanceof though, I will likely use it if it is easier and uses less memory. Thank you
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

The only issue I have though is that if I comment out the wd.setArray() method, the equals method says they are not equal even though the values are the same. I guess it might be intended that way since this is a comparison of deep and shallow object copies, not the equality of values.

Exactly right. Comparing arrays for content is a different thing from checking to see whether they are the same array.
Array.equals is better for checking the equality of arrays, am I right?

No. Array is a non-instantiable class (at least for you), so you can't call its equals() method.
Arrays.equals(), on the other hand, is precisely for comparing two arrays based on their content.
As far as using instanceof in equals methods, we haven't learned that in class yet so I don't want to get too far ahead if what I am doing is working the way we are taught.

Oops, my apologies then. It may be that the style you use is taught because it's considered "safer", but that's a discussion for a few months down the road .

Winston
Michael D Watson
Greenhorn

Joined: Sep 29, 2011
Posts: 5
Thank you both very much for helping me, John and Winston. I will mark this topic resolved now
Michael D Watson
Greenhorn

Joined: Sep 29, 2011
Posts: 5
I would like to have this thread (arrays not equal) deleted please. My professor uses this question every semester and I don't want someone being able to copy my assignment word for word. I was going to edit my classes out of the first post in the thread but the edit button is no longer there. I don't want people being able to cheat by looking up the answer this easily on my behalf. I thank everyone for helping me but I just needed help, not the whole answer to the problem. By my first post being here, someone in a future class can cheat and get the answer with no work by looking here.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Michael D Watson wrote:I would like to have this thread (arrays not equal) deleted please. My professor uses this question every semester...

I fear the thread will not be deleted. You might kindly ask your professor to change the questionnaire... Anyways its up to the moderator of the forum...
 
 
subject: arrays not equal
 
Similar Threads
Understanding superclass and subclass objects
clonning
Multidimensional array copy
Regarding Cloning Of an Java Object
about clone