one , brief introduction

describe :

An array is an ordered collection of data of the same type .

among , Each data is called an array element , Each array element can be accessed by a subscript .

Basic features :

The length of the array is determined . Once the array is created , Its size cannot be changed .

Array elements must be of the same type , Mixed type not allowed .

The elements in the array can be any data type , Includes basic and reference types .

Array variable is of reference type , Arrays can also be treated as objects , Each element in the array is equivalent to the member variable of the object . The array itself is an object ,Java Objects in are in the heap , Therefore, the array can save basic data types or other object types , The array object itself is in the heap .

two , Declaration array

grammar :

/* First , Brackets after data type */

elementType[] arrayVarName

/* Second , Brackets after variable name */

elementType arrayVarName[]

describe :

Variables of array type must be declared first , To freely use array objects in programs .

Java There are two ways to declare array variables in , A bracket in ' data type ' after , Another bracket is in ' Variable name ' after .

be careful :

When declaring array variables , initial ' data type '( Without brackets ) Represents the data type of the elements in the array .

The second bracket for declaring array variables is in ' Variable name ' Post mode , come from C/C++ Language style , For convenience C/C++ Programmers are learning Java Can get started faster , Not recommended .

When declaring an array, the array is actually created , No objects were instantiated .

Cannot specify the length of an array when declaring it ( Number of elements in the array ), Only when creating an array object ,JVM To allocate space , At this time, it is related to the length .

Example :

/* Recommended */

int[] intArr1;

/* Not recommended */

int intArr2[];

three , Create array

grammar :

dynamic initialization :

new elementType[arrayLength]

initiate static :

{firstElementValue, secondElementValue, thirdElementValue...}

describe :

There are two ways to create an array : Dynamic initialization and static initialization .

When creating an array , Length of array must be specified , Specify in brackets during dynamic initialization , During static initialization, the array length is the number of elements in braces .

When creating an array object using dynamic initialization , The length of the array will be specified first , And initialize the element by default .

When creating an array object using static initialization , Not only the array length is specified, but also the element is initialized by default , It also assigns values to all elements .

be careful :

Array is a reference type , Its elements are equivalent to instance variables of the class , So once the array is allocated space , Each of these elements is also

Example variables are implicitly initialized in the same way .

Example :

/* dynamic initialization */

new int[5];

/* initiate static */

{1, 2, 3, 4, 5};

four , Use array

Ⅰ, Declare and create

grammar :

/* Declare array variables and initialize dynamically */

elementType[] arrayVarName = new elementType[arrayLength];

/* Declare array variables and initialize statically */

elementType[] arrayVarName = {firstElementValue, secondElementValue,
thirdElementValue...};

describe :

The declaration of array variables and the creation of arrays can be completed in one statement , Usually, we also operate on an array object multiple times .

Example :

/* Declare array variables and initialize dynamically */

int[] intArr1 = new int[5];

/* Declare array variables and initialize statically */

int[] intArr2 = {1, 2, 3, 4, 5};

Ⅱ, Array length

grammar :

arrayObject.length

describe :

The array length can be obtained directly from the array object , The syntax is as shown above .

The array length is specified when the array object is created , After the array object is created , The length of the array object does not change .

Example :

int[] intArr = {1, 2, 3, 4, 5};

System.out.println(intArr.length);//5

Ⅲ, Array subscript

grammar :

arrayObject[elementIndex]

describe :

Array elements are indexed by ( Indexes ) Accessed .

Array subscript from '0' start , The total number of subscripts is the array length , So the subscript values are from zero to the array length minus one :0 ~ arrayObject.length-1

be careful :

Subscript of array object when used , If the subscript value is out of range , For example, less than zero , Or exceed the array length minus one , An error will be reported :ArrayIndexOutOfBoundsException( Array subscript out of bounds exception ).

Example :

int[] intArr = {1, 2, 3, 4, 5};

System.out.println(intArr[1]);//2

Ⅳ, Loop structure traversal array

grammar :

'for' loop

for (int i = 0; i < arrayObject.length; i++) {

/* use 'arrayObject[i]' Traversal operation array elements */

}

'foreach' loop

for (elementType elementVarName : arrayObject) {

/* use 'elementVarName' Traversal operation array elements */

}

describe :

The element type of the array and the size of the array are determined , So when processing array elements , We usually use 'for' Cycle or 'foreach' loop ( Enhanced 'for' loop ).

'foreach' A loop can traverse an array without Subscripts .

Example :

/* The running results are output in turn on the console :1 2 3 4 5 1 2 3 4 5 */

public static void main(String[] args) {

int[] intArr = {1, 2, 3, 4, 5};

/* for Loop through array */

for (int i = 0; i < intArr.length; i++) {

System.out.println(intArr[i]);

}

/* foreach Loop through array */

for (int element : intArr) {

System.out.println(element);

}

}

five , Two dimensional array

grammar :

statement

innerElementType[][] arrayVarName

establish

/* Dynamically initialize 2D array */

new elementType[outerArrayLength][innerArrayLength]

/* Static initialization of 2D array */

{{innerFirstElementValue, innerSecondElementValue,
innerThirdElementValue...},{innerFirstElementValue, innerSecondElementValue,
innerThirdElementValue...},{innerFirstElementValue, innerSecondElementValue,
innerThirdElementValue...}...}

Array length

/* Get outer array length */

arrayObject.length

/* Gets the inner array length of the specified outer array subscript */

arrayObject[outerIndex].length

Array subscript

/* Gets the element of the specified outer array subscript ( An inner array ) */

arrayObject[outerIndex]

/* Gets the element of the specified inner array subscript of the inner array of the specified outer array subscript */

arrayObject[outerIndex][innerIndex]

describe :

The syntax of multi-dimensional arrays above two dimensions is analogized according to the syntax of two-dimensional arrays .

Multidimensional arrays can be regarded as arrays of arrays , For example, a two-dimensional array is a special one-dimensional array , Each element is a one-dimensional array .

Example :

/* The running results are output in turn on the console :1 2 2 3 3 4 4 5 5 6 */

public static void main(String[] args) {

int[][] arrayObject = {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}};

/* nesting foreach Loop through a two-dimensional array */

for (int[] innerArr : arrayObject) {

for (int innerElement : innerArr) {

System.out.println(innerElement);

}

}

}

Technology
©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?