Java Arrays Introduction
In Java arrays can store a single value in the form of a variable ( for example int x = 10; or String name = “Java”; ). In this way we can store a single element or a value in a variable and we can access that value by calling that variable. What if our requirement needs a 1000 values while programming , creating 1000 variables for those 1000 values will be complex and accessing those 1000 variables in a problem represents hard coded. So Java made it easy with the help of ‘Arrays‘.
- An Array is a container object that holds fixed number of elements of same type.
- For example int numbers = new int; here the size of the array is ‘100‘
- The length of the array is established when it is created , after creation it’s length is fixed. Here we created an array ‘numbers‘ of length 10.
- We can store 10 numbers representing as a single variable “numbers“.
- The values are stored in the form of indexes , these indexes are useful to search for a number.
- Array index starts from 0 to (n-1) , Each item in an array is called ‘element’ and each element is accessed by it’s numerical index .
- As shown in the figure always the array index starts from 0 , so if we would like to access the 9th element in the ‘numbers‘ array , the 9th element is located in the index value 8.
- Arrays are of two types One-Dimensional Array and Multi-Dimensional Array
- A One-Dimensional Array is a list of like-types variables. To create an array , you first must create an array variable of the desired type. The general form of the one-dimensional array is int numbers = new int;
- Multi-Dimensional arrays are actually arrays of arrays. Multi-Dimensional array declaration generally look like int twoD = new int;