Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

Help comparing two arrays

Thread view: 
Miss Michelle. Heigardt - 30 Jul 2005 18:06 GMT
Hallo, I have written this. It prints "false" and I do not know why. I
think it should print "true". If you can help I would thank you.
Thank you
Michelle

 public static void main(String[] args)
 {
   int i[][]=new int[3][3];
   i[1][2]=4;
   int j[][]=new int[3][3];
   j[1][2]=4;
   System.out.println(java.util.Arrays.equals(i,j));
 }

Here is something I copied from here saying it should print "true".

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#equals(byte[],%20byte[])

public static boolean equals(int[] a,
                            int[] a2)

   Returns true if the two specified arrays of ints are equal to one
another. Two arrays are considered equal if both arrays contain the
same number of elements, and all corresponding pairs of elements in the
two arrays are equal. In other words, two arrays are equal if they
contain the same elements in the same order. Also, two array references
are considered equal if both are null.

   Parameters:
       a - one array to be tested for equality.
       a2 - the other array to be tested for equality.
   Returns:
       true if the two arrays are equal.
Knute Johnson - 30 Jul 2005 18:36 GMT
> Hallo, I have written this. It prints "false" and I do not know why. I
> think it should print "true". If you can help I would thank you.
[quoted text clipped - 29 lines]
>     Returns:
>         true if the two arrays are equal.

Michelle:

You actually have the answer right there.  What you have is an array
whose components are arrays of ints.  Not an array of ints as required
by the method Arrays.equals(int[] a, int[] a2).

Change the last line of your program to:

System.out.println(java.util.Arrays.equals(i[1],j[1]));

and it will print true.

Signature

Knute Johnson
email s/nospam/knute/

Patricia Shanahan - 30 Jul 2005 19:09 GMT
> Hallo, I have written this. It prints "false" and I do not know why. I
> think it should print "true". If you can help I would thank you.
[quoted text clipped - 29 lines]
>     Returns:
>         true if the two arrays are equal.

int i[][] is not an array of int. It is an array of references to arrays
of int, so you were looking at the wrong part of the documentation.

To compare, for example, i[0] to j[0], Arrays.equals will use
(i[0]==null ? j[0]==null : i[0].equals(j[0])), which is false unless
i[0] and j[0] are either both null or are references to the same object.

A lot of Java books confuse two different concepts:

1. Arrays of array references, which Java does have.

2. Multidimensional arrays, which it does not have and for which arrays
of array references are a rough approximation.

If i and j were two-dimensional arrays of int, they would have the same
size, and the same elements, in the same order, so they should be
equal. They are not equal because they are really arrays of references
to different arrays of int.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.