• Post Reply Bookmark Topic Watch Topic
  • New Topic

3 array questions  RSS feed

 
sarah Marsh
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello:
1.
I use System.arraycopy(originalArray, 0, copiedArray, 0, originalArray.length);
then what's the difference between
originalArray.equals(copiedArray) and
Arrays.equals(originalArray, copiedArray) ?

2.
I use String[] clonedArray = (String[])originalArray.clone();
then what's the difference between
originalArray.equals(clonedArray)) and
Arrays.equals(originalArray, clonedArray)) ?

3.what's difference between arraycopy and clone?

Thanks!
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1, 2. Don't know. What does it say in the API for those methods?
3. The arraycopy method copies all or part of an array into all or part of another array. Not a new object like clone.

CR
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
1, 2. Don't know. What does it say in the API for those methods?


Well, to be fair, there is no Javadoc for String[].equals(); you have to read the Java Language Spec to find out what it does. For any array type, the built-in equals() method is just the one inherited from Object, which compares the target to the argument using "=="; i.e., it tests if the two arrays are the same physical object.

Arrays.equals(), on the other hand, compares the two arrays for length, type, and contents, as you'd expect. They can be two different objects, as long as they're "the same".
 
sarah Marsh
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arraycopy method copies all or part of an array into all or part of another array, then the another array is a new object, right?

There's no compiling or running time error for String[].equals();
and obj1.equals(obj2) should be different from ==, right?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sarah Marsh:
arraycopy method copies all or part of an array into all or part of another array, then the another array is a new object, right?

You have to create 'another array' before calling arraycopy. arraycopy does not create it for you.

Originally posted by sarah Marsh:
obj1.equals(obj2) should be different from ==, right?


Depends on the type of obj1. If it is a class that has not overridden the equals() method of Object, then obj1.equals(obj2) will be the same as obj1 == obj2. i.e. it will only be true if obj1 and obj2 reference the same object.
[ June 22, 2006: Message edited by: Joanne Neal ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!