wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Array or Arrays? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array or Arrays?" Watch "Array or Arrays?" New topic
Author

Array or Arrays?

Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26
I'm trying to use an Array and inovke its method calls but I keep getting "cant find symbol" errors. In looking at the java API I can see that there is an Array class and an Arrays class. When I create an array like this - private int[] someArray = new int[24]; what class of array am I calling?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Neither.

Those are both utility classes that are used to manipulate arrays. Neither is an array.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 231
    
    1

Shelby Simpson wrote:When I create an array like this - private int[] someArray = new int[24]; what class of array am I calling?

To find out the class of an object, use the getClass() method. You will find that you are creating neither an "Array" or "Arrays" object.



This prints out "class [I". Similarly, [B is the class of the byte array, [C is the class of the char array, etc.

Shelby Simpson wrote:In looking at the java API I can see that there is an Array class and an Arrays class.

Oracle's website explains that these classes "contain various methods for manipulating arrays." To use the methods of these classes with your array, you must make a static reference to the class and pass in your existing array. For example:

Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26
Ok I see, I was looking at that all wrong. I didn't know about the getClass method. Thanks for clearing that up for me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
Joel Christophel wrote: . . . make a static reference to the class and pass in your existing array. . . .
You mean “call the static methods on the class name”, just as you do for any static method. You do not actually make a reference to the class. The code was, of course, correct.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array or Arrays?
 
Similar Threads
generate random number into 2d arrays
Printing a primitive array
Array Conversion
Array object vs. an array
Question on Multidimensional Array