• Post Reply Bookmark Topic Watch Topic
  • New Topic

compare array to an array object?  RSS feed

 
Matt Wilde
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what i have



for some reason this still does not work when comparing the object of "h" to my currentObject.... any tips?
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed this doesn't work. And here's why:

The variables that 'hold' objects don't actually 'hold' the object itself. Instead it holds a reference to the real object which lives on the heap.
What you are doing is comparing two references, which are obviously not equal since they refer to different objects.

So what you can do is use the 'equals' method on the java.util.Arrays class. Like:



This method will compare each element in the array to the elements in the other array.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt, for your information, there already is a class that does what you want: java.math.BigInteger. Unless this class is for educational purposes I would use that one.

Your isEqualTo method looks horrible. You already have a HugeInteger object, this, yet you create a copy anyway. You then use == to compare the two objects, which will always return false because that uses reference equality and you've just assigned a new object to reference d.

Also, instead of creating your own equality method, you should override the equals method (and therefore also the hashCode method). A quick attempt:
There is a discussion about using instanceof though. On the one hand, using instanceof in a non-final class or a non-final method can cause the symmetry rule of Object.equals to be broken. On the other hand, using class comparison (o.getClass() == getClass(), don't forget to check if o == null) can break the Liskov substitution principle.
 
Matt Wilde
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the Arrays.equals(array1, array2) but how do i access the array that was created along with the new HugeInteger object "h"? and compare said array to my array "b". And yes this is for educational purposes.
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Wilde wrote:I understand the Arrays.equals(array1, array2) but how do i access the array that was created along with the new HugeInteger object "h"? and compare said array to my array "b". And yes this is for educational purposes.


Well, that's a little dependent on how you've organized your Java project. To answer that question, we will need a little more context!
 
Matt Wilde
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well what you see in the first post is what i've got. So i'm just trying to access the integer array that was made with object "h".
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Wilde wrote:i'm just trying to access the integer array that was made with object "h".


http://www.coderanch.com/t/569679/java/java/compare-array-array-object#2589657
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!