1

I read that you can pass an array as a parameter to a method but is it recommended? What i mean is, is there any other way of doing it that would be more accepted?

In my case, i have 2 classes that each contains an array and i need to check if both of them are the same or are different. So i thought of creating a method in one of the class that would take an array in parameter and then compare every value of the two arrays.

I am not sure if Arrays.equals(): works since it's an object array and not just numbers.

I found this Java Array Comparison but it seems to be more complicated than what i need to do.

Community
  • 1
  • 1
  • 1
    (Arrays.equals will work, or you can do it yourself. Arrays.equals *may* be implemented as native code and be a hair faster, but it could be plain old Java code -- no better than you can do. One would have to look at the source to see which.) – Hot Licks Sep 18 '12 at 17:12
  • Keep in mind that a Java array is an object (subclass of Object), just like a String or a BufferedInputString or a HashMap. – Hot Licks Sep 18 '12 at 17:16
  • i'm not sure of how else you could do it (beside the arrays.equals) but i have read online that because the arrays are passed by reference, it might cause problems in certain conditions – user1626166 Sep 18 '12 at 17:17
  • 1
    The "problem" is that the array can be modified by the called routine. Of course, this is only a problem if you don't trust the person who wrote the called routine. (And, yes, sometimes you shouldn't trust yourself, but you can't be paranoid about every call you make -- if you pass, say, a Set of your objects they're still open to modification. And you can always use System.arraycopy to make copies of the arrays if you're worried about the arrays themselves being modified.) – Hot Licks Sep 18 '12 at 17:25

4 Answers4

2

I am not sure if Arrays.equals() works since it's an object array and not just numbers.

There is one flavour of Arrays#equals that takes two Object[] as parameters. As long as your objects properly implement equals, it should work as expected.

assylias
  • 321,522
  • 82
  • 660
  • 783
2

1) yes that's OK to pass arrays as parameters, there is absolutely no problem in doing so

2) Arrays.equals(Object[] a, Object[] b) can check Object arrays (you must be sure that you have the appropriate equals method on your object if you don't want to simply test instance identity). Note that the existence of this method is the proof of 1 ;)

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • I see, so i have to override the equals function to test my class parameters, is that correct? – user1626166 Sep 18 '12 at 17:19
  • Generally yes : your equals method will check if the content of the instance is identical. Note that you should always override the hashcode method when overriding the equals method. See [this](http://www.xyzws.com/javafaq/why-always-override-hashcode-if-overriding-equals/20) and [this](http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java). – Denys Séguret Sep 18 '12 at 17:23
1

Though it is fine to use arrays as parameters to methods, your use case can be solved by forming java.util.Set for those two arrays and use apache commons CollectionUtils.isEqualCollection to check if they are same.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • With what i seem to understand, this doesn't need an override of the equal method but i have to use the toArray() function: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collection.html#toArray() – user1626166 Sep 18 '12 at 17:24
  • sorry, I didn't get what you are trying to say? I was saying that the arrays can be replaced with sets and used for comparison (unless you want to allow duplicates of an object in that array which will complicate your `equals` definition). Could you please elaborate? – Vikdor Sep 18 '12 at 17:28
0

I don't see why this would not be recommended. I sometimes prefer passing arrays rather than collections as arguments to methods.

Dan D.
  • 32,246
  • 5
  • 63
  • 79