JAVA Foundation-Chapter 5 Array

Today's content

Array concept
Array definition
Array index
Array memory
Array traversal
Array maximum value acquisition
Array inversion
Array as method parameter and return value

teaching objectives

Understand the concept
of containers, master the first definition
of arrays, master the second definition
of arrays, master the third definition of arrays,
use indexes to access array elements,
understand the memory diagram of arrays,
understand null pointers and out-of-bounds exceptions,
master the traversal
of arrays Obtaining the maximum value of an
array Understand the principle of array inversion
Understand how arrays are passed as method parameters
Understand arrays as method return values

Chapter One Array Definition and Access

1. 1 Container overview

case analysis

Now we need to count the salary of employees of a company, such as calculating the average salary, finding the highest salary, etc. Assuming that the company has 50 employees, using the knowledge learned before, the program first needs to declare 50 variables to remember each employee’s salary, and then perform operations. This will be very troublesome and the error rate will be very high. high. So we can use containers for operations. Store all data in one container and operate uniformly.

Container concept

Container: Store multiple data together, and each data is called an element of the container.
Containers in life: drinking glasses, wardrobes, classrooms

1. 2 Array concept

Array concept: Array is a container for storing data with a fixed length, ensuring that the data types of multiple data must be consistent.

1. 3 Array definition

method one

format:

Detailed explanation of array definition format:

Data type of array storage: What data type can be stored in the created array container.

[]: Represents an array.

Array name: Give a variable name to the defined array, which satisfies the identifier specification, and can use the name to manipulate the array.

new:关键字,创建数组使用的关键字。
数组存储的数据类型: 创建的数组容器可以存储什么数据类型。
[长度]:数组的长度,表示数组容器中可以存储多少个元素。
注意:数组有定长特性,长度一旦指定,不可更改。
和水杯道理相同,买了一个 2 升的水杯,总容量就是 2 升,不能多也不能少。
举例:

Define an array container that can store 3 integers, the code is as follows:

Way two

format:

For example:

Define an array container that stores 1, 2, 3, 4, and 5 integers.

Way Three

format:

数组存储的数据类型[] 数组名字 = new 数组存储的数据类型[长度];
int[] arr = new int[ 3 ];
数据类型[] 数组名 = new 数据类型[]{元素 1 ,元素 2 ,元素 3 ...};
int[] arr = new int[]{ 1 , 2 , 3 , 4 , 5 };

For example:

Define an array container for storing 1, 2, 3, 4, 5 integers

1. 4 array access

Index: Every element stored in the array will automatically have a number, starting from 0, this automatic number is called the array index

(index),可以通过数组的索引访问到数组中的元素。
格式:
数组的长度属性: 每个数组都具有长度,而且是固定的,Java中赋予了数组的一个属性,可以获取到数组的
长度,语句为:数组名.length ,属性length的执行结果是数组的长度,int类型结果。由次可以推断出,数
组的最大索引值为数组名.length- 1 。

Index to access elements in the array:

Array name [index] = value, assign values ​​to the elements in the
array Variable = array name [index], get the elements in the array

Chapter 2 Array Principle Memory Diagram

2. 1 Memory overview

Data type [] Array name = {element 1, element 2, element 3 …};

int[] arr = { 1 , 2 , 3 , 4 , 5 };
```数组名[索引]

public static void main(String[] args) { int[] arr = new int[]{ 1, 2, 3, 4, 5 }; //Print the attributes of the array, the output result is 5 System.out.println(arr .length); }



public static void main(String[] args) { //Define storage int type array, assign elements 1, 2, 3, 4, 5 int[] arr = {1, 2, 3, 4, 5 }; // is 0 index element is assigned to 6 arr[ 0] = 6; //get the element at the index of array 0 int i = arr[ 0 ]; System.out.println(i); // directly output array 0 index element System.out .println(arr[ 0 ]); }










### 内存是计算机中的重要原件,临时存储区域,作用是运行程序。我们编写的程序是存放在硬盘中的,在硬盘中的程

### 序是不会运行的,必须放进内存中才能运行,运行完毕后会清空内存。

Java虚拟机要运行程序,必须要对内存进行空间的分配和管理。

## 2. 2 Java虚拟机的内存划分

### 为了提高运算效率,就对空间进行了不同区域的划分,因为每一片区域都有特定的处理数据方式和内存管理方式。

### 区域名称 作用

### 寄存器 给CPU使用,和我们开发无关。

### 本地方法栈 JVM在使用操作系统功能的时候使用,和我们开发无关。

The method area stores executable class files.

Heap memory stores objects or arrays, and those created by new are all stored in heap memory.

Method stack The memory used when the method is running, such as the main method running, enters the method stack for execution.

### JVM的内存划分:

## 2. 3 数组在内存中的存储

## 一个数组内存图

以上方法执行,输出的结果是[I@ 5 f 150435 ,这个是什么呢?是数组在内存中的地址。new出来的内容,都是在堆
内存中存储的,而方法中的变量arr保存的是数组的地址。

输出arr[ 0 ],就会输出arr保存的内存地址中数组中 0 索引上的元素

public static void main(String[] args) {
int[] arr = new int[ 3 ];
System.out.println(arr);//[I@ 5 f 150435
}


## 两个数组内存图

public static void main(String[] args) {
int[] arr = new int[ 3 ];
int[] arr 2 = new int[ 2 ];
System.out.println(arr);
System.out.println(arr 2 );
}


## 两个变量指向一个数组

# 第三章 数组的常见操作

## 3. 1 数组越界异常

### 观察一下代码,运行后会出现什么结果。

public static void main(String[] args) { // Define the array and store 3 elements int[] arr = new int[ 3 ]; // Assign the array index arr[ 0] = 5; arr[ 1] = 6 ; arr[ 2] = 7; //Output the element value on 3 indexes System.out.println(arr[ 0 ]); System.out.println(arr[ 1 ]); System.out.println(arr[ 2 ]); //Define the array variable arr 2 and assign the address of arr to arr 2 int[] arr 2 = arr; arr 2 [1] = 9; System.out.println(arr[ 1 ]); }















### 创建数组,赋值 3 个元素,数组的索引就是 0 , 1 , 2 ,没有 3 索引,因此我们不能访问数组中不存在的索引,程序运

行后,将会抛出 ArrayIndexOutOfBoundsException 数组越界异常。在开发中,数组的越界异常是不能出现的,一
旦出现了,就必须要修改我们编写的代码。

## 3. 2 数组空指针异常

### 观察一下代码,运行后会出现什么结果。

arr = null这行代码,意味着变量arr将不会在保存数组的内存地址,也就不允许再操作数组了,因此运行的时候
会抛出NullPointerException 空指针异常。在开发中,数组的越界异常是不能出现的,一旦出现了,就必须要修
改我们编写的代码。

### 空指针异常在内存图中的表现

public static void main(String[] args) {
int[] arr = { 1 , 2 , 3 };
System.out.println(arr[ 3 ]);
}

public static void main(String[] args) {
int[] arr = { 1 , 2 , 3 };
arr = null;
System.out.println(arr[ 0 ]);


## 3. 3 数组遍历【重点】

### 数组遍历: 就是将数组中的每个元素分别获取出来,就是遍历。遍历也是数组操作中的基石。

### 以上代码是可以将数组中每个元素全部遍历出来,但是如果数组元素非常多,这种写法肯定不行,因此我们需要改

造成循环的写法。数组的索引是 0 到lenght- 1 ,可以作为循环的条件出现。

## 3. 4 数组获取最大值元素

public static void main(String[] args) {
int[] arr = { 1 , 2 , 3 , 4 , 5 };
System.out.println(arr[ 0 ]);
System.out.println(arr[ 1 ]);
System.out.println(arr[ 2 ]);
System.out.println(arr[ 3 ]);
System.out.println(arr[ 4 ]);
}

public static void main(String[] args) {
int[] arr = { 1 , 2 , 3 , 4 , 5 };
for (int i = 0 ; i < arr.length; i++) {
System.out.println(arr[i]);
}
}


### 最大值获取:从数组的所有元素中找出最大值。

### 实现思路:

### 定义变量,保存数组 0 索引上的元素

### 遍历数组,获取出数组中的每个元素

### 将遍历到的元素和保存数组 0 索引上值的变量进行比较

### 如果数组元素的值大于了变量的值,变量记录住新的值

### 数组循环遍历结束,变量保存的就是数组中的最大值

## 3. 5 数组反转

public static void main(String[] args) { int[] arr = {5, 15, 2000, 10000, 100, 4000 }; //Define the variable and save the 0-indexed element in the array int max = arr[ 0 ]; //Traverse the array and take out each element for (int i = 0; i <arr.length; i++) { //Compare the traversed element with the variable max // If the array element is greater than max if (arr[i]> max ) { //max records the maximum value max = arr[i]; } } System.out.println("The maximum value of the array is: "+ max); }














### 数组的反转: 数组中的元素颠倒顺序,例如原始数组为 1 , 2 , 3 , 4 , 5 ,反转后的数组为 5 , 4 , 3 , 2 , 1

### 实现思想:数组最远端的元素互换位置。

### 实现反转,就需要将数组最远端元素位置交换

### 定义两个变量,保存数组的最小索引和最大索引

### 两个索引上的元素交换位置

### 最小索引++,最大索引--,再次交换位置

### 最小索引超过了最大索引,数组反转操作结束

# 第四章 数组作为方法参数和返回值

## 4. 1 数组作为方法参数

### 以前的方法中我们学习了方法的参数和返回值,但是使用的都是基本数据类型。那么作为引用类型的数组能否作为

### 方法的参数进行传递呢,当然是可以的。

### 数组作为方法参数传递,传递的参数是数组内存的地址。

public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5 }; /*The variable defined in the loop min = 0 minimum index max=arr.length-1 maximum index min++, max- */ for (int min = 0, max = arr.length -1; min <= max; min++, max-) { //Use third-party variables to complete the exchange of elements in the array int temp = arr[min ]; arr[min] = arr[max]; arr[max] = temp; } // After inversion, traverse the array for (int i = 0; i <arr.length; i++) { System.out.println( arr[i]); } }

















## 4. 2 数组作为方法返回值

### 数组作为方法的返回值,返回的是数组的内存地址

public static void main(String[] args) { int[] arr = {1, 3, 5, 7, 9 }; //Call method, pass array printArray(arr); } /* Create method, method receives array type Parameter of traversing the array */ public static void printArray(int[] arr) { for (int i = 0; i <arr.length; i++) { System.out.println(arr[i]); } }












public static void main(String[] args) { //Call the method and receive the return value of the array//Receive the memory address of the array int[] arr = getArray(); for (int i = 0; i <arr .length; i++) { System.out.println(arr[i]); } } /* Create method, return value is array type









## 4. 3 方法的参数类型区别

## 代码分析

### 1. 分析下列程序代码,计算输出结果。

### 创建方法,返回值是数组类型

return returns the address of the array
*/
public static int[] getArray() { int[] arr = {1, 3, 5, 7, 9 }; //returns the address of the array and returns to the caller return arr; }



public static void main(String[] args) {
int a = 1 ;
int b = 2 ;
System.out.println(a);
System.out.println(b);
change(a, b);
System.out.println(a);
System.out.println(b);
}


### 2. 分析下列程序代码,计算输出结果。

### 总结:

### 方法的参数为基本类型时,传递的是数据值. 方法的参数为引用类型时,传递的是地址值.

public static void change(int a, int b) {
a = a + b;
b = b + a;
}

public static void main(String[] args) {
int[] arr = { 1 , 3 , 5 };
System.out.println(arr[ 0 ]);
change(arr);
System.out.println(arr[ 0 ]);
}

public static void change(int[] arr) {
arr[ 0 ] = 200 ;
}

Guess you like

Origin blog.csdn.net/weixin_43419256/article/details/108198318