Arrays hold a predefined amount of elements of a predefined type. They're length cannot be changed. You can however change the values of the individual elements. To access the individual elements you have to use indexes. An arrays indexes start at 0 and go to 1 less than the length. So an int array that has a length of 5 has indexes from 0-4. It may be helpfull to iterate through all the elements in an array. You can do this with for loops and the array length.
To define an array you type: x[] y = new x[z]; where x is the type, y is the name, and z is the length.
To find the length you type: y.length; where y is the name.
To access the elements you type: y[z]; where y is the name and z is the index.
To iterate through an array type: for (int i = 0; i < y.length; i++) {} where y is the array.

No comments:
Post a Comment