Creating Arrays
You can declare a new array variable, just like you would any other member variable (like this)
String[] args;
The square brackets mean "array." An array in Java is an object, which means that you initialize an array with the keyword
new
. Like this:
String[] args = new String[10];
This creates a new array object, called
args
, with 10 buckets for
putting
Strings into. You can fill an array with whatever kind of thing you want: Object,
int
,
boolean
,
Address
,
char
, another array, and so on.
The first element of a Java array has an index of 0. To reference the element in the second bucket of an array named
args
, type this:
args[1]
|
FRIDGE
Remember that arrays are the
size
that you make them, no bigger, and that the first element's index of an array is 0. That means that if your array holds 10 elements, you can happily access elements with index 0 through 9 all day long. Those are the 10 you get. Try accessing the element at index 10, and you'll get an
ArrayIndexOutOfBoundsException
, meaning the element you tried to access doesn't exist. Try accessing the element at index -56. Same thing. This can be confusing, so try not to be
confused
by it. Remember that the first element of an array is 0, so the last element of an array is
array length minus 1
!
|
|
That's because the array starts at 0, dammit!
There is a shorthand to declaring and initializing and populating a Java array all with one line of code. You have to already know what all your values are, but this frequently comes in handy.
String[] cats = {"Noodle", "Doodle", "Little Mister"};
Now you have an array with three buckets, containing one each of the stated values. We can get a value out of an array by referencing the array
name
, followed by square brackets containing the index of the element you want. Like this:
cats[2]; //returns "Little Mister"
The following code
demonstrates
different ways of creating regular and multidimensional arrays in Java.
Creating Arrays.java
package net.javagarage.demo.arrays;
/**<p>
* Demonstrates how to create arrays
* and refer to their elements.
* </p>
* @author eben hewitt
**/
public class CreatingArrays {
public static void main(String[] args) {
//1. instantiate new array with 5 cells
int[] myInts = new int[5];
//put a value into the 2nd cell
myInts[1] = 1;
System.out.println("1st cell: " + myInts[0] +
". 2nd cell: " + myInts[1]);
/*
* prints 1st cell: 0. 2nd cell: 1
* notice that means that the first cell
* (the 0th cell was initialized to the default
* value for its type (int)
**/
//2. now show another way to make them
//notice that spaces don't matter
//populate array with the original members of //Motorhead
String [] myStringArray = {"Lemmy","Larry","Lucas"};
//this is one of the few times in Java you
//can use ; after }
//3.
//you can also do this (create without initializing)
Object[] objects;
//note that you can declare it by putting the [] in a
//different location. the following is LEGAL:
Object myThings[];
/* but no one does it that way, and i'd avoid it.
* sometimes convention is just easier for everyone.
* it is more natural to say "Object array called
* myThings" as you readit makes sure the complete
* type is in the declaration, not the identifier.
* After all, your value on the heap is an Array
* object called 'myThings' that holds Objects,
* not a "myThings array".
*/
//try to access a value:
//System.out.println(myThings[0]);
//COMPILER ERROR! Not initialized!
//create a 2-dimensional array with 10 elements
//in each dimension:
int[][] square = new int[10][10];
//create a "ragged" array, with each dimension
//holding varying elements
int[][][][][] ragged = new int[2][4][3][10][5];
}
}
One thing to note about creating arrays (and, perhaps more to the point, the loose language that is sometimes used, even by Java book authors, as in the
preceding
code) is that there is no such a thing as a real multidimensional array in Java. Every Java array has only one dimension. You can create an array that holds another array, however, thereby achieving two-dimensional array representational capability.
About array values: You can insert into an array any value that can be automatically promoted to the declared type. For example, say you have an array of
int
s. You can put a
byte
or a
short
in there, because an
int
is 32 bits, and those are smaller than 32 bits, and so can snugly fit inside an
int
. No problem. What you can't do is add a
long
to that array because a
long
is 64 bits, which is too big to squeeze in there. You can't put an object in there either.
|