The definition and use of arrays in detail (Java)

1: Basic concepts of arrays

1.1 What is an array?

Array: It can be regarded as a collection of elements of the same type. It is a continuous space in memory.
a. The elements stored in the array are of the same type
b. The spaces of the array are connected together
c. Each space has its own number , the starting position number (ie the subscript) is 0 , and the end position number is the array length-1
d. The array is a reference type

1.2 Array creation and initialization

1.2.1 Array creation

/*
T[] 数组名=new T[N];
T:表示数组中存放的数据类型
T[]:表示数组的类型
N:表示数组的长度
*/
int[] array1=new int[10];
float[] array2=new float[10];
double[] array3=new double[10];
String[] array4=new String[10];

1.2.2 Array initialization

/*
	数组的初始化主要分为 静态初始化 和 动态初始化
		a.动态初始化:在创建数组时,直接指定数组中元素的个数.
		  如果不确定数组当中内容时,建议使用动态初始化.
*/
	int[] array=new int[10];
/*
		b.静态初始化:在创建数组时不直接指定数组的元素个数,
			而直接将具体的数据类容进行指定.
		如果确定数组当中的内容,建议使用静态初始化
		注意:静态初始化虽然没有指定数组的长度,
			   但是编译器在编译时会根据{}中元素个数来确定数组的长度。
		语法格式:T[] 数组名=new T[]{data1,data2,data3...datan};
		简化:T[] 数组名={data1,data2,data3...datan};
*/
	int[] array1 = new int[]{
    
    0,1,2,3,4,5,6,7,8,9};
	double[] array2 = new double[]{
    
    1.0, 2.0, 3.0, 4.0, 5.0};
	String[] array3 = new String[]{
    
    "hell", "Java", "!!!"}
	//代码简化,可以省略=右边的new int[]
	int[] array4 ={
    
    0,1,2,3,4,5,6,7,8,9};
	double[] array5 ={
    
    1.0, 2.0, 3.0, 4.0, 5.0};
	String[] array6 ={
    
    "hell", "Java", "!!!"};
	
	/*创建数组的时候,也可以使用C语言的方式进行创建,但是不建议.
		原因:容易造成数组的类型的误解
		[]如果在类型之后,就表示数组类型,
		int[]结合在一块写意思更明确
*/
	int array7[]={
    
    1,2,3,4,5,6,7,8,9,0};

3. The use of arrays

1.3.1 Access to elements in an array.

The array is a continuous space in the memory. The number of the space starts from 0 and increases in turn.
The number is called the subscript of the array, and the array can access its elements at any position through the subscript.

Notes:
a. The array is a continuous content space, so it supports random access, that is, fast access to elements in any position in the array through subscript access
b. The subscript starts from 0, and does not contain between [0, N) N, N is the number of elements, which cannot be out of bounds, otherwise a subscript out-of-bounds exception will be reported.

	int[]array = new int[]{
    
    10, 20, 30, 40, 50};
    System.out.println(array[0]);		//10
    System.out.println(array[1]);		//20
    System.out.println(array[2]);		//30
    System.out.println(array[3]);		//40
    System.out.println(array[4]);		//50
     也可以通过[]对数组中的元素进行修改
    array[0] = 100;
    System.out.println(array[0]);		//100
    //出错,下标时0~4,如果是5,会发生数组下标越界
    //System.out.println(array[5]);

1.3.2 Traversing an array

The so-called traversal refers to accessing all the elements in
the array. Access refers to performing some operation on the elements in the array.

//遍历数组,可以通过for循环,array.length时数组的长度
//在数组中,可以使用 数组对象(数组名).length来获取数组的长度.
    for (int i = 0; i < array.length; i++) {
    
    
    	//注意此处不用println,不需要换行,只需要加个空格
         System.out.print(array[i]+" ");//100 20 30 30 50
    }

2: Arrays are reference types

2.1 JVM memory distribution (for the time being)

Memory is a continuous storage space, mainly used to store program runtime data.
a. The code needs to be loaded into the memory when the program is running
. b. The intermediate data generated by the program running should be stored in the memory
.
To be destroyed
Therefore, the JVM also divides the memory used according to the function.
insert image description here
Program counter (PC Register): just a small space, saves the address of the next executed instruction.
Virtual machine stack (JVM Stack): with Some information related to method invocation. When each method is executed, a stack frame will be created first. The stack frame contains: local variable table, operand stack, dynamic link, return address and other information. Some information related to the execution of the method. For example: local variables. When the method finishes running, the stack frame is destroyed, that is, the data stored in the stack frame is also destroyed.
Native method stack (Native Method Stack): The function of the native method stack is similar to that of the virtual machine stack. The only thing saved is the local variables of the Native method. In some versions of the JVM implementation (such as HotSpot), the native method stack and the virtual machine The stack is together.
Heap: The largest memory area managed by the JVM. Objects created with new are saved on the heap (such as the previous new int[]{1, 2, 3} ), the heap is with the It is created when the program starts to run and is destroyed when the program exits. As long as the data in the heap is still in use, it will not be destroyed.
Method Area: It is used to store data such as class information, constants, static variables, and code compiled by the real-time compiler that have been loaded by the virtual machine. The bytecode compiled by the method is stored in this area.
A brief understanding here is enough. If you want to learn more, you can teach yourself a wave of JVM.

2.2 The difference between basic type variables and reference type variables.

The variable created by the basic data type is called the basic variable, and the corresponding value is directly stored in the variable space. The variable
created by the reference data type is generally called the reference of the object, and its space is stored in the space where the object is located. address.

public static void main(String[] args){
    
    
		int a=10;
		int b=20;
		int[] arr={
    
    1,2,3};
}

In the above code, a, b, arr are all variables inside the method, so their space is allocated in the stack frame corresponding to the main method.
a and b are variables of built-in type, so what is stored in their space is the value initialized for the variable.
Array is a reference variable of the array type, and the content stored in it can be simply understood as the first address of the array in the heap space.
insert image description here

As can be seen from the above figure, the reference variable does not directly store the object itself, it can be simply understood that it stores the starting address of the object in the heap space. Through this address, the reference variable can be used to manipulate the object. Similar to pointers in C, but references in Java are easier to manipulate than pointers.

2.3. Recognize null

null means "null reference" in Java, that is, a reference that does not point to an object.

int[] arr = null;
System.out.println(arr[0]);
// 执行结果
/*Exception in thread "main" java.lang.NullPointerException at
	Test.main(Test.java:6)*/

The role of null is similar to NULL (null pointer) in the C language, and both represent an invalid memory location. Therefore, no read or write operations can be performed on this memory
. Once you try to read or write, a NullPointerException will be thrown
Note: In Java There is no convention that null has any association with memory at address 0.

3: Array practice

3.1. Array to string.

import java.util.Arrays;
public static void main(String[] args) {
    
    
	//定义一个数组,并且初始化
	int[] array={
    
    10,20,30,40,50,60};
	String newArr = Arrays.toString(arr);
	System.out.println(newArr);//结果为:[10,20,30,40,50,60]
	//使用这个方法去打印数组 
	//开始要导java.util.Arrays包,其中包含了一些操作数组的常用方法.会比较方便
}

3.2 Array copy

import java.util.Arrays;
public static void main(String[] args){
    
    
	 // newArr和arr引用的是同一个数组
	 // 因此newArr修改空间中内容之后,arr也可以看到修改的结果
	 int[] arr = {
    
    1,2,3,4,5,6};
	 int[] newArr = arr;
	 newArr[0] = 10;
	 //打印的结果为:newArr: [10, 2, 3, 4, 5, 6]
	 System.out.println("newArr: " + Arrays.toString(arr));
	 
	 /* 
	 	使用Arrays中copyOf方法完成数组的拷贝:
	 	copyOf方法在进行数组拷贝时,创建了一个新的数组
	    arr和newArr引用的不是同一个数组
*/
	 arr[0] = 1;
/*
	 Arrays.copyOf(array,newLength);
	 在copyOf当中,创建了一个新的数组,然后将array中,
	 newLength 个元素拷贝到新数组中.
*/
	 newArr = Arrays.copyOf(arr, arr.length);
	 //打印的结果为:newArr: [1, 2, 3, 4, 5, 6]
	 System.out.println("newArr: " + Arrays.toString(newArr));
	 
	 
	 // 因为arr修改其引用数组中内容时,对newArr没有任何影响
	 arr[0] = 10;
	 //打印结果为:		arr: [10, 2, 3, 4, 5, 6]
	 //			   	    newArr: [1, 2, 3, 4, 5, 6]
	 System.out.println("arr: " + Arrays.toString(arr));
	 System.out.println("newArr: " + Arrays.toString(newArr));
	 
	 
	 /* 
	 	拷贝某个范围.
	 	Arrays.copyOfRange(array,from,to);
	 		array是需要拷贝的数组,from是起始下标,to是结尾下标.
	 		from一定要比to小,否则就会抛出异常
	 */
	 int[] newArr2 = Arrays.copyOfRange(arr, 2, 4);
	 //打印的结果为:	newArr2: [3, 4]
	 System.out.println("newArr2: " + Arrays.toString(newArr2));
 }

int[ ] arr={1,2,3,4,5};
int[ ] newArr=arr;
insert image description here

3.3 Exercises for arrays

3.3.1 Java basic practice questions Array

Java basic practice questions Array

3.3.2 Bubble sort

Bubble Sort

Guess you like

Origin blog.csdn.net/weixin_47278183/article/details/120267902