An array of basic Java summaries

Source: Internet
Author: User
Tags array definition

Software 151, Shu Jun

A) basic concepts of arrays

1, the array can be seen as multiple combinations of the same type of data, the unified management of these data.

2, array variables are reference types, arrays can also be considered as objects, each element of the array is equivalent to the object's member variables.

3. The elements of an array can be any data type, including the base type and the reference type.

Arrays in 4, 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.

(ii) 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 #.

For example:

int a1[]; Int[] A2;

Double b[];

Person[] P1; String s1[];

Note: When declaring an array in the Java language, you cannot specify its length (the number of elements in the array), such as: int a[5]; So declaring a one-dimensional array is illegal.

(c) The 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 recognize the most three-dimensional space.

(iv) objects created by arrays

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

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

(v) The 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 month;
int day;

Date (int y, int m, int d) {
Year = y;
month = m;
Day = D;
}

}

(vi) Initialization of arrays

1. Dynamic initialization

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

public class Test {
public static void Main (String args[]) {
int a[]; Defines an array, which declares an array of type int a[]
A = new int[3]; Allocates memory space for array elements.
A[0] = 3;
A[1] = 9;
A[2] = 8; Assigns a value to an array element.
Date days[];
days = new Date[3];
Days[0] = new Date (1, 4, 2004);
DAYS[1] = new Date (2, 4, 2004);
DAYS[2] = new Date (3, 4, 2004);
}
}

Class Date {
int year, month, day;

Date (int y, int m, int d) {
Year = y;
month = m;
Day = D;
}
}
2. Static initialization

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

public 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;
}
}
(vii) 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.

public class Test {
public static void Main (String args[]) {
int a[] = new INT[5];
Date[] days = new DATE[3];
System.out.println (A[3]);
System.out.println (days[2]);
}
}

Class Date {
int year, month, day;

Date (int y, int m, int d) {
Year = y;
month = m;
Day = D;
}
}
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],index An array element subscript, which can be an integer constant or an integral type 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 a property length that indicates its size, for example: The A.length value is the length of the array a (number of elements).

An array of basic Java summaries

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.