- An array is a collection of variables of the same type.
- A variable declared in brackets, [], is an array reference.
- Three steps
String username[];
float[] balance;
- Components of an array are accessed by a simple integer index.
- Element indexing begins with the number 0.
username[0] = "Fred";
username[1] = "Barney";
- The size of an array is easily accessed with the length attribute.
for (int i=0; i<username.length; i++)
{
System.out.println("The user's name is: " + username[i]);
}
- Indexing past the end of an array throws an exception.
- Multidimensional arrays can be created.
|
|