Basic Java Learning--array first knowledge (1)

Source: Internet
Author: User

JavaBasic Learning--array first knowledge (1)1what is an array

A common data structure in Java is an array, which can be divided into one-dimensional arrays, two-dimensional arrays, and multidimensional arrays.

An array is a data type that consists of a set of identical variables, each of which has the same data type, and each element in the array can be determined with a uniform array name and subscript.

2Use of Arrays

General use steps for arrays:

    1. Declaring an array

    2. allocating memory to the array

The following is an example of a one-dimensional array:

    1. Data Type Array name [];

    2. array Name = new data type [ data Count ];

2.1Declaration and assignment of one-dimensional arrays

1, the declaration of the Array

int num []; declares an array

num = new INT[5]; allocates memory space to an array of 5 elements

The declaration of an array can also take the following form:

int num []= new int [5]; allocating memory space at the same time as declared

2. Assigning values to arrays

the assigned initial value of an array is the following form:

int num []={1,2,3,4,5}; // This represents the declaration and assignment of an array, although it appears that there is no

Specifies the number of data in the array, but because the curly braces

given the specific data, so the system allocates space by default

3. The representation of the elements in thearray and the length of the array

the representation of an element in an array:

Array name [Subscript of elements in array ]//The subscript of an element in an array is starting from 0

For example, the representation of the second element in the array is

num [1]// Gets the second element of the array

How to represent the length of an array:

The array name . ength

that is, the length of the above array is num. Length

2.2two-dimensional arrays

1. Declaration of two-dimensional arrays

int num []; declaring a two-dimensional array

num = new INT[2][3]; allocates memory space to an array with elements of 2 rows and 3 columns

The declaration of an array can also take the following form:

int num [][]= new int [2][3]; allocating memory space at the same time as declared

2. Assigning values to arrays

the assigned initial value of an array is the following form:

int num []={{1,2,3},{4,5,6}}; // This represents the declaration and assignment of an array, although it appears that there is no

Specifies the number of data in the array, but because the curly braces

given the specific data, so the system allocates space by default

3.the representation of the elements in the array and the length of the array rows

the representation of an element in an array:

Array name [ row subscript] [ column subscript]//The index of the element in the array is starting from 0

For example, the second row in the array is represented by the next column element.

num [1][1]// Gets the second row of the array of elements

How to represent the length of an array:

array name . ength// Gets the number of rows in the array

Array name [ row below table ].ength// Get the number of elements of the corresponding row

 


This article is from the "Broken Castle of the Little Prince" blog, please be sure to keep this source http://wh201503.blog.51cto.com/10047381/1890781

Basic Java Learning--array first knowledge (1)

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.