A. Arrays in Java: An array is a data structure that stores a fixed-size sequential collection of elements of the same type. In Java, arrays can be of two types: single-dimensional and multi-dimensional.
- Single-dimensional Array: A single-dimensional array is an array that contains a fixed number of elements of the same type. It can be declared and initialized as follows:
int[] arr = new int[5]; // declares an array of 5 integers
arr[0] = 1; // assigns 1 to the first element of the array
arr[1] = 2; // assigns 2 to the second element of the array
arr[2] = 3; // assigns 3 to the third element of the array
arr[3] = 4; // assigns 4 to the fourth element of the array
arr[4] = 5; // assigns 5 to the fifth element of the array
- Multi-dimensional Array: A multi-dimensional array is an array that contains arrays as its elements. It can be declared and initialized as follows:
int[][] arr = new int[3][2]; // declares a 2-dimensional array of 3 rows and 2 columns
arr[0][0] = 1; // assigns 1 to the element at row 0, column 0
arr[0][1] = 2; // assigns 2 to the element at row 0, column 1
arr[1][0] = 3; // assigns 3 to the element at row 1, column 0
arr[1][1] = 4; // assigns 4 to the element at row 1, column 1
arr[2][0] = 5; // assigns 5 to the element at row 2, column 0
arr[2][1] = 6; // assigns 6 to the element at row 2, column 1
B. Array Operations in Java: Arrays in Java provide a wide range of operations that can be performed on them. Some of these operations are as follows:
- Length: The length property is used to get the length of an array. For example:
int[] arr = {1, 2, 3, 4, 5};
int length = arr.length; // length is 5
- Sorting: The sort() method is used to sort an array in ascending order. For example:
int[] arr = {5, 4, 3, 2, 1};
Arrays.sort(arr); // arr is now {1, 2, 3, 4, 5}
- Searching: The binarySearch() method is used to search for an element in a sorted array. For example:
int[] arr = {1, 2, 3, 4, 5};
int index = Arrays.binarySearch(arr, 3); // index is 2
- Copying: The copyOf() method is used to copy an array to a new array with a specified length. For example:
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = Arrays.copyOf(arr1, 3); // arr2 is now {1, 2, 3}
C. Enhanced for loop in Java: The enhanced for loop, also known as the for-each loop, is used to iterate over an array or collection. It simplifies the process of iterating over the elements of an array. The enhanced for loop is declared as follows:
for (datatype variable : array) {
// code to be executed for each element in the array
}
In this loop, the datatype is the data type of the elements in the array, variable is the variable that is used to store each element of the array in each iteration of the loop, and array is the array being iterated over.
Here is an example of the enhanced for loop being used with a single-dimensional array:
int[] arr = {1, 2, 3, 4, 5};
for (int num : arr) {
System.out.println(num);
}
This loop will iterate over the elements in the array and print them to the console.
Here is an example of the enhanced for loop being used with a multi-dimensional array:
int[][] arr = {{1, 2}, {3, 4}, {5, 6}};
for (int[] row : arr) {
for (int num : row) {
System.out.println(num);
}
}
This loop will iterate over the elements in the multi-dimensional array and print them to the console. The outer loop iterates over the rows in the array, and the inner loop iterates over the elements in each row.