An array is a type of data structure. It is simply a collection of a same type of data. For example, an int array could store some number of the primitive data type int. To reference a specific variable array, we use indexes as follows:
array[index]
Where index is the index of the variable in the array. Note that arrays start at index 0, i.e. the first element is array[0].
You can declare an array in the following ways:
- Manually setting every value:
dataType[] array = {datatype1, datatype2, datatype3}; //where the datatype variables represent elements of the data type of the array
- Creating an empty array of a set length:
dataType[] array = new dataType[3]; //creates 3 spaces (0, 1, 2) of the default value of the data type specified
Arrays are immutable, so their length cannot be changed once it is initialized. To find the length of an array, simply use arrayName.length. However, the indexed variables could be mutable, so they can be changed.
Homework: https://docs.google.com/forms/d/e/1FAIpQLSeti87omRmO_-Z7vuaC-jSpcyHvEnnbfmFO34qfHIOKjfIdJw/viewform?usp=sf_link