An array of Java to understand

Source: Internet
Author: User

First, what is an array

An array can be understood as a huge "box" in which multiple types of data can be stored sequentially, such as an array of type int scores stores the scores of 4 students:

Int[] scores={76,83,92,87};

Where the name of the array is scores, each value has an index, starting with 0, you can get the first element in the array by scores[0] 76.

Second, the use of arrays

Working with arrays in Java requires only four steps:

1. Declaring an array

Syntax: array type [] array name, or array type array name [];

such as: int[] scores;//storage of student scores of the array, the type is integral type;

String name[];//stores an array of student names, type string;

2. Allocating space

In a nutshell, it is the number of elements in the specified array that can be stored.

Syntax: array name =new array type [array length];

such as: scores=new int[5];//length of 5 integer array;

Or merge the steps: int scores[]=new int[5]; It is important to note that the value length 5 here must be specified.

3. Assigning values

After allocating space, the data can be placed into the array, and the elements in the array are accessed by subscripts, such as storing student scores in the scores array:

scores[0]=89; scores[1]=79;

4. Working with the data in the array

Another way to create an array directly in Java is to combine declaring an array, allocating space, and assigning values, such as

int [] scores={78,97,88,78}; equivalent to: int[] scores=new int[]{78,97,88,78}; It must be noted that the numeric length must be empty here .

Note: 1, array subscript starting from 0. So Scores[3], which represents the 4th element in the array, not the 3rd element

2, array subscript range is 0 to the array length-1, if the cross-border access, will be error

3, array name. Length: Used to get the lengths of an array

Third, using the Arrays class to manipulate arrays in Java

Common methods in Arrays:

1. Sort in ascending order

Syntax: Arrays.sort (array name);

2. Converting an array to a string

Syntax: arrays.tostring (array name);

You can use the ToString () method to convert an array into a string, which connects multiple array elements sequentially, separated by commas and spaces between multiple elements.

Example:

The result is: the element in the output array nums: [25,7,126,53,14,86]

Iv. manipulating arrays with foreach

foreach is not a keyword in Java, it is a special simplified version of A for statement, which is simpler and easier to iterate through arrays and collections. To understand the meaning of foreach as "for each" from the literal meaning of English.

Syntax: for (element type element variable: Traversal object) {

Code of Execution;}

V. Two-dimensional arrays in Java

1. Declaring arrays and allocating space

Syntax: array type [] array name =new array type [number of rows] [number of columns];

Example: int[][] num=new int[2][3];//defines a two-dimensional array of three columns

2, the assignment of a two-dimensional array is similar to a one-dimensional array, you can assign the value by subscript, note that the index starts from 0.

Syntax: array name [index of row] [index]= value of column];

or assign an array type [] Array name ={{value 1, value 2...},{value 11, value 22}, while declaring the array;

3, the processing array two-dimensional array access and output the same dimension array, just one more subscript. In the loop output, you need to embed a loop inside, even if you are using a double loop to output each element in a two-dimensional array. Such as:

Run Result: 123

456

An array of Java to understand

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.