Using the Arrays class to manipulate arrays in Java
The Arrays class is a tool class provided in Java, in the Java.util package. The class contains methods for manipulating arrays directly, such as sorting, searching, and so on (for classes and methods), which are explained in detail in a later section.
Common methods in Arrays:
1. Sorting
Syntax: Arrays.sort (array name);
You can use the sort () method to sort the array, as long as the array name is placed in the parentheses of the sort () method, which you can sort (in ascending order), such as:
Operation Result:
2. Converting an array to a string
Syntax: arrays.tostring (array name);
You can use the ToString () method to convert an array to a string that joins multiple array elements sequentially, separated by commas and spaces between multiple elements, such as:
The result of the operation is:
Elements in the output array nums: [25,7,126,53,14,86]
Task: Implementation uses the Arrays class to sort the array and convert the output
//Import Arrays ClassImportJava.util.Arrays;//package must be imported Public classHelloWorld { Public Static voidMain (string[] args) {//defines a string arrayString[] Hobbys = {"Sports", "Game", "movie" }; //Use the sort () method of the arrays class to sort the arrayArrays.sort (Hobbys); System.out.println ("The elements in the sorted array:"); for(inti = 0;i) {System.out.println (hobbys[i]); } //Use the ToString () method of the arrays class to convert the array to a string and outputSystem.out.println (arrays.tostring (Hobbys)); }}
Elements in the sorted array: Gamemoviesports[game, movie, sports]
Array ordering for Arrays class operations Java