Detailed explanation of Java array Foundation

array

Array: a collection of data of the same type.

There are two methods for initializing Java arrays:

Static initialization: the programmer assigns a value to each element of the array when initializing the array; Dynamic initialization: during array initialization, the programmer only specifies the length of the array, and the system assigns an initial value to each element.

Whether the array must be initialized

For this problem, the key is to find out the difference between array variables and array objects. Array variables are stored in stack memory, and array objects are stored in heap memory. Array variable is just a reference variable, which can point to the actual array object.

The so-called array initialization is not to initialize array variables, but to initialize array objects.

Define array

Method 1 (recommended, better indicating the array type) type [] variable name = new type [number of elements in the array]; for example:

The array name, or reference a, points to the first address of the array element.

Mode 2 (same as C language)

Type variable name [] = new type [number of elements in array];

For example:

Mode 3: direct initialization during definition

Type [] variable name = new type [] {comma separated initialization value};

The red part can be omitted, so there are two kinds:

Where int [] a = New Int [] {1,4}; The array length cannot be added to the second square bracket of, because the number of elements is determined by the contents of the curly braces.

Fundamentals of array application

Array length

Each array in Java has an attribute named length, which represents the length of the array.

The length attribute is public final int, that is, the length is read-only. Once the array length is determined, the size cannot be changed.

equals()

Can I use the equals () method to compare the contents of an array?

The following procedure:

The output is false.

Therefore, it is proved that the equals () method cannot be directly used to compare the contents of the array. Because there is no implementation in override object, its implementation is still used, that is, the equals () method is implemented with = = to compare whether it is the same object.

How to compare? One solution is to write your own code, and the other is to use Java util. Arrays。

  java. util. The methods in arrays are all static. It includes various overloaded versions of the equals () method.

The code is as follows:

When the array element is not a basic data type

When the array element is not a basic native data type, the reference type is stored, not the object itself. After the object is generated, the reference points to the object, otherwise the reference is null.

As follows:

Output:

  null   10   20   30

You can also write directly in the initialization list:

Two dimensional array

A two-dimensional array is an array of arrays.

2D array Foundation

There are two basic definition methods, such as:

  type[][] i = new type[2][3]; (recommended) type I [] [] = new type [2] [3];

The following procedure:

The output result is two true.

Variable length two-dimensional array

Each element of a two-dimensional array is a one-dimensional array. These arrays are not necessarily of equal length.

When declaring a two-dimensional array, you can only specify the first dimension size, leave the second dimension size blank, and then specify arrays of different lengths. Note, however, that the first dimension size cannot be empty (you cannot specify only the number of columns and not the number of rows).

The following procedure:

Two dimensional arrays can also be initialized at the time of definition, using the nesting of curly braces. At this time, the size of two dimensions is not specified, and array elements of different lengths can be generated according to the number of initialization values.

The following procedure:

Output:

  1 2 3   4   5 6 7 8

summary

The above is all about Java arrays in this article. I hope it will be helpful to you. Welcome to: Java array cross-border problem example analysis, Java map storing array and fetching value code explanation, and other related contents of this site.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>