Java array and arrays class operations on arrays, java array arrays class

Source: Internet
Author: User

Java array and arrays class operations on arrays, java array arrays class

Two methods are available for Array initialization.

  • Static initialization: during initialization, the programmer displays the initial values of each array, and the system determines the length of the array. For example:
int[] a1 = new int[] {1,2,3,4};
  • Dynamic initialization: during initialization, the programmer only specifies the length of the array, and the system assigns the initial value to the array element. For example:
 int[] a = new int[5];

 

Write a class and test it.

Package chenlizhi; import java. util. arrays; public class TestArrays {public static void main (String [] args) {int [] a1 = new int [] {4, 3, 2, 1 }; int [] a2 = new int [] {6, 5, 7, 8}; // sort Arrays in ascending order. sort (a1); System. out. println ("sorting the a1 array:" + Arrays. toString (a1); // Arrays. binarySearch (a1, 4) searches for the subscript in the array. The premise is that it must be sorted. Otherwise, it is uncertain. If there are duplicates, return the first one. Otherwise, return the negative number System. out. println ("Search for the subscript of 4 in the a1 array:" + Arrays. binarySearch (a1, 4); // Arrays. binarySearch (a1, 1, 4, 4) finds whether there are 3 values in the array from 1 to 4 subscripts. If any returns subscript. If no negative values are returned, the System must be sorted. out. println ("Search for the a1 array from subscript 1 to subscript 3 and check whether there is a value of 3" + Arrays. binarySearch (a1, 1, 3, 3); // Arrays. copyOf (a1, 5) Copies array a1 as the copied array 5 as the new array length int [] a3 = Arrays. copyOf (a1, 5); System. out. println ("a3 array value:" + Arrays. toString (a3); // The a1-bit copied array 2 is the starting subscript of the a1 array. 3a1 is the ending subscript of the array. int [] a4 = Arrays. copyOfRange (a1, 2, 3); System. out. println ("a4 array value:" + Arrays. toString (a4); // check whether the length and array elements of the two arrays are the same. If both are the same, true is returned. Otherwise, false System is returned. out. println ("check whether the a1 array and a2 array are the same:" + Arrays. equals (a1, a2); // replace all values in the a1 array with 1 Arrays. fill (a1, 1); System. out. println ("output a1 array value:" + Arrays. toString (a1); // Replace the subscript 1 to subscript 3 in the a2 array with 1 Arrays. fill (a2, 1, 3, 1); System. out. println ("output a2 array value:" + Arrays. toString (a2 ));}}

Output result:

The result of sorting the a1 array is: [1, 2, 3, 4]
Search for the subscript of 4 in the a1 array: 3
Search for a1 array from subscript 1 to subscript 3 and check whether there is a value of 3 2
A3 array value: [1, 2, 3, 4, 0]
A4 array value: [3]
Check whether the a1 array and a2 array are the same: false
Output a1 array value: [1, 1, 1, 1]
Output a2 array value: [6, 1, 1, 8]

 

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.