Java Array Learning Notes

Source: Internet
Author: User

Find their basic knowledge is not solid enough, slowly review!

Array creation:

Mode one: type[] Variable name = new type[element number]; -such as: int[] a = new int[10];

Mode two: type variable name [] = new type[element number]; -such as: int a[] = nwe int[10];

Way three: type[] a= New type[]{initialization value, comma delimited}--such as: int[] a=new int[]{1,2,3}

The initialization value cannot be defined at the same time-for example: int[] a=new int[3]{1,2,3}

Multidimensional Arrays:

  Multidimensional arrays, the first dimension must be specified, and must be defined by high dimensional to the low dimension--for example: int[][] a=new int[][3];

Type[][] A=new type[2][3];

Type a[][]=new type[3][2];

Int[][] A=new int[][]{{1,2,3},{2,2},{3,2,1}}

Length

Read-only member length is part of an array object and is the only field or method that can be accessed. [] is the only way to access an array object.

Multidimensional arrays, length can only calculate the first dimension length.

Assignment references:

When the Java array is initialized, it has only a reference to the array and does not allocate storage space to the arrays. Therefore, replication between arrays cannot be simply assigned with "=" because the same object is being manipulated.

  

   The results show:

    

    Change the value of arrayb[0], change the reference array, so we output ARRAYA[0], in fact, and Arrayb[0] the same.

Array replication:

1.for Cycle

2.system.arraycopy (SRC, Srcpos, dest, destpos, length);

SRC: source array;
Srcpos: The starting position of the source array to be copied;
Dest: an object array;
Destpos: The starting position of the destination array placement;
Length: The size of the copy.

  --system.arraycopy (arraya,0,arrayb,0,1), which is faster than a for loop, but the object array is simply a copy reference and will not have two copies.

and System.arraycopy () does not automatically wrap and unpack, require arrays of the same type, or convert to the same type.

Array comparison:

Arrays.equals (Arraya, Arrayb); equality Condition: The number of array elements is equal, the corresponding elements are equal

Multidimensional array comparison: Arrays.deepequals (Arraya, Arrayb); two arrays must be of the same type

Java Array Learning Notes

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.