Arrays serve to consolidate multiple values within a solitary variable, eliminating the need for individual variable declarations.
When declaring an array, indicate the variable type followed by square brackets:
String[] cars; |
We’ve defined a variable capable of holding an array of strings. To populate it, you can enclose the values within curly braces, separated by commas.
|
To generate an array of integers, you might express it as follows:
|
You can retrieve an array element by referencing its index number.
This statement retrieves the value of the initial element in the “cars” array.
|
Note that array indexes commence at 0: [0] represents the first element, [1] denotes the second element, and so forth. |
To modify the value of a particular element, reference its index number.
|
|
To determine the number of elements within an array, utilize the length property.
|