5th Chapter Java Array

Source: Internet
Author: User
Tags array length

1. What is an array

The array can be imagined as a huge box that contains data of the same data type.

For example: int[] scores = {78,68,94,93};

2. How to use array 2.1 in Java to declare an array

Before use, declare as follows:
data type [] array name;
Or
Data type array name [];

Example: int[] scores; equivalent int scores;

2.2 Allocating space

The second step is how much space is allocated to the array.
The allocation method is:
Array name = new data type [array length]

Example: scores = new INT[5]

Note: In the first step we use int[] scores to define an array, and in the second step we assign 5 lengths to the scores array.
The above two steps can be combined together:

Int[] scores = new INT[5];

2.3 Assigning values

Using the previous two steps, we created an array, and the third step was to populate the array with data. Here's how:

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

2.4 Another way to create an array

It uses three steps to create an array, but there is another way to create an array that combines the declaration, allocation space, and assignment of an array with the following method:

Int[] scores = {78,89,78,98};

This is completely equivalent to:

Int[] scores = new int[]{78,89,78,98};

Note: The right side of the int[] is filled with nothing.

3. Iterating through an array

Traversing a Java array is generally used for, as in other languages, with two features:
1. The array's small mark is starting from 0
2. The subscript for the last number of arrays is: array Length-1
The traversal is as follows:

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3String[] Hobbys = {"Sports", "Game", "movie" };4System.out.println ("Value of elements in the loop output array:");5          for(inti=0; i){6 System.out.println (Hobbys[i]); 7}
4. Manipulating arrays using the arrays class

In Java, some of the operations of the array are in arrays, here are two examples, other methods can query the Java.util.Arrays class
Arrays class is in the Java.util.Arrays, when used to first import

4.1 Array Sorting

How to use: Arrays.sort (array name);

4.2 Converting an array to a string

How to use: arrays.tostring (array name);
An example is as follows;

1 //Import Arrays Class2 Importjava.util.Arrays;3 4  Public classHelloWorld {5      Public Static voidMain (string[] args) {6 7         //defines a string array8String[] Hobbys = {"Sports", "Game", "movie" };9 Ten         //Use the sort () method of the arrays class to sort the array One Arrays.sort (hobbys); A  -         //Use the ToString () method of the arrays class to convert the array to a string and output - System.out.println (arrays.tostring (Hobbys)); the     } -}

5. Manipulating arrays with foreach

The Foreach method is actually a simplification of the For method, which is a method that does not require the following table to traverse the array, using the following syntax:
for (element type element variable: Traverse object) {
Code to execute
}
As an example:

1 Importjava.util.Arrays;2 3  Public classHelloWorld {4 5      Public Static voidMain (string[] args) {6 7         //define an integer array and save the score information8         int[] scores = {89, 72, 64, 58, 93 };9 Ten         //sorting an array of arrays classes One Arrays.sort (scores); A  -         //iterating through the elements in the output array using foreach -          for(intscore:scores) { the System.out.println (score); -         } -     } -}
Two-dimensional arrays in 6.Java

In a one-dimensional array, each location in the array holds a single data, in a two-dimensional array, where each position in the array is an array, and a two-dimensional array is a table

6.1 Declaring and allocating space

data type [] array name =new data type [number of rows] [number of columns]
Or
data type [] array name;
Array name = new data type [number of rows] [number of columns]
For example:

int[][] num = new INT[2][3]

6.2 Assigning values

Assignment is assigned by coordinates, and coordinates start at 0.
Array name [index of row] [index]= value of column];

Example: num[0][0]=12;

6.3 Processing data

Two-dimensional array processing data is through two lower coordinates, usually traversing a two-dimensional array when it is necessary to nest in a for loop for a for loop

5th Chapter Java Array

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.