• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java arrays

 
goel Ashish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am little confused in arrays. What i think is if we declare a statement like this :-



then this means that we are declaring a 2D array object containing elements of type int. Am I right??

Second: what if we want to compare two array objects?? Is there any way to override equals() method for array objects so that we can invoke equals() method to compare two arrays.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not have real 2D arrays. With a declaration that you have above, what you are really declaring is an array of which the elements are arrays that contain integers. The preferred syntax in Java is to put the angle brackets after the type instead of the variable name:

It is not possible to override the equals method or any other method for arrays. When you want to compare the content of two arrays, you can use one of the Arrays.equals() methods (java.util.Arrays is a class that contains utility methods for working with arrays).
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This declaration
int arr[][];


can be read as "Declare the array of 'array of integers'"

Also array class as already overridden equals() .check the API for Arrays.equals(arg1,arg2);

Thanks
 
goel Ashish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See its not about 1D or 2D array.
My purpose to ask the question was to ask if array is an object then where is its class defined??
 
Federico Cardelle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3

array is not a class. There are three kinds of reference type: class, interface and array.
Its definition is in the java language specification
http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#27803
and then implemented in each java implementation.
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
goel Ashish wrote:Is there any way to override equals() method for array objects so that we can invoke equals() method to compare two arrays.


No, but the java.util.Arrays class has a method for that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic