In Java arrays are represented by a class called java.util.Arrays. It is a utility class, to define several utility methods to work with an array. The class Arrays is considered to be a static factory as this is having all static methods.
What is Java Arrays ?
Array is a container, which holds a fixed number of values in a single type. It is mandatory to define the size of an array at the time of initialization and the provided size is fixed, we can not change it later. Here are some important points about Java arrays.
- Java Arrays are index-based collections.
- The values in the array are stored in the memory from the left to right.
- The default values of the array. “0” in the case of primitives, “null” in the case of Objects, “false” in the case of boolean.
- Arrays are fixed length in size.
- Array is a homogeneous data structure so that an array can store uniform elements.
- The main advantage of an array is we can represent multiple values under the same name. So that readability of code is improved.
- To specify the array size, the allowed data types are byte, short, int and char, If we use any other data type the compiler will generate the exception.
Some of the disadvantages of using Arrays:
- The Arrays are in fixed length, we can change the length of an array once we declare it.
- The Array is a homogeneous data structure so that we can not store different types of data in a single array.
To overcome these above two disadvantages using Java collection framework.
How to Create Java Array?
Creating an array in Java is two-step process.
- Declaring of an array variable and
- Initialize an array object.
int array[]; // Declaring
array = new int[10]; // Initializing
Types of Java Array
- Single dimensional arrays
- Multidimensional arrays
- Anonymous arrays
Single dimensional arrays
Every array in Java is represented as an Object so that we can create an array by using new
operator.
int[] a = new int[10];
We can declare and assign values to an array in a single line like below :
int[] a = {5,10,60,50,70};
char[] c = {'a','b','c','d'};
At the time of initialization of an array, we should mention the size of it. Otherwise, we will get a compilation error.
int[] a = new int[]; // Compilation Error
int[] a = new int[-1]; // Runtime Exception (NegativeArraySizeException)
int[] a = new int[10]; // Legal
Java Array Single Dimensional Example :
public class ArrayDemo {
public static void main(String[] args) throws InterruptedException {
int[] array;
array = new int[10];
array[0] = 10;
array[1] = 8;
array[2] = 5;
array[3] = 15;
array[4] = 9;
array[5] = 7;
for (int i = 0; i < array.length; i++) {
System.out.println(" a[ " + i + " ]" + array[i]);
}
}
}
Output :
a[ 0 ] 10
a[ 1 ] 8
a[ 2 ] 5
a[ 3 ] 15
a[ 4 ] 9
a[ 5 ] 7
a[ 6 ] 0
a[ 7 ] 0
a[ 8 ] 0
a[ 9 ] 0
The above program is represented in the memory like below.
Java Multidimensional arrays
In Java, multidimensional arrays are represented as arrays of arrays. The main advantage of this approach is memory utilization will be improved.
<span style="color: #003300;">int[][] a = new int[3][];</span>
<span style="color: #003300;">a[0]=new int[2];</span>
<span style="color: #003300;">a[1]= new[1];</span>
<span style="color: #003300;">a[2] = new[3]</span>
The above example represented in memory
Array Single Line Declaration, Initialization
int[][] a={{1,5,6,7,},{10,20,30}};
If we want to use this type of declaration we should perform declaration, initialization in one line otherwise Compiler will generate an exception.
int[] x;
x={10,20,30}; // Illegal State Exception
Java Anonymous Arrays
In the case of anonymous arrays, we can specify the size. Otherwise, we will get a compilation error.
new int[]{5,10,15,20,25};
The main objective of anonymous arrays are just for instant usage.
public class AnonymousArrayDemo {
public static void main(String[] args) {
sum(new int[]{1,2,3});
}
public static void sum(int[] x){
int total=0;
for (int i : x) {
total +=i;
}
System.out.println("Total : "+total);
}
}
Output :
Total : 6
Happy Learning 🙂