Hello, today for you to share the Java array, quickly take out a small book to write it down!

A one-dimensional array

(1) Declare array variables

To use an array in a program, you must declare a variable that refers to the array and specify its meta type.

Syntax for declaring array variables:

elementType[] arrayRefVar; //(element type [] array reference variable;)

elementType arrayRefVar[]; //(element type array reference variable [];)

Most Java application programmers prefer the first style because it separates array types from variable names

  • elementType

It can be any data type, and all elements in the array must have the same data type

  • arrayRefVar

Is an array variable name that follows the rules for user-defined identifiers

(2) Array creation and initialization

Unlike the declaration of basic datatype variables, declaring an array variable does not allocate any space in memory to the array. It simply creates a storage location for a reference to the array.

When allocating space to an array, you must specify the number of elements that the array can store to determine its size. You can’t resize an array once it’s created.

  • Allocates memory space for array variables

elementType[] arrayRefVar = new elementType[arraySize];

Element type [] Array reference variable =new element type [array size];

elementType arrayRefVar[] = new elementType[arraySize];

Element type array reference variable =new element type [array size];

  • Array initialization

  • Default values for array elements:

When an array is created, its elements are given default values. The default values for numeric primitives are 0, char is ‘\u0000’, and Boolean is false.

Creates an array object and assigns its initial value

  • Combine declaring an array, creating an array, and initializing an array into one statement

elementType[] arrayRefVar = {value0, value1, valuek}

The array initialization syntax does not use the operator new. When using array initialization syntax, you must declare, create, and initialize an array in a single statement. Separating them would produce syntax errors.

  • Initialize an anonymous array:

new elementType[]{value0,value1,value2… };

This representation creates a new array and initializes it with the values provided in parentheses. The size of the array is the number of initial values. Using this syntax, you can reinitialize the array with new variables.

arrayRefVar = new elementType[]{value0,value1,value2… };

(3) Access an array element

Array elements can be accessed by subscripts. Array subscripts range from 0 to arrayRefvar.length-1.

When allocating space to an array, you must specify the number of elements that the array can store to determine its size. You can’t change the size of an array once it’s created. You can use ArrayRefvar. length to get the size of the array

Each element in an array can be represented using the following syntax, called indexed variable

arrayRefVar[index];

Array reference variable [subscript];

Loop through the number group:

  • Count for loops:

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

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

}

  • Enhance the for each loop

for ( int element:arrayRefVar){

System.out.println(element);

(4) Array copy

  • Shallow copy

When an array variable is copied to another array variable, both variables reference the same array

elementType[] new_array = arrays ;

This statement does not copy the contents of the array referenced by ArraySL to new_array, but only the values referenced by ArraySL to new_array. After that, both arrays and new_array point to the same array, essentially copying a reference from one array to another variable so that both variables point to the same memory address.

  • Deep copy

All the values of one array are copied into a new array,

Methods to copy an array:

  • Loop assignment

  • Use the copyOf method of the Arrays class

elementType[] new_array = Arrays. copyOf(arrays, arrays.length);

  • This method is usually used to increase the size of an array: if the array elements are numeric, the extra elements are assigned a value of 0. If the array element is Boolean, false is assigned. Conversely, if the length is less than the length of the original array, only the uppermost data element is copied.

  • Use the static method ArrayCopy in the System class

arraycopy(sourceArray, srcPos, targetArray, tarPos, length);

  • The arguments srcPos and tarPos represent the starting position in the sourceArray sourceArray and targetArray targetArray, respectively

  • The arrayCopy method does not allocate memory space to the target array. You must create the target array and the memory allocated to it before copying.

(5) Command line parameters

The String[] argument to the main method of every Java application is an array of strings.

  • Pass a string to the main method

java TestMain arg0 arg1 arg2

When you run the program, you can pass string arguments to the main method from the command line: the arguments arg0, arg1, and arg2 are strings, but do not need to be enclosed in double quotes when they appear on the command line. These strings are separated by Spaces. If the string contains Spaces, it must be enclosed in double quotes.

  • Use command line arguments

  • When the main method is called, the Java interpreter creates an array to store command-line arguments and passes a reference to that array to ARGS. Call command line arguments with args[index].

  • If you run the program without passing a String, use new String[0] to create an array. In this case, the array is an empty array of length 0. Args is a reference to this empty array. Therefore, args is not null, but args. Length is 0.

  • In the Main method of a Java application, the program name is not stored in the ARGS array

2. Multidimensional arrays

(1) Declare multidimensional array variables

Data type []… [] Array name;

Data type array name []… []; // This is allowed, but not recommended

(2) Multi-dimensional array initialization

  • Allocates memory space for array variables

  • Method 1 :(recommended)

elementType[]… [] arrayRefVar = new elementType[arraySize0]… [arraySizen];

(Element type []… [] Array reference variable =new element type [array size]… [array size];)

int[][] matrix;

  • Method 2 :(not recommended)

elementType arrayRefVar[]… [] = new elementType[arraySize0]… [arraySizen];

Element type array reference variable =new element type [array size]… [array size];)

int matrix[][];

  • Assign initial values to array variables

elementType[]… [] arrayRefVar = {{value},… ,{value}};

The length of each dimension can be different. Such arrays are called zigzag arrays

(3) Access multidimensional array elements

Array elements can be accessed by subscripts.

arrayRefVar[index0]… [index1];

Array reference variable [subscript 0]… The subscript 1)

  • Loop through a multidimensional array
  • Count loop nesting

  • Nesting of for-each loops (using a two-dimensional array as an example)

(4) Multidimensional arrays are arrays of arrays

Java doesn’t actually have multidimensional arrays, only one-dimensional arrays, and multidimensional arrays can be thought of as “arrays of arrays”.

Each n-dimensional array can be treated as an array of n-1 dimensional arrays, where each element can be treated as a reference to the n-1 dimensional array:

So you can apply for a multidimensional array line by line:

When creating an array with the syntax new int[5][], you must specify the first subscript.

Well, today’s article is here, I hope to help you confused screen!