This Java code defines a class named ArrayMethods
that contains a main
method.
The main
method creates an array myArray
and initializes it with the values {6, 4, 9, 1, 3, 7}
.
Next, the code calls the Arrays.sort
method to sort the array myArray
in ascending order. The Arrays.sort
method modifies the original array in place and sorts its elements in ascending order.
Finally, the code outputs the sorted array by using a for loop to iterate over the elements of the array and printing each element followed by a space. The code calls System.out.print
with the current array element and the string ” ” to print each element. The for loop continues until it has iterated over all elements of the array.
import java.util.Arrays;
public class ArrayMethods {
public static void main(String[] args) {
int[] myArray = { 6, 4, 9, 1, 3, 7 };
Arrays.sort(myArray);
for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + " ");
}
}
}
1 3 4 6 7 9