Java->arrays class

Source: Internet
Author: User

Concept

This class contains various methods for manipulating arrays, such as sorting and searching. It is important to note that if the specified array reference is null, access to methods in this class throws a null pointer exception NullPointerException.

Common Methods

Sort method for sorting elements in the specified array ( element values are sorted from small to large )

The source arr array element {1,5,9,3,7}, sorted after the arr array element is {1,3,5,7,9}

Int[] arr = {1,5,9,3,7};

Arrays.sort (arr);

tostring Method that returns the string form of the contents of the specified array element

Int[] arr = {1,5,9,3,7};

String str = arrays.tostring (arr); the value of STR is [1, 3, 5, 7, 9]

The binarysearch method, in the specified array, to find where the value of the given element appears. If no query is reached, the return position is-1. Requires that the array must be an ordered array .

Int[] arr = {1,3,4,5,6};

int index = Arrays.binarysearch (arr, 4); The value of index is 2

int index2= Arrasy.binarysearch (arr, 2); The value of Index2 is-1

methods of arrays class practice

Defines a method that receives an array that stores 10 student test scores, which require the return of the last three test scores with the lowest test score.

public static int[] method (double[] arr) {

Arrays.sort (arr); Sorting array elements (element values are sorted from small to large)

Int[] result = new INT[3]; Three test scores after storage

system.arraycopy (arr, 0, result, 0, 3);//Copy the first 3 elements of the ARR array into the result array

return result;

}

Java->arrays class

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.