Array Basics
Array Basics
An array is a container object that holds a fixed number of values of a single type. We do not need to create different variables to store many values, instead we store them in different indices of the same objects and refer them by these indices whenever we need to call them.
Syntax:
Note: array indexing in java starts from [0].
There are two types of array in java:
- Single Dimensional Array
- Multi-Dimensionalal Array
We will see how to perform different operations on both the type of arrays,
A. Length of an array:
Finding out the length of an array is very crucial when performing major operations. This is done using the .length property:
Example:
Output:
B. Accessing array elements:
Array elements can be accessed using indexing. In java, indexing starts from 0 rather than 1.
Example:
Output:
C. Change array elements:
The value of any element within the array can be changed by referring to its index number.
Example:
Output: