Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

3 array questions

 
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 74084
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic