Nested loop

Form: for, while, do… Inside the body of the while, there is another loop (for, while, do… While) run the formula: the outer loop loop once, the inner loop loop once

Array declaration and initialization

Array declaration:

/ / recommendElement data type [] The name of a two-dimensional array;/ / do not recommendElement data type two-dimensional array name [];Copy the code

Method 1: Static initialization

  • Format:
Data type [] Array name = {element1Elements,2Elements,3...};// This must be done in one statement
Copy the code
  • For example:

Defines an array container for integers 1,2,3,4,5

int[] arr = {1.2.3.4.5};/ / right

int[] arr;
arr = {1.2.3.4.5};/ / error
Copy the code

Method 2: Static initialization

  • Format:
Data type [] Array name =newData type []{element1Elements,2Elements,3.. }; Or data type [] array name; Array name =newData type []{element1Elements,2Elements,3...};
Copy the code
  • For example:

Defines an array container for integers 1,2,3,4,5.

int[] arr = new int[] {1.2.3.4.5};/ / right

int[] arr;
arr = new int[] {1.2.3.4.5};/ / right

int[] arr = new int[5] {1.2.3.4.5};{} specifies the element list, so you don't need to specify the length in [length].
Copy the code

Method 3: Dynamic initialization

  • Format:
The data type of the element stored in an array [] Array name =newThe data type [length] of the element stored in the array; [] The name of the array; Array name =newThe type of data stored in the array [length];Copy the code
  • Array definition format:
    • The data types of the elements the array stores: What data types the array container can store.
    • The type of the element can be any Java data type. For example: int, String, Student, etc
    • [] : indicates an array.
    • Array name: Gives a variable name to the array defined by an identifier specification. You can manipulate the array by name.
    • New: keyword, used to create arrays. Because the array itself is a reference data type, we create the array object with new.
    • [Length] : The length of the array, indicating how many elements can be stored in the array container.
    • Note: Arrays have a fixed length. Once specified, the length cannot be changed.

Array traversal

  • Array length property:Each array has a fixed length. Java assigns an array property to obtain the length of the array:An array of lengthThe result of the length property is the length of the array, the result of type int. It can be inferred from this that the maximum index value of the array isAn array of length - 1.
  • Array traversal: To get each element of the array separately, that is, traversal. Traversal is also the cornerstone of array manipulation.
Public static void main(String[] args) {int[] arr = new int[]{1,2,3,4,5}; Println (" Array length: "+ arr.length); System.out.println(" The elements of the array are: "); for(int i=0; i<arr.length; i++){ System.out.println(arr[i]); }}Copy the code

Idea shortcut itar can quickly generate traversal loops

Memory analysis of arrays

Java programs are run in the JVM.

The JVM divides memory into several parts:

  • Java method stack: Used to store local variables in methods, such as the main method’s local variables
  • Heap (for storing new objects)
  • Local method stack, which is the memory that Java needs to use when calling C code
  • Method area for loading stored classes, static data, constants, and so on
  • Program counter, used to record the next instruction of each thread

The elements of an array are stored in contiguous space, each of which is contiguous

The data type of an array element determines how much memory each element occupies

For example, the int type is 4 bytes

The JVM numbers each byte of memory, and arrays are contiguous, so simply record the address of the first byte of memory for the first element

Since Java runs on the JVM, we can’t actually get the real address.

The array name is a variable that stores the first addresses of all the array elements.

We print the array name directly and the result looks something like: [I@4554617c

We can chop it down to a memory address, but it’s not.

Because Java does not expose memory addresses. This is what it stands for:

[: indicates an array. If [[: indicates a two-dimensional array

I: indicates that the element is of type int

@ : is a hyphen

4554617c: is the array object HashCode value, the previous HashCode, if you need to put the object into the hashtable, 4554647C can be used as the id number of the object.

public class Exam1 {
 public static void main(String[] args) {
     int[] arr = new int[5];
     System.out.println(arr[4]); }}// It runs as follows
/ / 0
Copy the code

The subscript

To access an element, use the array name [subscript] format.

Array name: Gets the first address

[subscript] : This element, several elements separated from the first address, cell

The first element is 0 elements/cells away from the first address

The second element is 1 element/cell away from the lower value

public static void main(String[] args){
    int a = 1;// This a is a local variable stored in the stack
    int[] arr = new int[5];// The new part is stored in "heap", arR is also local variable, stored in "stack"
    System.out.println("-- -- -- -- -- -- -- -- -- --");
    int arr1 = {1.2.3.4.5};
    System.out.println(arr1);//[I@4554617c
    System.out.println("-- -- -- -- -- -- -- -- -- --");
    int[] arr2 = new int[5];
    
}
Copy the code

Because an array can be accessed directly to an element by [subscript], it can be accessed efficiently

The new keyword means: to request a piece of memory space.

Therefore, when the array is initialized, the element type and length must be specified. Only when you know the element type and length can the JVM know how much space it needs to allocate to you.