An array of Java

Source: Internet
Author: User

To define a method for an array:

int[] arr = new int [5]; ← New array defines the length of the array to be written in parentheses

Int[] arr1 ={1,2,3,4,5}; ← The contents of the defined array are directly written in curly braces

To assign a value to an array:

ARR[2] = 3; ← Directly write the index value in the array to give him the number of values on the line.

string[] STRs = new String [5]; ← A newly defined array of string types, if you do not fill in the contents, after the traversal output, each item inside is null. Returns 5 null after traversing the output.

Array sorting:

Int[] arr = {45, 34, 53, 43};

Arrays.sort (arr); ← Quick sort of array

System.out.println (arrays.tostring (arr)); ← Methods for outputting arrays in tool classes

Binary search method: (before using the binary search method, first to sort the array, using the. Sort method)

The binary search method in Java is called the binary lookup method, the array is arranged first, and then the arranged array is split into two parts from the middle position. Each time the value of the middle position is compared with the unknown Origin object, if the value of the middle position is larger than the unknown origin object, the first half of the lookup process is recycled, and if the median value is smaller than the unknown Origin keyword, the lookup process is cycled in the second half. There is no unknown origin keyword in the sequence until it is found.

int i = Arrays.binarysearch (arr, 34); ← Two parameters inside the parentheses, the left is the query from that array, the right is the query number, not find the words to return a negative.

System.out.println (i)

Binary search method range query a number:

Int J = Arrays.binarysearch (arr, 1, 3, 34); ← parameters are, which array, from index value 1, to index value 3 (not including 3, only to its previous index)

System.out.println (j);

Copying an array

. copyof Usage:

Int [] newArr = arrays.copyof (arr, 7); ← The left side of the argument is the original array, the right is the length of the new array (if the copied array is longer than the original, more than the 0 represented)

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

. Copyofrange Usage:

int[] newArr1 = Arrays.copyofrange (arr, 1, 3); ← 3 parameters, the first is which array, the last two parameters are the index values in the array, from where to, not including the final number of the index value (the last number minus 1).

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

An array of Java

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.