• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

arrays not equal

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both very much for helping me, John and Winston. I will mark this topic resolved now
 
Michael D Watson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
We don't have time for this. We've gotta save the moon! Or check this out:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic