Array of Java (4)

Source: Internet
Author: User

What is an array:

Q: Write code to save 4 students ' test scores.

A: Simple ah, define 4 variables Bai

Q: That "calculates the exam results of 400 students all year round", swollen

For:.......

Array, you can help you to solve the problem properly!!

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 store 4 students ' scores

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], scores[2] can take the third element 92!

For example:

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3 4         //define an array to save five students ' scores5         int[] scores = {78, 93, 97, 84, 63 };6 7         //the second score in the output array8System.out.println ("The 2nd result in the array is:" + scores[1] );9     }Ten}

How to use arrays in Java:

Working with arrays in Java requires only four steps:

1. Declaring arrays

Syntax: data type [] array name;

or a data type array name [];

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

2. Allocate space

In short, it is the maximum number of elements that can be stored in the specified array

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:

In other words, we can also combine the above two steps to allocate space for it while declaring an array, such as:

3. Assign Value

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 arrays

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:

Use the Arrays class to manipulate arrays in Java:

The Arrays class is a tool class provided in Java, in the Java.util package. The class contains methods for manipulating arrays directly, such as sorting, searching, and so on.

Common methods in Arrays:

1. Sorting

Syntax: Arrays.sort (array name);

You can use the sort () method to sort the array, as long as the array name is placed in the parentheses of the sort () method, which you can sort (in ascending order), such as:

1 Importjava.util.Arrays;2  Public classhelloworld{3      Public Static voidMain (string[] args) {4         //defines an array of shaping5         int[] scores = {78, 93, 97, 84, 63};6         //Use the sort () method of the arrays class to sort the array7 Arrays.sort (scores);8System.out.println ("Value of the elements in the sorted array:");9         //iterate through the elements in the output arrayTen          for(inti = 0; i < scores.length; i + +){ One System.out.println (Scores[i]); A         } -     } -}

Operation Result:

2. Converting an array to a string

Syntax: arrays.tostring (array name);

You can use the ToString () method to convert an array to a string that joins multiple array elements sequentially, separated by commas and spaces between multiple elements, such as:

The result of the operation is:

Elements in the output array nums: [25,7,126,53,14,86]

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. In terms of the literal meaning of foreach, which means "for each", how do you use a foreach statement?

Grammar:

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

Operation Result:

It's easy to see the foreach!!

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 arrays and allocating space

Or

Such as:

2. Assign Value

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, even if you are using 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 different, an irregular two-dimensional array is created, as follows:

The result of the operation is:

Array of Java (4)

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.