Java Learning: Arrays

Source: Internet
Author: User

1. Defining arrays

Defining a one-dimensional array

int array1[];
int [] ARRAY2,ARRAY3;

The symbol "[]" description declares an array object. There is no difference between the two declarations, but the second can declare multiple arrays at the same time

// Create an array of size 10 integers int New int [ten]; // Create an array of size 5 integers and initialize int [] Array = {12345};

2. Length instance variable

int[] arr = new INT[10];

Arr.length = array length

3, one-dimensional array replication

Copy the length from the SRC array srcpos position to the beginning of the dest array Destpos

Parameter 1: Original array

Parameter 2: Copying the starting position of the original array

Parameter 3: Target array

Parameter 4: Starting position of destination array storage

Parameter 5: Length of the copy

System.arraycopy (src, srcpos, dest, destpos, length);

int New int [5]; int New int [5]; // The statement will also point arr1 to the ARR2 array, change the value of the ARR2 array, and the value of the ARR1 array will change // arr1 = arr2; // The statement copies the values in the ARR2 array into the arr1 array, changing the values in the ARR2 without affecting the ARR1 array // system.arraycopy (arr2, 0, arr1, 0, arr2.length);

4, two-dimensional array

Int[][] [New int[5][6];

Two.length represents the number of rows in the array, and Two[i].length represents the number of columns in the array.

5. Irregular arrays

Int[][] [New int[4][]; Array has 4 rows

Two[0] = new INT[1]; There are 1 columns in line 1th.

Two[0] = new INT[2]; There are 2 columns in line 1th.

Two[0] = new INT[3]; There are 3 columns in line 1th.

Two[0] = new INT[3]; There are 4 columns in line 1th.

6. For-each loop: Used to iterate through an array

//For-each Accessing a one-dimensional array Public classArray { Public Static voidMain (string[] args) {intsum =0; int[] arr = {1,2,3,4,5};  for(intI:arr) {Sum+=i; } System. out. println (sum); }}

Results: 15

//For-each Accessing a two-dimensional array Public classArray { Public Static voidMain (string[] args) {intsum =0; int[] arr = {{1,2},{3,4}};  for(inti[]: arr) {             for(intj:i) {sum+=J; }} System. out. println (sum); }}

Results: 10

Java Learning: 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.