Operation of an array of Java arrays and arrays classes

Source: Internet
Author: User
Tags array length

There are two ways to initialize an array

    • Static initialization: The initial value of each array is displayed by the programmer at initialization, and the array length is determined by the system. Such as:
int New int [] {1,2,3,4};
    • Dynamic initialization: When initialized, only the length of the array is specified by the programmer, and the system assigns the initial value to the element. Such as:
int New int [5];

Write a class test.

 PackageChenlizhi;Importjava.util.Arrays; Public classTestarrays { Public Static voidMain (string[] args) {int[] A1 =New int[] {4,3,2,1}; int[] A2 =New int[] {6,5,7,8}; //Sort AscendingArrays.sort (A1);                System.out.println ("sorting the A1 array results to:" + arrays.tostring (A1)); //Arrays.binarysearch (A1, 4) Look for an array of values that are worthy of subscript if it is necessary to sort or not be sure if there is a duplicate return first if there is no such worth to return a negative numberSystem.out.println ("Find the value of 4 in the A1 array is labeled as:" +arrays.binarysearch (A1, 4)); //Arrays.binarysearch (A1, 1, 4, 4) Look up the array from 1 to 4 subscript if there is no value of 3 if there is a return subscript if there is no return negative if the precondition must be sortedSystem.out.println ("Find A1 array starts from subscript 1 to subscript 3 end, see if there is a value of 3" + Arrays.binarysearch (A1, 1, 3, 3)); //arrays.copyof (A1, 5) copy array A1 to the length of the copied array 5 for the new array        int[] a3 = arrays.copyof (A1, 5); System.out.println (The value of the A3 array is: "+arrays.tostring (A3)); //same as above. A1 bit copied array 2 is the A1 array begins subscript 3a1 the subscript        int[] a4 = arrays.copyofrange (A1, 2, 3); System.out.println (The A4 array value is: "+arrays.tostring (A4)); //See if the length and array elements of two arrays are the same if they are the same return true otherwise falseSYSTEM.OUT.PRINTLN ("see if the A1 array and the A2 array are the same:" +Arrays.equals (a1, a2)); //change all values in the A1 array to 1Arrays.fill (A1, 1); System.out.println (The value of the output A1 array is: "+arrays.tostring (A1)); //change the value of the A2 array from subscript 1 to subscript 3 to 1Arrays.fill (A2, 1, 3, 1); System.out.println (The value of the output A2 array is: "+arrays.tostring (A2)); }}

The output is:

The A1 array is sorted by: [1, 2, 3, 4]
Find the value of 4 in the A1 array subscript: 3
Find the A1 array from subscript 1 to subscript 3 end to see if there is a value of 3 2
The values of the A3 array are: [1, 2, 3, 4, 0]
The A4 array value is: [3]
See if the A1 array and the A2 array are the same: false
The value of the output A1 array is: [1, 1, 1, 1]
The value of the output A2 array is: [6, 1, 1, 8]

Operation of an array of Java arrays and arrays classes

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.