In this write-up, we will acknowledge
- What is an array in Java?
- What are the types of Arrays
What is an array in Java?
In Java, an array is a collection of corresponding data types. We can store a large quantity of data having the same data type in a solo variable. The most important thing is that an array is considered an object in Java because it uses a new keyword at the time of its creation. In Java, arrays have a superclass called Object class. Arrays use and occupy heap memory to store data.
Arrays are fast compared to primitive data types because primitive data types use internal conversion and wrapper classes, making them slow compared to arrays. Arrays are strongly typed, meaning we can only store identical data types.
Syntax:
In the syntax, data_types represent Integer, float, string, boolean, long, double, and short data types whereas variable represents the array name and lastly, array_elements represents the values of the array.
Code:
public static void main(String[] args) {
String[] arrs = {"My","Name","Is","Max","Fuler"};
int[] age = {23,40,27};
System.out.println(arrs[3]+ arrs[4] + " is " + age[0] + " years old.");
}
}
In the code above, we create two arrays arrs[], age[] of string and integer data types respectively.. Then we concatenate the specific elements from both arrays and request to display a message.
Output:
The output shows that we get the required result by creating and concatenating two arrays.
Types of Arrays in Java
In Java, an array has two types. Those array types are as follows
- One Dimensional Array
- Multi-Dimensional Array
One Dimensional Array
In a one-dimensional array, data can be stored in one way either in a single row or in a single column. In a one-dimensional array usually, the data is stored in a column. This array type contains a 1-D array.
Code:
public static void main(String[] args) {
String[] arrs = {"This","is","a","one","dimensional","array"};
for(String x : arrs)
System.out.println(x);
}
}
In this code, we create a string array and display it with the help of For Each loop.
Output:
In this output, it is clearly seen that the elements of a one-dimensional string array are displayed using For Each loop.
Multi-Dimensional Array
In a multidimensional array, data can be stored in multiple rows or columns. We can call a multi-dimensional array as an array inside the arrays. This array type contains a 2-D and a 3-D array.
2-D Arrays
In a 2-D array, data is stored in arrays and columns. A 2-D array is the one that points out another array by using a 1-D array. This array has further two types
- Matrix Array
- Jagged Array
Syntax:
In the above syntax, we represent a 2-D array by 2 square brackets after the data_type and we initialize it with data in multiple curly brackets and wrap those multiple curly brackets inside the single curly bracket.
Matrix Array
This 2-D array is said to be a matrix array if the array has an equal number of columns in each row.
Code:
public static void main(String[] args) {
String[][] arrs = {{"This","is","a"},{"2-D","matrix","array"}};
for(int m=0;m<arrs.length;m++)
{
for(int n=0;n<arrs[m].length;n++)
System.out.println(arrs[m][n]);
}
}
}
In this code, we create a 2-D string array with an equal number of columns. Then we use nested for loops, to display the elements of the 2-D matrix array.
Output:
The output shows that a 2-D matrix array is created and displayed successfully.
Jagged Array
This 2-D array is said to be a jagged array if the array does not have an equal number of columns in each row.
Code:
public static void main(String[] args) {
String[][] arrs = {{"This","is","a"},{"2-D"},{"jagged","array"}};
for(int m=0;m<arrs.length;m++)
{
for(int n=0;n<arrs[m].length;n++)
System.out.println(arrs[m][n]);
}
}
}
In this code, we create a 2-D string array having a different number of columns. Then we use nested for loops, to display the elements of the 2-D jagged array.
Output:
The output shows that the 2-D jagged string array is created. Then the nested for loops give us the required result.
So the difference between matrix and jagged array is that matrix array has equal number of columns whereas the number of columns in a jagged array are not equal.
3-D array
In a 3-D array, data is also stored in arrays and columns. A 3-D array is the one that points to other arrays by using a 2-D array.
Syntax:
In the above syntax, we represent a 3-D array by 3 square brackets after the data_type and we initialize it with data in multiple curly brackets and wrap those multiple curly brackets inside the double curly bracket.
Code:
public static void main(String[] args) {
String[][][] arrs = {{{"This","is","a"},{"3-D"},{"array"}}};
for(int m=0;m<arrs.length;m++)
{
for(int n=0;n<arrs[m].length;n++)
{
for(int p=0;p<arrs[m][n].length;p++)
System.out.println(arrs[m][n][p]);
}
}
}
}
In this code, we create a three dimensional string array and display its element with the help of three for loops.
Output:
The output shows that the 3-D string array is created. Then the nested for loops give us the required result.
Conclusion
In Java, an array is said to be a set of values having identical data types. An array has two types: single dimensional array(1-D) and multi-dimensional array(2-D/3-D). In this article, we have talked about arrays and their types in Java. Then we further discuss matrix arrays and jagged arrays.