In java, the array is a data structure. The array is the collection of the same type of data stored in a group. In Java array used to store a collection of the same types of the variable.
In java, an array can store primitive data type (int, char, etc) as well as objects (Integer, String, etc).
Diagram of Array
In java, to access the elements of an array you need elements index, see the above diagram, each element have its index value
NOTE: Array indexing starts from 0(alert-success)
The syntax for accessing array element using an array index
array-variable[array index];(code-box)
Example
number[0];(code-box)
Java program of array
public class ArrrayInJava
{
public static void main(String[] args)
{
int[] number = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
System.out.print(number[0] + " ");
System.out.print(number[1] + " ");
System.out.print(number[2] + " ");
System.out.print(number[3] + " ");
System.out.print(number[4] + " ");
System.out.print(number[5] + " ");
System.out.print(number[6] + " ");
System.out.print(number[7] + " ");
System.out.print(number[8] + " ");
System.out.print(number[9] + " ");
}
}
Output of the array program
10 20 30 40 50 60 70 80 90 100(code-box)
Types of Array in java
There are two types of an array in java, the following are listed
- Single dimensional array
- Multidimensional array
Single Dimensional Array
Diagram of single-dimensional Array
Syntax of single dimensional array
access modifier type [] variable-name;(code-box)
OR
access modifier type variable-name[];(code-box)
Examples single dimensional array
private int[] numbers;
private String[] names;
private double[] salaries;
public Integer[] employeId;
Java Program of single dimensional array
public class SingleDimensionalArrray
{
public static void main(String[] args)
{
int[] numbers = {74, 45, 68, 92, 10};
System.out.println("Index o = " + numbers[0]);
System.out.println("Index 1 = " + numbers[1]);
System.out.println("Index 2 = " + numbers[2]);
System.out.println("Index 3 = " + numbers[3]);
System.out.println("Index 4 = " + numbers[4]);
}
}
Output single dimensional array program
Index 0 = 74
Index 1 = 45
Index 2 = 68
Index 3 = 92
Index 4 = 10(code-box)
We can also iterate or fetch array value using a loop. If you want to know more about loops in java then, read this Loop in java
Iterating arrays using for loop
public class SingleDimensionalArrray
{
public static void main(String[] args)
{
int[] numbers = {74, 45, 68, 92, 10};
for(int i=0; i<=numbers.length-1; i++)
{
System.out.println("Index " + i + " = " + numbers[i]);
}
}
}
Output of the java program when iterating arrays using for loop
Index 0 = 74
Index 1 = 45
Index 2 = 68
Index 3 = 92
Index 4 = 10(code-box)
Iterating using for each loop, also known as enhance for loop
public class SingleDimensionalArrray
{
public static void main(String[] args)
{
int[] numbers = {74, 45, 68, 92, 10};
for (int number : numbers)
{
System.out.println(number);
}
}
}
Output of the java array program when iterating using for each loop
74
45
68
92
10(code-box)
Multidimensional array
Diagram of Multidimensional array
Syntax of Multidimensional array
access modifier type [][] variable-name;(code-box)
OR
access modifier type variable-name [][];(code-box)
Examples of multidimensional array
private int[][] numbers;
public int numbers2[][];
Integer[][] number3;
String names[][];
The multidimensional array Java program and its iteration using the for loop are shown below. For each loop can also be used to iterate across an array.
public class MultiDimensionalArrray
{
public static void main(String[] args)
{
int[][] numbers = {{11,12,13,14}, {15,16,17,18}, {19,20,21,22}, {23,24,25,26} };
for (int i=0; i<=numbers.length-1; i++)
{
for (int j=0; j<=numbers.length-1; j++)
{
System.out.print(numbers[i][j] + " ");
}
System.out.println();
}
}
}
Output of the multidimensional array program
11 12 13 14
15 16 17 18
19 20 21 22
23 24 25 26
Summary of the above program
In the above MultiDimensionalArrray java program, we have declared a multidimensional array which is numbers
This program has a 4X4 matrix because the above program has four rows and four columns
We are iterating above, program using for loop, we have created two four-loop with int variable as i and j.
In above program, we have total 4 indexes, take a look in MultiDimensionalArrray diagram, so we are iterating first index as for (int i=0; i<=numbers.length-1; i++)
And in each index, we have 4 values, take a look into the diagram, to iterate each indexes value we are using the second loop which is for (int j=0; j<=numbers.length-1; j++)
And finally, print out the array
How it will iterate
first, i loop will initialize and will declare, after initialization i loop will check the condition which we have provided as i<=numbers.length-1
, If the statement will return true then, statement bock will execute
In the statement block of i loop, we have another loop which j, in j loop we are iterating indexes of the array. take a look in the diagram
Once j loop condition's return true then, the statement of j block will execute, which will print arrays values of index one by one
Methods in array
There are many methods in java array some of them are as follows.
Methods | Descriptions |
---|---|
equals(Object obj) |
equals(Object obj)
public boolean equals(Object obj) Indicates whether some other object is "equal to" this one. |
Arrays.asList(array) |
To convert the array into collection e.g list, set, etc NOTE: This method accepts one single argument of type Object which is your array. |
Arrays.toString(array) |
To convert the array into String NOTE: This method accepts one single argument of type Object which is your array. |
Arrays.stream(Object [] array) |
To convert the array into stream NOTE: This method accepts one single argument of type Object which is your array. |
Arrays.sort(Object [] array) |
To sort an array NOTE: This sort method acquires one single argument of type Object which is your array. |
Arrays.sort(Object[] array, int fromIndex, int toIndex) |
To sort an array from specific position NOTE: This sort method has three parameters such as an object, int, and int. array holds arrays and the fromIndex parameter is to indicate the starting point and toIndex is used to specify till where you want to sort. |
Conclusion
Today we learned, how to use array in java and how to iterate arrays using for loop as well as using for each loop. Don't stop here, explore more about array and methods in arrays more.
Thanks for reading and keep learning.