Source: http://www.imooc.com/code/1556
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
Grammar:
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
Grammar:
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:
Operation Result:
The Arrays class also provides a number of other ways to manipulate the array here is not listed here, and you guys can read more on the wiki.
Task
Pro, the use of Arrays class you have mastered it, let us examine it.
An array hobbys is defined in the editor, please complete the code in sections 2, 11, and in line with the Arrays class array to sort and transform the output.
Operation Result:
1 //Import Arrays Class2 3 4 Public classHelloWorld {5 Public Static voidMain (string[] args) {6 7 //defines a string array8String[] Hobbys = {"Sports", "Game", "movie" };9 Ten //Use the sort () method of the arrays class to sort the array One A - //Use the ToString () method of the arrays class to convert the array to a string and output - System.out.println (); the } -}
Myans:
1 //Import Arrays Class2 Importjava.util.Arrays;3 4 Public classHelloWorld {5 Public Static voidMain (string[] args) {6 7 //defines a string array8String[] Hobbys = {"Sports", "Game", "movie" };9 Ten //Use the sort () method of the arrays class to sort the array One Arrays.sort (hobbys); A - //Use the ToString () method of the arrays class to convert the array to a string and output - System.out.println (arrays.tostring (Hobbys)); the } -}
-java first season-6-7 using the Arrays class to manipulate arrays in Java