Java Reference
In-Depth Information
These three arrays have elements that each hold a one-dimensional array, and you can also specify the
sizes of these independently. Note how the empty square brackets indicate there is still a dimension
undefined. You could give the arrays in each of these elements random dimensions between 1 and 7
with the following code:
for(int i = 0; i < beans.length; i++) // Vary over 1st dimension
for(int j = 0; j < beans[i].length; j++) // Vary over 2nd dimension
beans[i][j] = new long[(int)(1.0 + 6.0*Math.random())];
If you can find a sensible reason for doing so, or if you are just a glutton for punishment, you can
extend this to four, or more, dimensions.
Arrays of Characters
All our arrays have been numeric so far. You can also have arrays of characters. For example, we can
declare an array variable of type char[] to hold 50 characters with the statement:
char[] message = new char[50];
We could also define an array of type char[] by the characters it holds:
char[] vowels = { 'a', 'e', 'i', 'o', 'u'};
This defines an array of five elements, initialized with the characters appearing between the braces. This
is fine for things like vowels, but what about proper messages?
Using an array of type char , you can write statements such as:
char[] sign = {'F', 'l', 'u', 'e', 'n', 't', ' ',
'G', 'i', 'b', 'b', 'e', 'r', 'i', 's', 'h', ' ',
's', 'p', 'o', 'k', 'e', 'n', ' ',
'h', 'e', 'r', 'e'};
Well, you get the message - just - but it's not a very friendly way to deal with it. It looks like a
collection of characters, which is what it is. What we really need is something that is a bit more
integrated - something that looks like a message, but still gives us the ability to get at the individual
characters if we want. What we need is a String .
Strings
You will need to use character strings in most of your programs - headings, names, addresses, product
descriptions... - the list is endless. In Java, strings are objects of the class String . The String class is
a standard class that comes with Java, and it is specifically designed for creating and processing strings.
Search WWH ::




Custom Search