aspose file tools*
The moose likes Beginning Java and the fly likes java arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java arrays" Watch "java arrays" New topic
Author

java arrays

goel Ashish
Greenhorn

Joined: May 14, 2011
Posts: 21
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

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

The important thing is not to stop questioning.Curiosity has its own reason for existing.
goel Ashish
Greenhorn

Joined: May 14, 2011
Posts: 21
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

Joined: Jul 26, 2011
Posts: 26
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

Joined: May 03, 2008
Posts: 4642
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java arrays