Java array Learning

Source: Internet
Author: User

Define an array:

1. Int [] arr = {90, 80, 70 ....}

2. Int [] arr = new int [10];

3. Int [] arr = new int [] {90, 80, 70 ,...}

 

In Java, when the = operation is used for the basic data type, the value is copied to the variable, but when it is used for the object, the object is specified to the reference name for reference.

 

Copy an array:

1. Cyclic Replication

2. system. arraycopy (source, start index, destination, start index, and copy length );

3. copyof ();

 

Arrays class:

The arrays class is located in the Java. util package. It provides some methods for direct use:

 

Name

Description

Sort ()

To help you sort the specified array, the quick sort method is used.

Binarysearcy ()

Binary Search for sorted arrays. If a specified value is found, the index of the value is returned. Otherwise, a negative value is returned.

Fill ()

After an array is configured, the default value is given based on the array type.

Equals ()

Checks whether all elements in the two arrays are equal. If yes, true is returned. Otherwise, false is returned.

Deepequals () Deep comparison of arrays, applicable to arrays larger than two dimensions
Deeptostring () Deep output of arrays, applicable to arrays larger than two dimensions

 

You cannot use = to compare whether the element values of the two arrays are equal, is used to test whether two object names reference the self-consent object, that is, to test whether two names are bound to the consent object.

 

Foreach and array:

The foreach syntax has been added since j2se 5.0, also known as the enhanced for loop:

For (type element: array)

For example:

Int [] arr = {1, 2, 3 ,}

For (INT element: ARR)

System. Out. println (element );

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.