Java Basic Learning Summary--array

Source: Internet
Author: User
Tags array definition

A. Basic concepts of Arrays
    • Arrays can be thought of as multiple combinations of the same type of data, which are managed uniformly.
    • An array variable is a reference type, and an array can be considered an object, and each element in the array is equivalent to the member variable of that object.
    • The elements of an array can be any data type, including the base type and the reference type.
    • Arrays in C and C + + can be allocated on the stack, whereas arrays in Java can be allocated only on the heap, because arrays in Java are reference types.
two. One-dimensional arrays

There are 2 ways to declare one-dimensional arrays:

    • Format one: array element type array name []; That is, type var[];
    • Format two: array element type [] array name; i.e. type[] var;
    • Format two declares an array in the same way that you declare a one-dimensional array on C #.

Example: int a1[]; Int[] A2;

Double b[];

Person[] P1; String s1[];

Note: JAVA cannot specify its length when declaring an array in a language ( number of elements in an array )

such as: int a[5]; It is illegal to declare a one-dimensional array.

three. Model of the array
    • One-dimensional array: one-dimensional array is a row, a row of small cells.
    • Two-dimensional array: a two-dimensional array is a row and a column composed of a plane into a small lattice, there are rows and columns.
    • Three-dimensional array: a three-dimensional array is a cube.
    • Humans are most aware of the three-dimensional space.
Four. Creation of array Objects

Use the keyword new in Java to create an array object.

Format: Array name = New array element type [number of array elements]

For example:

Five. element is an array of reference data types

  Note: Each element in an array that is a reference data type needs to be instantiated.

For example:

Class date{

int year; int moth; int day;

Date (int y; int m, int d) {

Year=y;

Month=m;

Day=d;

}

}

Six. initialization of the array
    • 1. Dynamic initialization

    The array definition is performed separately from the operation of allocating space and assigning values to arrays.

For example:

1 Public classtest{2 Public Static voidMain (String args[]) {3intA[];//defines an array, which declares an array of type int a[]4 a=New int[3];//allocates memory space for array elements. 5 a[0]=3; a[1]=9; a[2]=8;//assigns a value to an array element. 6Date days[];7 days=NewDate[3]; 8 days[0]=NewDate (1, 4, 2004); 9 days[1]=NewDate (2, 4, 2004);Ten days[2]=NewDate (3, 4, 2004);11     } 12 }13 14classdate{15intYear , month, day;Date (intYintMintd) {Year =y;month =m;Day =D;20     }21st }22

    • 2. Static initialization

allocates space and assigns values to an array element while it is being defined.

For example:

Puclicclasstest{ Public Static voidMain (String args[]) {intA[] = {3, 9, 8};//allocates space and assigns values to the array while defining the array. Date days[] = {                NewDate (1, 4, 2004),                NewDate (2, 4, 2004),                NewDate (3, 4, 2004)        }; }}classdate{intYear , month, day; Date (intYintMintd) { Year=y; Month=m; Day=D; }}

Seven. Default initialization of array elements
  • An array is a reference type whose elements are equivalent to the member variables of the class, so after allocating memory space to the array, each element is implicitly initialized by the rules of the member variable.
  • 1 Public classtest{2 Public Static voidMain (String args[]) {3intA[] =New int[5]; 4 date[] Days=NewDate[3]; 5 System.out.println (a[3]); 6 System.out.println (days[2]); 7     } 8 } 9classdate{10intYear , month, day;One Date (intYintMintd) {Year =y;month =m;+ Day =D;15     }16}

    • Output Result:

System.out.println (A[3]); The printed result is: 0.

System.out.println (days[2]); The result of printing is: null (NULL)

Eight. references to array elements

After the definition is allocated with the operator new, each element in the array can be referenced, and the array element is referenced by:Arrayname[index], and the index is an array-element subscript, which can be an integer constant or an integral expression. such as:a[3], b[i], C[6*i].

Array element subscript starts at 0, and the valid subscript value range for an array of length n is 0 ~ n-1.

Each array has an attribute of length indicating its size, for example:A. length the value for the array a the length ( number of elements ) .

Nine. two-dimensional arrays

  

  

Ten Understanding the array model of each dimension in Java

  

Java Basic Learning Summary--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.