JAVA Chapter 4 array, java Chapter 4 Array

Source: Internet
Author: User

JAVA Chapter 4 array, java Chapter 4 Array

ArrayA group of ordered data with the same type is saved.

1. Create:

Array declarationFormat: Int arrary [];

Int [] array1, array2; // declare multiple arrays at the same time.

The preceding statement only declares the array and does not allocate memory to it. It cannot be stored or accessed. Arrays in Java can be seen as special objects, and memory space can be allocated to Arrays Using new.

ArrayInitialization:Int array [] = new int [5]; // method 1. The default value is displayed!

ArrayAnother Creation Method: Int array [] = {1, 2, 3, 4, 5 };

 

2. ArrayCopy:

Int array1 [] = {1, 2, 3 };

Int array2 [] = {4, 5, 6 };

Array1 = array2; // The rough method is to point array1 to the memory space of array2, and the contents in array2 are not copied to array1. Here, only values are assigned.

System. arraycopy (fromArray, fromIndex, toArray, toIndex, length); // The method for enabling array copy.

 

3,Multi-dimensional array:

Create: int [] [] array2D = new int [3] [3];

Int [] [] array2D ={{ 1, 1, 1} {2, 2, 2} {3, 3, 3 }};

 

4,Irregular Array:

Create: int [] [] array2D = new int [3] []; // The array row must be determined when it is declared. The number of rows can be determined again!

Array2D [1] = new int [1];

Array2D [2] = new int [2];

Array2D [3] = new int [3];

 

5. For-Each statement:
The for loop can only take effect For one array. To obtain Each element of a multi-dimensional array, use the for-Each loop statement.

  FormatIs:For (data type variable: Set)The/* for keyword is followed by the Data Type of the Set, followed by an element used for operations, which indicates the currently accessed collection element, and finally the set to be accessed. */

      Statement Block

  For example:Int nums [] [] = {, 3}, {, 6}, {, 9 }};

For (int x []: nums)

For (int y: x ){

System. out. print (y + "");

}

     

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.