This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

understanding array declarations

 
Daniel Loranz
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic