Java Basics-Arrays

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 class test{2 public     static void Main (String args[]) {3     int a[];  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. 6     date days[]; 7     days=new date[3]; 8     days[0]=new Date (1, 4, 2004); 9     days[1]=new Date (2, 4, 2004); 10< C16/>days[2]=new Date (3, 4, 2004), one     }}13 class date{15 int year     , month, day;16     date (int y, int m, I NT D) {year         = y;         month = m; N Day         = D;     }21}22                 
    • 2. Static initialization

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

For example:

Puclic class test{public        static void Main (String args[]) {            int a[] = {3, 9, 8};   Allocates space and assigns values to the array while defining the array.            Date days[] = {                new date (1, 4, 2004),                new Date (2, 4, 2004),                new Date (3, 4, 2004)        };    } Class date{    int year, month, day;    Date (int y, int m, int d) {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 class test{2 public         static void Main (String args[]) {3             int a[] = new INT[5]; 4             date[] Days=new Da TE[3]; 5             System.out.println (a[3]); 6             System.out.println (days[2]); 7     } 8} 9 class date{10     int year, month, day; One     Date (int y, int m, int d) {year         = y;         month = m; + Day         = D;     }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 Basics-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.