Javase Getting Started learning 9:java basic syntax arrays

Source: Internet
Author: User

A definition of an array

An array can be understood as a huge "box" in which multiple types of data can be stored sequentially, such as arrays that can define an int type.

Scores stores scores of 4 students.


The elements in the array can be accessed by subscript, and the subscript starts at 0. For example, you can get the first element in the array by scores[0] 76,

SCORES[2] can be taken to the third element of 92.

Two operation of arrays requires only four steps:

1 declaring an array

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

Where the array name can be any valid variable name, such as:


2 Allocating space

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

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

Where the array length is the number of elements that can be stored in an array, such as:


We can also combine the above two steps to allocate space for it while declaring the array, such as:


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:


4 working with data in an array

We can manipulate and manipulate the array after the assignment, such as getting and outputting the values of the elements 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:


It is equivalent to:


three use loops to manipulate arrays in Java

In practical development we often use loops to control the operation of array members. Such as:


Operation Result:


where the array name. length is used to get the lengths of the array

Some things to keep in mind are:

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 array length-1, if the cross-border access, will be an error. Such as:


The above error message means that the array subscript exceeds the range, that is, the array access is out of bounds. Create an array of length 2 in the code above, so the number

Group Subscript Range is 0 to 1, and the program subscript appears 2, that is, scores[2], over the range, resulting in array access out of bounds.

four manipulating arrays using the arrays class

The Arrays class is a tool class provided in Java, in the Java.util package. This class contains methods for manipulating arrays directly, such as direct real

The array is sorted, searched, and so on.

Common methods in arrays:

1 sort

Syntax: Arrays.sort (array name);

You can use the sort () method to sort the array by placing the array name in the parentheses of the sort () method, which can be done by

in ascending order), such as:

Operation Result:


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 that joins multiple array elements sequentially, using the

Commas and spaces are separated, such as:


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

The Arrays class also provides many other ways to manipulate arrays here without listing them.

five using the foreach operation array

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. from English

The literal meaning of a foreach is the meaning of "for each", so how do you make a foreach statement?

Grammar:


We use the for and foreach statements to iterate through the array, respectively:


Operation Result:


six two-dimensional arrays in Java

The so-called two-dimensional array, which can be simply understood as a "special" one-dimensional array, each of its array space holds a one-dimensional array.

So how to use a two-dimensional array, the steps are as follows:

1 declaring an array and allocating space


Or


Such as:


2 Assigning values

The assignment of a two-dimensional array, similar to a one-dimensional array, can be assigned individually by subscript, noting that the index starts at 0:


You can also assign a value to an array while declaring it


Such as:


3 Working with arrays

A two-dimensional array accesses and outputs the same array of dimensions, just one more subscript. In the loop output, you need to embed a loop inside, i.e.

Use a double loop to output each element in a two-dimensional array. Such as:


Operation Result:


What you need to know: When you define a two-dimensional array, you can specify only the number of rows, and then specify the number of columns for each row, respectively. If the number of columns per row is not

, an irregular two-dimensional array is created, as follows:


The result of the operation is:




Javase Getting Started 9:java an array of basic syntax

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.