Related Topics

JAVA Programming
int[] myArray = {1, 2, 3, 4, 5};
In this example, myArray
is a single-dimensional array with 5 elements.
A multi-dimensional array, on the other hand, is an array that is composed of one or more arrays. Each array in the multi-dimensional array is itself a single-dimensional array. The elements of a multi-dimensional array are accessed using multiple indices. For example, the following code creates a two-dimensional array of integers:
int[][] myArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
In this example, myArray
is a two-dimensional array with 3 rows and 3 columns. Each row of the array is a single-dimensional array, and the elements of the multi-dimensional array are accessed using two indices. For example, myArray[1][2]
is equal to the value 6
.
Multi-dimensional arrays can have more than two dimensions. For example, a three-dimensional array is an array that is composed of one or more two-dimensional arrays, and the elements of the three-dimensional array are accessed using three indices. Similarly, a four-dimensional array is an array that is composed of one or more three-dimensional arrays, and the elements of the four-dimensional array are accessed using four indices, and so on.
The choice between using a single-dimensional or multi-dimensional array depends on the problem you are trying to solve. If you need to represent a simple list of values, a single-dimensional array is usually sufficient. If you need to represent a table of values or a matrix, a multi-dimensional array is more appropriate.
int[][] myArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
In this example, matrix
is a 2-dimensional array with 3 rows and 3 columns. Each row of the matrix is itself a 1-dimensional array containing 3 integers.
To access a specific element of the matrix, you need to use two indices: one for the row and one for the column. For example, to access the element in the second row and third column of the matrix, you would use the following code:
int element = matrix[1][2]; // element is 6
Here, the first index 1
refers to the second row of the matrix (remember that arrays in Java are zero-indexed, so the first row has an index of 0), and the second index 2
refers to the third column of the matrix.
Arrays.sort(array);
Here, array
is the array that you want to sort.
If you have an array of objects, you can also use the Arrays.sort()
method to sort the elements based on the natural ordering of the objects. In this case, the objects must implement the Comparable
interface. Here is an example of sorting an array of strings using the Arrays.sort()
method:
String[] names = {"Alice", "Bob", "Charlie", "David", "Eve"};
Arrays.sort(names);
After executing this code, the names
array will be sorted alphabetically in ascending order.
If you have an array of primitive types such as int
, double
, or char
, you can also use the Arrays.sort()
method to sort the elements. Here is an example of sorting an array of integers using the Arrays.sort()
method:
int[] numbers = {5, 3, 9, 1, 7};
Arrays.sort(numbers);
After executing this code, the numbers
array will be sorted in ascending order.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.