How can we implement array sorting using Java code?

You can use the sort method of the Arrays class in Java to arrange Arrays in order. The specific steps are as follows:

  1. Import the java.util.Arrays class.
import java.util.Arrays;
  1. Define an array.
int[] arr = {5, 9, 3, 1, 6};
  1. Sort the array using the sort method of the Arrays class.
Arrays.sort(arr);
  1. Output the sorted array.
System.out.println(Arrays.toString(arr));

Here is the complete code:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] arr = {5, 9, 3, 1, 6};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}

The result of the operation is:

[1, 3, 5, 6, 9]
广告
Closing in 10 seconds
bannerAds