Arrays class Application • Use the arrays class to manipulate arrays in Java __java

Source: Internet
Author: User
manipulating arrays in Java using the Arrays class

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 and searching directly, and so on (the related content of classes and methods is explained in detail in later chapters).

Common methods in Arrays:

1, sorting

Syntax: arrays.sort (array name);

You can use the sort () method to sort the array, as long as you place the array name in parentheses in the sort () method, and you can sort it (in ascending order), such as:

Run Result:

2. Convert 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, such as:

The results of the operation are:

the elements in the output array nums: [25,7,126,53,14,86]

The Arrays class also provides a number of other ways to manipulate an array. Here's a list of all the little friends you can look for in the wiki more information tasks

Pro, the use of Arrays class you have mastered it, let's examine it.

An array hobbys is defined in the editor, please fill in the code in 2, 11, line, and implement the sort using the Arrays class and convert the output.

The results of the operation are: [game, movie, sports]

Import the Arrays class import
java.util.Arrays;

public class HelloWorld {public
    static void Main (string[] args) {
        
		//define an array
		of strings string[] Hobbys = {"Sports", "Game", "movie"};
        
		Use the sort () method of the Arrays class to sort the array
		arrays.sort (Hobbys);
        
		Converts an array to a string and outputs
		System.out.println (arrays.tostring (Hobbys)) using the ToString () method of the Arrays class           ;

Arrays class Application

The Arrays class is a class provided in the Java API, in Java.util packages, the methods provided in the Arrays class can directly implement array sorting, searching, and so on. Common methods are as follows:

1, toString

Transform array: Converts the specified array contents to a string form

2, fill

Assigning values to arrays: through the Fill method.

3, Sort

Array sorting: By the Sort method, the default is ascending.

4, equals

Compare arrays: Compare the values of elements in an array by the Equals method for equality.

5, BinarySearch

Binary lookup: Use the binary search method to search the range of the specified array

Sample code:

import java.util.Arrays;

Public class HelloWorld {

Public static void main (string[] args) {

int [] array = new int[5];

Populating arrays

/* Arrays.fill (A1, value);

Function: Fill the A1 array with each element is value

*/

Arrays.fill (array, 5);

System.out.println ("Populate array: Arrays.fill (Array, 5):");

System.out.println (arrays.tostring (array));

Assign the 3rd and 4th elements of an array to 8

/* Arrays.fill (A1, Fromindex, Toindex,value);

Function: Fills the A1 array from index to Fromindex to index to toIndex-1 each element is value

*/

Arrays.fill (Array, 2, 4, 8);

SYSTEM.OUT.PRINTLN ("assigns the 2nd and 3rd elements of the array to 8:arrays.fill (array, 2, 4, 8):");

System.out.println (arrays.tostring (array));

int [] Array1 = {7, 8, 3, 2, 12, 6, 3, 5, 4};

Sort 3rd to 7th of the array

/* Arrays.sort (A1, Fromindex, Toindex);

Function: To sort each element in the A1 array starting from index to Fromindex to index to toIndex-1

*/

Arrays.sort (Array1, 2, 7);

System.out.println (the 2nd to 6th elements of an array are sorted: Arrays.sort (array,2,7): ");

System.out.println (arrays.tostring (array1));

Sort the entire array

Arrays.sort (array1);

System.out.println ("Sort the entire array: Arrays.sort (array1):");

System.out.println (arrays.tostring (array1));

Compare array elements for equality

System.out.println ("Compare the equality of array elements: Arrays.equals (Array, array1):" + \ n "

+ arrays.equals (array, array1));

int [] array2 = Array1.clone ();

SYSTEM.OUT.PRINTLN ("Array elements are equal after cloning: Arrays.equals (Array1, Array2):" + "\ n"

+ arrays.equals (array1, array2));

Use the binary search algorithm to find the subscript that contains the specified element (must be sorted, otherwise the result is incorrect)

Arrays.sort (array1);

System.out.println ("The position of element 3 in Array1: Arrays.binarysearch (Array1, 3):"

+ "\ n" + arrays.binarysearch (array1, 3));

Returns a negative number if it does not exist

System.out.println ("The position of element 9 in Array1: Arrays.binarysearch (array1, 9):"

+ "\ n" + arrays.binarysearch (array1, 9));

}

}

Run Result:

Populating arrays: Arrays.fill (Array, 5):
[5, 5, 5, 5, 5]
Assigns the 2nd and 3rd elements of an array to 8:arrays.fill (array, 2, 4, 8):
[5, 5, 8, 8, 5]
The 2nd to 6th elements of an array are sorted: Arrays.sort (array,2,7):
[7, 8, 2, 3, 3, 6, 12, 5, 4]
Sort the entire array: Arrays.sort (array1):
[2, 3, 3, 4, 5, 6, 7, 8, 12]
Compare array elements for equality: arrays.equals (Array, array1):
False
Whether the array elements are equal after cloning: Arrays.equals (Array1, array2):
True
The position of element 3 in Array1: Arrays.binarysearch (Array1, 3):
1
The position of element 9 in Array1: Arrays.binarysearch (array1, 9):
-9



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.