Using Arrays

You use arrays in a program as you would any variable, except for the element number between the square brackets next to the array’s name. You can use an array element anywhere a variable could be used. The following statements all use arrays that have already been defined in this hour’s examples:

elfSeniority[193] += 1;niceChild[9428] = "Eli";if (hostileAirTravelNations[currentNation] == true) {    sendGiftByMail();}

The first element of an array is numbered 0 instead of 1. This means that the highest number is one less than you might expect. Consider the following statement:

String[] topGifts = new String[10];

This statement creates an array of string variables numbered from 0 to 9. If you referred to topGifts[10] somewhere else ...

Get Sams Teach Yourself Java™ in 24 Hours, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.