Java Learning Notes-eighth chapter array

Source: Internet
Author: User

Eighth Chapter Array

  1. definition: in Java, an array is a collection of ordered data with the same data type, which is an object. Each data in the array is called an array element and is accessed by subscript. Arrays are divided into one-dimensional arrays and multidimensional arrays.

  2. Declaration of one-dimensional arrays: declaring an array specifies only the array name and the data type of the element, does not specify the number and initial values of the array elements, and the system does not allocate memory space for the arrays. The declaration format of a one-dimensional array is as follows:

    data type [] array name; Or a data type array name [];

  3. initialization of a one-dimensional array: an array declaration needs to be initialized before it can be used, by initializing it to allocate memory space for the array, or assigning a value to the array element. Initialization of arrays is divided into static initialization and dynamic initialization.

    (1) Static initialization: assigns a value to an array element at the same time that it is declared. You do not need to specify the size of the array, the system automatically calculates the group length based on the number of elements and allocates memory space. Applies to cases where the number of elements is not many or can be enumerated. The syntax format is as follows:

    data type [] Array name = {Element 1 initial value, element 2 initial value, element 3 initial value ...};

    (2) Dynamic initialization: The use of the new operator to allocate memory space for arrays. At this point the array is declared and initialized separately. Commonly used in cases where the array elements are more or cannot be enumerated. The syntax format is as follows:

    data type [] array name;

    array name = new data type [array length]; Can also be written as: data type [] array name =new data type [array length];

  4. use of one-dimensional arrays: array name [array subscript];

    (1) The array subscript is used to uniquely identify each element in the array, which can be an integer or an expression. All array subscripts in Java start at 0 and end with "Array length-1".

    (2) The array object has the length property to represent the array lengths.

  5. two-dimensional array declaration: data type [[]] array name; or data type array name [];

  6. initialization of two-dimensional arrays: static initialization and dynamic initialization

    (1) Static initialization: assigns an initial value to an element while declaring an array. For example:

     int a[][] = {{12},{54,63},{123,456}};

    A

    Two-dimensional array can also be considered an array of one-dimensional arrays, as in the above example, array A is a two-dimensional array of 3 different lengths of one-dimensional arrays.

    (2) Dynamic initialization: allocates memory space for an array with the New keyword, specifies the number of rows and columns of the elements of the arrays, but does not assign an initial value to the array. There are two ways to initialize a two-dimensional array dynamically:

    A. Direct: Specifies the number of rows and columns at initialization, and applies to the same number of columns for each row of the array. For example:

    int a[][] = new int[3][2];

    B. Hierarchical: assigns the number of rows to a two-dimensional array first, and then assigns the number of columns to each row. For example:

    a[0] = new INT[1];          //define first line a[0] is 1 elements

    a[1] = new Int[2] ;          //define second line a[1] is 2 elements

  7. use of two -dimensional arrays: two-dimensional arrays are used in a way similar to one-dimensional arrays, where each element of an array is identified by a subscript. The subscript for each dimension is also starting from 0. The syntax format is: array name [array subscript 1][array subscript 2];

  8. Object array: an array of array elements whose type is a class object is called an array of objects. The definition of an object array is consistent with the definition of a primitive type array.

    (1) When an array of objects is defined, only the array object reference is created, the object element is not initialized, and the object array is allocated memory space, so after the object array is defined, it is also initialized.

    (2) Each element in an array of objects is an object, so you can use the operator "." Accesses a member in an object.

  9. array as a parameter of the method: in Java, an array can be passed as a parameter to a method. The array is passed as a parameter of the method, so the argument and the parameter have the same memory space. If you change the value of a parameter in a method, the value of the argument is changed.

    (1) When an array is a formal parameter, the parentheses after the array name cannot be omitted, the number of brackets and the number of dimensions of the arrays are equal, and the number of elements of the group is not required.

    (2) When an array is an argument, the parentheses after the array name are omitted.

    (3) An array element can also be passed as a parameter of a method, at which point a value is passed, the value of the parameter is changed in the parameter, and the value of the argument is not changed.

Java Learning Notes-eighth chapter 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.