Use of Java arrays

Source: Internet
Author: User

One, the Declaration and allocation of arrays

Int[] scores = new INT[5];

Second, the assignment value:

Int[] scores = {35,58,95,56,12};

Third, use the loop operation array:

The array name. length is used to get the lengths of the array

Four, using the Arrays class operation array:

Arrays class in the Java.util package, you need to refer to the

Common methods of the arrays class:

1. Sorting

Arrays.sort (array name);

2. Converting an array into a string

Arrays.tostring (array name);

3. Assigning values to arrays

Arrays.fill (array name, value);//each element in the array is assigned value

Arrays.fill (array name, fromindex,toindex,value);//the assignment from coordinates fromIndex to toIndex-1 in an array is value

4. Compare whether elements are equal

Arrays.equals (array name 1, array name 2);

5. Finding the range of elements using the dichotomy method

Arrays.binarysearch (array name, value);//value the coordinates in the array, returns a negative number if it does not exist

V. Manipulating arrays with foreach

for (element type element variable: Traverse object)

{

Code to execute

}

#思考问题 # What do you do if you want to get the subscript of an array element in a foreach statement?

Six or two-D arrays

Use a double loop to iterate through the array.

Use of Java arrays

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.