The Java array is divided into two main types which then expands in different varieties of arrays. Here's a breakdown of the type of arrays:
Regular Array: Regular array is further divided into 1-dimensional, 2- 2-dimensional, 3-dimensional arrays, and so on.
Jagged Array: Jagged is an array of arrays such that member arrays can be of different sizes, i.e., we can create 2-D or 3-D arrays but with a variable number of columns in each row. These types of arrays are also known as Jagged arrays.
One-Dimensional (1D) Array
A 1D array is a list of elements stored in a single row. It is used to store multiple values of the same type in a linear format.
Syntax:
dataType[] arrayName = new dataType[size];
or
dataType[] arrayName = {value1, value2, value3, ...};
Example:
A list of students in a single class.
class MasterBackend {
public static void main(String[] args) {
// 1D Array: Students in a single class
String[] students = {"Ayush", "Rahul", "Neha", "Priya"};
System.out.println("Students in a class:");
for (String student : students) {
System.out.println(student);
}
}
}
Output:
Students in a class:
Ayush
Rahul
Neha
Priya