Java Learning Notes (v) arrays

Source: Internet
Author: User

Concept: An array is a collection of elements stored in a contiguous block of memory. Each element in the array must be of the same data type and be differentiated by the index. The index of the first element in the array is 0 (the index is the number also called subscript).

Why use arrays: A single value type variable is sometimes difficult to meet the design requirements of an application, and an array can store multiple underlying data type variables or objects of the same type.

Two steps to create an array: Declare a reference to an array, initialize the array with the new keyword, and specify the size of the array. Example: int [] sum; or int sum []; This is the declaration of an array; Specifies the size: sum = new int [10];  The declaration and the specified size can be together: int [] sum = new int [10]; Assigning an array (initialization) can be: sum[0] = 1;sum[1] = 2; It is also possible to combine three steps: int [] sum = {A-c}; This is an integer array of length three.

Accessing an array: The length property of the array can be used to reduce the occurrence of arrayindexoutofboundsexception (subscript out-of-bounds) exceptions, and to cycle through the elements in the group sum:

for (int i = 0; i < sum.length; i++) {

System.out.println (Sum[i]);

}

Reference Array, Example: An Employee class has name,address,number three attributes are string, a variable myCompany to reference the array: MyCompany = new employee[100]; Assign a value to this reference array, MyCompany [9] = new Employee ("Zhang San", "Chengdu City", "123");
An array is an object whose type inherits from Java.lang.object, so you can invoke any method of object. New has created only one object in memory at a time.

The copy of the array can be used with the Arraycopy () method in the System class, for example:

public static void Arraycopy (object source, int sourcepos, object destination, int destinationpos, int length) SOURCEP The OS is copying the source array, destinationpos the target array, length lengths;

int [] temp = odds; Declaring an array

odds = new int [20]; Specify array size

System.arraycopy (temp, 0, odds, 4, temp.length); For replication

for (int i = 0; i < odds.length; i++) {

System.out.println (Odds[i]); The value in the Loop print group odds

}

Two-dimensional array: int [] [] sum; Three-dimensional array: int [] [] [] sums, and the initialization of a one-dimensional array, the first of the two-dimensional array refers to the number of rows, the second refers to the number of columns.

Javadoc Documentation: You can get comments in Java source code and generate HTML pages, so we should write more comments when writing code.

Expressing specific types of annotation information;

@author: Author name

@depracated: Indicates that the current class member has expired as the version of the program has been upgraded, and that the developer should not use this API just to ensure compatibility persists

{@docRoot} represents the relative path to the root directory to which the current page is to be output

@exception: Used by the method to list the thrown exception

{@link The link text for the package. Class # member}: Plain text font instead of code font

@param: Describe the formal parameters of a method

@return: The return value of the method

@see: Create a link to "see" XX entries, such as classes, methods, variables, etc.

@since: A version number that describes the presence or change of a member

@serial, @serialField, @serialData: Serialization uses

@throws: Describes the exception thrown

{@value}: value for a significantly brighter static field

@version: Describe the software version of the class

Java Learning Notes (v) arrays

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.