Programmingempire
This article explains Using Arrays in Java. Basically, an array is a data type that contains multiple elements of the same data type.
Declaring Arrays in Java
The following code shows that how to declare arrays in Java.
Syntax:
data_type array_name[];
or
data_type[] array_name;
Since, arrays are dynamically, allocated in Java, we need to use the new operator, as shown in the following examples.
Examples:
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException {
// Array of integers
int[] arr1=new int[5];
// Array of strings
String arr2[]=new String[4];
// Array of user-defined type
MyClass[] arr3=new MyClass[10];
}
class MyClass{
int a, b;
public MyClass(int a, int b)
{
this.a=a;
this.b=b;
}
}
}
As can be seen in the above example, we can create arrays of both primitive types and user-defined types. Also, the above example demonstrates creating the one-dimensional array. Further, we can also create an array by initializing its elements. The following example demonstrates initializing an array.
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException {
// Array of integers
int[] myarray={1,3, 8, 12, 9, 6};
//Display array elements
for (int k : myarray){
System.out.print(k+" ");
}
}
}
As shown above, the array named myarray is created by assigning initial values. Also, the number of values used for initialization provides the size of the array. Hence, in this example, the array size is 6.
Output
In fact, arrays in java have a built-in property called length that returns the number of elements in the array. For instance, we can find the length of array as follows,
int[] myarray={1,3, 8, 12, 9, 6, 2, 1, 3, 7, 9};
System.out.println(myarray.length);
Output: 11
Using Two-Dimensional Array in Java
The following example shows how to use a two-dimensional array in Java.
public class Main
{
public static void main(String[] args){
// A 2D Array of integers
int[][] m1=new int[][]{{6,1,9}, {4,7,8}, {12,3,91}, {5,2,1}, {12, 6, 18}, {4, 7, 34}};
System.out.println(m1.length);
// Display a 2D Array
System.out.println("A 2D Array....");
for(int i=0;i<m1.length;i++)
{
for(int j=0;j<m1[i].length;j++){
System.out.print(m1[i][j]+" ");
}
System.out.println();
}
}
}
Since in the above example, the array is created by initializing, rather than specifying its size, we need to find the size of both dimensions. Therefore, we can use the length property. Within the outer for loop, the length property returns the total number of rows. While, in the inner loop, we can find the number of columns in each row by using the length property by specifying the index of each row. For example, m1[i].length.
Output
Jagged Arrays in Java
Besides regular arrays, we can also have jagged arrays in Java. Basically, a jagged array can have a different number of columns in each row. The following example shows how to use a jagged array.
public class Main
{
public static void main(String[] args) {
// A Jagged Array
int[][] jagged_array1=new int[4][];
jagged_array1[0]=new int[]{5, 1, 9};
jagged_array1[1]=new int[]{8, 23};
jagged_array1[2]=new int[]{9, 1, 67, 45, 90, 3, 23, 56, 4, 78};
jagged_array1[3]=new int[]{6, 1, 90, 5};
// Display a 2D Array
System.out.println("A Jagged Array....");
for(int i=0;i<jagged_array1.length;i++)
{
for(int j=0;j<jagged_array1[i].length;j++){
System.out.print(jagged_array1[i][j]+" ");
}
System.out.println();
}
}
}
Output