Chapter 6 Array

Source: Internet
Author: User

Chapter 6 Array

6-1 What is an array?

  

 

All elements in the array can be accessed by subscript. The subscript starts from 0. For example, you can use scores [0] to obtain element 76 in the array.

6-2 How to Use arrays in Java

In Java, operations on arrays only take four steps:

① Declare an array

Syntax: Data Type [] array name;

Or the data type array name [];

The array name can be any valid variable name, for example:

  

 

② Allocate space

Simply put, it is used to determine the maximum number of elements that can be stored in an array.

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

The length of the array is the number of elements that can be stored in the array, for example:

  

 

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

  

 

③ Assign values

After the space is allocated, you can put data into the array. All elements in the array are accessed by subscript, for example:

  

 

④ Process data in the array

We can operate and process the assigned array, such as getting and outputting the values of elements in the array.

  

 

In Java, another method is provided for directly creating arrays. It combines declared arrays, allocated spaces, and assigned values, for example:

  

 

It is equivalent:

  

 

Exercise questions:

  

 

Analysis

Option A needs to specify the length of the array;

Option C cannot specify the length of the array when declaring an array while assigning values;

Option D declares an array syntax error. It should be int [] score = {78, 23, 44, 78 };

Therefore, select B

6-3 Use loops to operate arrays in Java

  

 

Running result:

  

 

The array name. length is used to obtain the length of the array.

Notes ":

1. the array subscript starts from 0. Therefore, scores [3] indicates 4th elements in the array, instead of 3rd elements.

2. The range of objects under the array is 0 to the length of the array-1. If the object is accessed across the border, an error is returned. For example:

  

 

The following error is reported during running:

  

 

The error message above indicates that the array subscript exceeds the range, that is, the array access is out of bounds. In the above Code, create an array with a length of 2, so the range of the array subscript is 0 to 1, and the subscript in the program appears 2, that is, scores [2], beyond the range, the array access is out of bounds.

6-4 Use the Arrays class to operate Arrays in Java

The Arrays class is a tool class provided by Java in the java. util package. This class contains some methods for directly operating arrays, such as sorting and searching arrays.

Common Methods in Arrays:

1. Sort

Syntax: Arrays. sort (array name );

You can use the sort () method to sort the array. You only need to put the array name in the brackets of the sort () method to sort the Array (in ascending order ), for example:

  

 

2. convert an array to a string

Syntax: Arrays. toString (array name );

You can use the toString () method to convert an array into a string. This method concatenates multiple array elements in order. Multiple elements are separated by commas (,) and spaces, for example:

  

 

Running result:

Elements in the output array nums: [,]

The Arrays class also provides many other methods to operate Arrays.

6-5 Use foreach to operate arrays in Java

Foreach is not a keyword in Java. It is a special simplified version of the for statement. It is easier and more convenient to traverse arrays and collections. To understand the meaning of foreach, that is, "for each", how can we use foreach statements?

Syntax:

  

 

We use the for and foreach statements to traverse the array respectively.

  

 

6-5 Two-dimensional array in Java

To use a two-dimensional array, follow these steps:

1. Declare the array and allocate space

  

 

Or

  

 

For example:

  

 

2. Assignment

The assignment of two-dimensional arrays is similar to that of one-dimensional arrays. You can assign values one by one using subscript. Note that the index starts from 0.

  

 

You can also assign values to an array while declaring it.

  

 

For example:

  

 

3. Process Arrays

The access to a two-dimensional array is the same as the output of an array of the same dimension, but there is an additional subscript. In loop output, a loop needs to be embedded in it, that is, the dual loop is used to output every element in the two-dimensional array. For example:

  

 

Running result:

  

 

Note: when defining a two-dimensional array, you can specify only the number of rows, and then specify the number of columns for each row. If the number of columns in each row is different, an irregular two-dimensional array is created, as shown below:

  

 

The running result is:

  

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.