Duplicate :
|
|
Duplicate :
|
|
Duplicate :
|
|
Duplicate :
|
Name: dm26566 Date: 08/20/98 Arrays should provide their own implementations for toString(), equals() and hashCode() instead of the default implementations they inherit from Object. The toString() method should generate a comma seperated list of the array elements enclosed in square brackets. Primitive types should be converted to strings using the appropriate conversion methods and objects (including other arrays) should be converted as if by String.valueOf(Object) Examples: [1, 2, 3] // array of ints [a, b, c] // array of chars [MyClass@ABCDEF, null, MyClass@123456] // array of MyClass objects [] // empty array (zero length) [[1, 2], [], [3], null] // array of arrays of ints The equals() method should perform a value comparison instead of a reference comparison. This will also require that the hashCode() method be overridden to return a value based on the array's contents. I recommend using the formula given for java.util.List.hashCode(). You'll have to modify it to handle primitive types as well as object references. (Review ID: 37232) ====================================================================== Name: jl125535 Date: 11/28/2001 java version "1.3.1" Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24) Java HotSpot(TM) Client VM (build 1.3.1-b24, interpreted mode) Another reason to support equals for array objects is to allow java.util.Arrays.equals() to behave correctly when presented with arrays of arrays. According to the javadoc, java.util.Arrays.equals() is behaving properly. In the code below, the unexpected behavior arises from the semantics of the equals() method for objects that represent arrays. Actual output: Arrays.equals() = false rows1.equals() = false Expected output: Arrays.equals() = true rows1.equals() = true Code: import java.util.Arrays; public class ArrayEqualityTest { public static void main(String[] args) { String[][] rows1 = { new String[] { "4", "spank harder" }, new String[] { "6", "douglas -- gangsta" }, new String[] { "5", "momma''s revenge" } }; String[][] rows2 = { new String[] { "4", "spank harder" }, new String[] { "6", "douglas -- gangsta" }, new String[] { "5", "momma''s revenge" } }; System.out.println("Arrays.equals() = " + Arrays.equals(rows1, rows2)); System.out.println("rows1.equals() = " + rows1.equals(rows2)); } } (Review ID: 136372) ======================================================================
|