Java Array Summary

Source: Internet
Author: User

1. Basic concepts and functions of arrays: Arrays are collections of elements of the same data type, and the array itself is a reference data type, that is, an object. But arrays can store basic data types, or they can store reference data types.

For example:int [] a = new int []{1,2,3,4,5};

string [] s = new string []{" Bear " , " little Bear ", " Little Bear" };

Employee [] e=new employee[10]; ( Employee is a custom class)

2. declaration of an array: two ways of declaring an array (typically the first):

① data Type [] array name for example:int [] A;

② the name of the array of data types [] For example: int a [];

3. Array creation: Three ways to create an array:

① allocates memory based on the specified length while declaring the array, but the element values in the array are the default initialization values. Example:char[] chary = new CHAR[10];

② the array and allocates memory while initializing it. For example:int[] ary1 = new Int[]{1, 2, 3, 4, 5};

③ is the same as the previous one, just a relatively brief syntax. For example:int[] Ary2 = {1, 2, 3, 4, 5};

4. length of the array: Long property. For example:int [] B1 = new int []{1,2,3,4,5,6,7};

System.out.println (b1.length);

① You must specify the length of the array when you create the array, and it is not allowed to change once defined.

The length of the ② array, although 7, is actually given in memory to 8 locations, and the other one stores 7.

5. assignment between arrays: Direct Assignment, but the actual effect is that two array references point to the same piece of memory.

6. access to arrays: Array name [ index ] For example: a[2];

Note:The index of the ① array starts at 0 .

The data type of the ② index is an integer type.

③ index maximum and array length is always 1.

7. iteration of an array: there are two ways. For example:

int [] B1 = new int []{1,2,3,4,5,6,7};

The first type: the For loop iterates over the array.

for (int i =0;i<b1.length;i++) {

System.out.println (B1[i]);

}

Second: Enhance The iteration of the For Loop array.

for (int x:b1) {

SYSTEM.OUT.PRINTLN (x);

}

8. Declaration of multidimensional Arrays : There are three ways:①int [] A;   ②int []a1[]; ③int a2[][];

9. iteration of a multidimensional array:int [[] b1 = new int [][]{{1,2},{3,4}};

for (int i =0;i<b1.length;i++) {

for (int J =0;j<b1[i].length;j++) {

System.out.print (B1[i][j]);

}

}

copy of the array:arraycopy (object src1, int srcpos, object src2, int destpos, int length)
from the specified source array Src1 Copies an array, copying from the specified location Srcpos start, to target array Src2 end of the specified position.

For example: int[] a={10,100,1000};

Int[] b={20,200,2000,20000};

System.arraycopy (A, 1, B, 2, 2);

for (int x:b) {

SYSTEM.OUT.PRINTLN (x);

}

the output is:20 200 100 1000

The features of the array:the ① array is the reference data type;

② arrays are collections that store the same data types;

The subscript of the ③ array starts with 0 .

④ Advantages: Cross-border inspection, general algorithm;

⑤ disadvantage: Once the length is defined, it cannot be changed.

Java Array Summary

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.