2. Java Array

Source: Internet
Author: User

Meaning: A data type that consists of variables of the same type.

One-dimensional arrays

2 steps to use an array:

(1) declares an array (2) to allocate memory to the array.

Declaration Format 1:

Data type array name [];//Declare one-dimensional array such as: int arr1[];

Array name = new data type [number]; Allocate memory to arrays such as: Arr1=new int[5];

Declaration Format 2:

Data type array name [] = new data type [number]; such as: String num[]=new string[5];

Take the length of the array:

Array name. length

Initial value assignment of an array

Declaration format:

Data type array name [] = {Initial value 0, initial value 1, ..., initial value n}; such as: int num[]={7,98,66,64,66};

Int Num[]=new int[4];

num[0]=55;

num[1]=34;

...

num[4]=45;

Array-related API methods

System.arraycopy method

System.arraycopy (source,0,dest,0,x): The meaning of the statement is: Copy the source array from the subscript 0 starting from the X element to the target array, from the target array of subscript 0 corresponding to the location to start access.

Arrays.sort (array name)

Sorting elements in an array

Two-dimensional arrays

Declaration format:

1. Array name of data type []; such as: int num[][];

2. Array name = new data type [number of rows] [number of columns]; for example: Num=new int [2][3];

3. Data type array name [] = new data type [number of rows] [number of columns];

4. Data type array name [] = {{No. 0 row initial value},

{1th row of initial value},

...

{nth row of initial value},

};

Note: The number of elements in each row of a two-dimensional array can be different.

Gets the number of rows in a two-dimensional array and the elements of a particular row

Array name. length//Gets the number of rows in the array

Array name [index of row].length//Gets the number of specific line elements

2. Java Array

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.