An Array is a collection of values (or objects) of similar data types (primitive and reference, both form of data types are allowed) held in sequential memory addresses.
Here, `datatype[]` describes that all the variables stated after it will be instantiated as arrays of the specified datatype. So, if we want to instantiate more arrays of the similar datatype, we just have to add them after the specified `arrayName`(don't forget to separate them through commas only). An example is given below in the next section for reference.
Here, `datatype` describes only that the variables stated after it belong to that datatype. Besides, `[]` after the variable name describes that the variable is an array of the specified datatype (not just a value or object of that datatype). So, if we want to instantiate more arrays of the similar datatype, we will add the variables names just after the one already specified, separated by commas and each time we will have to add `[]` after the variable name otherwise the variable will be instantiated as an ordinary value-storing variable (not an array). For better understanding an example is given in the next section.
Above code snippet an array of datatype double named list1 and a simple variable of datatype double named list2 (Don't be confused by the name **list2**. Variables names have nothing to do with the type of variable).
Note: The style `double list[]` is not preferred as it comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers. Additionally it's more readable to use the style `double[] list`: you can easily read that it's a "double array named list" other than "a double called list that is an array."
Here we have declared and initialized the array in one step. We could have also written it in two parts with one step being the declaration of array followed by the initialization of array. By default, all memory locations allocated to the array is initialized to its default values, depending upon the datatype.
We are creating a array variable named `List` of type double and allocating it 10 memory locations. This double datatype array is initialized to `0.0` by default.
It's possible to find the number of elements in an array using the `length` attribute. It should be noted here that `length` is an **attribute** of every array, i.e., a variable storing the length of the array. It must not be confused for a **method** of Array since the name is same as the `length()` method corresponding to String classes.
list[1] = 3; // now, if you access the array like above, it will output 3 rather than 2
```
_Example of code:_
```java
int[] a = {4, 5, 6, 7, 8}; // declare array
for (int i = 0; i <a.length;i++){//loopgoesthrougheachindex
System.out.println(a[i]); // prints the array
}
```
Output:
```java
4
5
6
7
8
```
### Multi-dimensional Arrays
Two-dimensional arrays (2D arrays) can be thought of as a table with rows and columns. Though this representation is only a way to visualize the array for better problem-solving. The values are actually stored in sequential memory addresses only.
```java
int M = 5;
int N = 5;
double[][] a = new double [M][N]; //M = rows N = columns
for(int i = 0; i <M;i++){
for (int j = 0; j <N;j++){
//Do something here at index
}
}
```
This loop will execute M ^ N times and will build this:
Similarly a 3D array can also be made. It can be visualized as a cuboid instead of a rectangle(as above), divided into smaller cubes with each cube storing some value. It can be initialized follows:
In a similar manner, one can declare an array of as many dimensions as desired, although visualizing an array of more than 3 dimensions is difficult.
Tip: Arrays like this can be quite confusing, especially to beginners, so if you are planning to use this, be prepared to make a visual representation either in your head or on pen and paper.
Jagged arrays are multi-dimensional arrays that have a set number of rows but a varying number of columns. Jagged arrays are used to conserve memory use of the array. Here is an example:
- **Code Optimization**: It makes the code optimized, we can retrieve or sort the data efficiently.
+ **Random access**: We can get any data located at an index position.
**Disadvantages of Arrays**
- **Size Limit**: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in Java which grows automatically.