-java first season-6-7 using the Arrays class to manipulate arrays in Java

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.