This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes understanding array declarations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "understanding array declarations" Watch "understanding array declarations" New topic
Author

understanding array declarations

Daniel Loranz
Ranch Hand

Joined: Jun 17, 2008
Posts: 41
When working with declarations, I usually think something like the following.

int i; // I think "get i ready to hold int values"
String s; // I think "get s ready to hold a reference to a string object"

int myArray []; // how should I read this?

Declaring an array almost feels like there are two distinct declarations happening. I think I'm getting stuck on wanting to first see something like ...

Array myArray;

and then declaring what the elements of the array will be. In fact, why do I need to declare the elements at all?

Thanks very much!
[ June 25, 2008: Message edited by: Daniel Loranz ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
I think it is clearer if you write your array in java style rather than in C style --
int[] myArray;
rather than
int myArray[];
Then you think
"get myArray ready to hold int values"

Basically there are two initializations (not two declarations) ... first you declare the container (I think of a room ... maybe a library) "myArray" that will hold ints (or books or whatever). You need to initialize it with the number of things it will contain.

int[] myArray = new int[24]; // space for 24 ints in myArray.
Book[] library = new Book[24]; // space for 24 Books in library

Then you need to initialize the stuff inside the array ... especially if they are Objects. ints are automatically initialized to '0' but an object (like a Book object) will be initialized to null. So you can have a Library with no Books in it until you create some Books to fill the available spaces (remember that 24 spaces are available).

library[0] = new Book(); // You have put one book into library. You still have space for 23 more books.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Or you can take the shortcut and declare and initialize the array and its contents at the same time ...
int[] myArray = {0, 1, 2, 3, 4, 5}; // an array of 6 ints ... no empty spaces
Book[] library = new Book(), new Book(), new Book(); // a library with 3 books ... no empty spaces.
Daniel Loranz
Ranch Hand

Joined: Jun 17, 2008
Posts: 41
Originally posted by Marilyn de Queiroz:
[QB]I think it is clearer if you write your array in java style rather than in C style --
int[] myArray;
rather than
int myArray[];
Then you think
"get myArray ready to hold int values"


I like that. Thanks very much.

And thanks for the additional comments as well.

[ June 25, 2008: Message edited by: Daniel Loranz ]
[ June 25, 2008: Message edited by: Daniel Loranz ]
 
GeeCON Prague 2014
 
subject: understanding array declarations