数组
1.概述
- 一次性声明大量的用于存储数据的变量
- 这写存储的数据一般都是相同的数据类型
- 数组是一个用于存储多个相同类型数据的存储模型
2.一维数组的使用
- 一维数组的声明和初始化
- 调用数组的指定元素
- 数组的属性:length,(数组的长度)
- 数组的遍历
- 数组的默认初始值
- 一维数组的内存解析
3.数组的定义
-
范例1
int [] arr;(推荐使用)
含义:先定义数组,在定义数组名
-
范例2
int arr [];
含义:先定义数组名,在定义数组;
4.数组的初始化
- 动态初始化
- int [] arr = new int[5];
package com.ygc;
/**
* @author: YGC
* @createTime: 2023/08/16 22:30
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 数组
*/
public class ArrayDemo {
public static void main(String[] args) {
int [] arr = new int[5];
/*
1.int 数组的数据类型 []代表数组
2. arr代表的数组名
3. 第二个int 表示的时数组的数据类型要与前面定义的相同
4.[5]表示定义了一个数组长度为5的数组[0-4]
5.数组会分配一个初始值,值是多少油数据类型决定
* */
}
}
5.数组的访问
需要注意的是,数组从0开始计算
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = new int[5];
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
int c = arr[2];
System.out.println("c=" + c);
}
}
6.数组的内存分配
- 做new操作时都会将该对象(实例)存放在堆内存中
- 堆内存会将该实例的地址赋值给栈内存,也就是定义数组的时候的数组名
- 此时输出的是该数组在堆内存中的地址,用于栈内存访问
System.out.println(arr);//[I@4554617c
6.1访问顺序
执行程序时,先根据栈内存中的地址,访问到堆内存,然后根据下标找到对应的数
6.2注意事项
- 数组在初始化的时候,会为存储空间分配默认值
int :0
double :0.0
boolean :false
char:0
String : null
- 如果数组指向相同的堆内存地址,则不会在开辟新的空间,而是将原来的空间取代
package com.ygc;
public class ArrayDemo {
public static void main(String[] args) {
int [] arr1 = new int[5];
arr1[0] = 100;
arr1[1] = 50;
System.out.println(arr1);//[I@4554617c
System.out.println(arr1[0]);
System.out.println(arr1[1]);
int [] arr2 = arr1 ;//将内存赋值给arr2
arr2[0] = 300;
arr2[1] = 10;
System.out.println(arr2);//[I@4554617c
System.out.println(arr2[0]);//300
System.out.println(arr2[1]);//10
System.out.println(arr1[0]);//300
System.out.println(arr1[1]);//10
}
}
栈内存
- 栈内存是一片存储区域,用来存储局部变量
- 堆内存:凡是通过new建立的都是在堆内存中,存放的都是 实体(对象 )
7 数组静态初始化
静态初始化指的就是,在数组初始化的时候,赋给数组值,并由系统决定数组的长度
7.1 格式
int [] arr = new int[]{1,2,3}
数组类型[] 数组名 = new 数组类型[]{数组元素}
7.2 格式 2
int[] arr = {1,2,3};
数组类型[] 数组名 = {数组元素}
package com.ygc;
/**
* @author: YGC
* @createTime: 2023/08/17 9:57
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 静态数组
*/
public class ArrDemo01 {
public static void main(String[] args) {
/*定义一个静态数组*/
int[] arr = {1,2,3,4};
int[] arr1 = new int[]{1,2,3,4,5};
System.out.println(arr[0]);
System.out.println(arr1[4]);
}
}
7.常见问题
7.1 数组访问越界
- 意思是定义了一个四个长度的数组却引用了第五个元素造成了本来不存在的元素,却让计算机访问
package com.ygc;
/**
* @author: YGC
* @createTime: 2023/08/17 10:01
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 数组常见错误
*/
public class ArrDemo02 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
System.out.println(arr[4]);
}
}
//错误代码
/*Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at com.ygc.ArrDemo02.main(ArrDemo02.java:12)*/
7.2空指针异常,
原数组已经不在堆内存中具有地址,但你仍然使用数组名访问数组元素
package com.ygc;
/**
* @author: YGC
* @createTime: 2023/08/17 10:01
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 数组常见错误
*/
public class ArrDemo02 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
arr = null;
System.out.println(arr[3]);
/*空指针异常
java.lang.NullPointerException
*/
}
}
8.数组遍历
8.1使用for循环实现数组的遍历
package com.ygc;
/**
* @author: YGC
* @createTime: 2023/08/17 10:18
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 遍历数组
*/
public class ArrDemo03 {
public static void main(String[] args) {
int[] arr = {1,2,3,4};
for (int i = 0; i < 4; i++) {
System.out.println(arr[i]);
}
}
}
8.2使用API进行数组的遍历
arr.length:表示的是数组的长度,数组元素的个数,不是数组的下标
package com.ygc;
/**
* @author: YGC
* @createTime: 2023/08/17 10:18
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 遍历数组
*/
public class ArrDemo03 {
public static void main(String[] args) {
int[] arr1 = {66,77,88,99};
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);
}
}
}
9.练习
9.1数组获取最大值
package com.ygc;
/**
* @author: YGC
* @createTime: 2023/08/17 10:39
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 数组获取最大值
*/
public class ArrExer01 {
public static void main(String[] args) {
int tempMax = 0;
int max = 0;
int[] arr = {170,162,131,168,190};
/*此时arr.length不满足条件,因为下面需要使用arr[i+1],会使数组下标越界*/
for (int i = 0; i < arr.length-1; i++) {
if (arr[i] > arr[i+1]) {
tempMax = arr[i];
arr[i+1] = arr[i];
arr[i+1] = tempMax;
}
max = arr[i+1];
}
System.out.println("最大的值是:"+max);
}
}
9.2获取数组最大值练习
package com.ygc;
/**
* @author: YGC
* @createTime: 2023/08/17 10:22
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 获取数组最大值练习
*/
public class ArrExer {
public static void main(String[] args) {
int[] arr = {170,162,131,168,190};
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("最大的值是:"+max);
}
}
9.3从键盘读入学生成绩,找出最高分,并输出学生成绩等级。
package com.ygc.day03;
import java.util.Scanner;
/**
* @author: YGC
* @createTime: 2023/08/18 13:04
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 从键盘读入学生成绩,找出最高分,并输出学生成绩等级。
*/
public class ArrExer02 {
/*
* 1.定义一个数组,用for循环接收键盘输入的值
* 2.找出最大值
* 成绩>=最高分-10 等级为’A’
* 成绩>=最高分-20 等级为’B’
* 成绩>=最高分-30 等级为’C’
* 其余 等级为’D’
* 3.使用if语句评级
* */
public static void main(String[] args) {
Scanner studentNumber = new Scanner(System.in);
System.out.println("请输入学生的人数");
int count = studentNumber.nextInt();
double[] scores = new double[count];
for (int i = 0; i < count; i++) {
Scanner input = new Scanner(System.in);
System.out.println("请输入第" + (i + 1) + "个学生的成绩");
double score = input.nextDouble();
scores[i] = score;
}
double max = scores[0];
for (int j = 0; j < scores.length; j++) {
if (max < scores[j]) {
max = scores[j];
}
}
System.out.println("最高分是" + max);
for (int k = 0; k < scores.length; k++) {
if (scores[k] > max - 10) {
char grade = 'A';
System.out.println("第" + (k + 1) + "个学生的等级是" + grade);
} else if (scores[k] > max - 20) {
char grade = 'B';
System.out.println("第" + (k + 1) + "个学生的等级是" + grade);
} else if (scores[k] > max - 30) {
char grade = 'C';
System.out.println("第" + (k + 1) + "个学生的等级是" + grade);
} else {
char grade = 'D';
System.out.println("第" + (k + 1) + "个学生的等级是" + grade);
}
}
}
}
9. 4遍历扑克牌
package com.ygc.Exercise;
/**
* @author: YGC
* @createTime: 2023/08/20 12:55
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 遍历扑克牌
*/
public class ArrayExer_PukePai {
public static void main(String[] args) {
String[] hua = {"黑桃", "红桃", "梅花", "方片"};
String[] dian = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
String[] puke = new String[52];
int k = 0;
/*System.out.print(puke[i] + "\t");*/
for (int i = 0; i < hua.length; i++) {
for (int j = 0; j < dian.length; j++) {
puke[k++] = hua[i] + dian[j];
}
}
for (int i = 1; i <= puke.length; i++) {
System.out.print(puke[i - 1] + "\t");
if (i % 13 == 0) {
System.out.println(" ");
}
}
}
}
9.5输出随即不同的数,每个数都不相同
package com.ygc.Exercise;
/**
* @author: YGC
* @createTime: 2023/08/20 11:58
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 随即不同的数
*/
public class ArrayExer_DifferentNumber {
/*创建一个长度为6的int型数组,要求数组元素的值都在1-30之间,且是随机赋值。同时,要求元素的值各不相同。 */
public static void main(String[] args) {
//1.创建一个长度为6的数组
int[] arr = new int[6];
//2.输出随机数1-30,并赋值给数组
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 6) + 1;
//3.控制元素各不相同
boolean flag = false;
while (true) {
for (int j = 0; j < i; j++) {
if (arr[i] == arr[j]) {
flag = true;
break;
}
}
if (flag) {
arr[i] = (int) (Math.random() * 6) + 1;
flag = false;
continue;
}
break;
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
//更优的方法
}
9.6实现评委打分
package com.ygc.Exercise;
/**
* @author: YGC
* @createTime: 2023/08/19 18:21
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 实现评委打分
* 案例:评委打分
* 分析以下需求,并用代码实现:
* (1)在编程竞赛中,有10位评委为参赛的选手打分,分数分别为:5,4,6,8,9,0,1,2,7,3
* (2)求选手的最后得分(去掉一个最高分和一个最低分后其余8位评委打分的平均值)
*/
public class ArrayExer_pingfeng {
public static void main(String[] args) {
int[] scores = {5, 4, 6, 8, 9, 0, 1, 2, 7, 3};
int max = scores[0];
int sumScore = 0;
double avgScore = 0;
int min = scores[0];
for (int i = 0; i < scores.length; i++) {
if (max < scores[i]) {
max = scores[i];
}
if (min > scores[i]) {
min = scores[i];
}
sumScore += scores[i];
}
double sumScore1 = sumScore - max - min;
avgScore = sumScore1 / 8;
System.out.println(avgScore);
}
}
9.7 一个数组,让数组的每个元素除第一个元素,得到的商作为被除数所在位置的新值。
package com.ygc.Exercise;
/**
* @author: YGC
* @createTime: 2023/08/20 11:32
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 数组的复制与赋值
*/
public class ArrayExerCopy {
public static void main(String[] args) {
/* 一个数组,让数组的每个元素除第一个元素,得到的商作为被除数所在位置的新值。*/
int[] arr = new int[]{12, 43, 65, 3, -8, 64, 2};
/*取出第一个元素*/
int one = arr[0];
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] / one;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
9.9 数组的赋值
package com.ygc.Exercise;
/**
* @author: YGC
* @createTime: 2023/08/20 11:32
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 数组的复制与赋值
* 案例:复制、赋值
* 使用简单数组
* (1)创建一个名为ArrayExerCopy的类,在main()方法中声明array1和array2两个变量,他们是int[]类型的数组。
* (2)使用大括号{},把array1初始化为8个素数:2,3,5,7,11,13,17,19。
* (3)显示array1的内容。
* (4)赋值array2变量等于array1,修改array2中的偶索引元素,使其等于索引值(如array[0]=0,array[2]=2)。
* (5)打印出array1。
* 思考:array1和array2是什么关系?
* 【answer】array1和array2是两个变量,共同指向了堆空间中的同一个数组结构。即二者的地址值相同。
* 拓展:修改题目,实现array2对array1数组的复制
*/
public class ArrayExerCopy {
public static void main(String[] args) {
//1.创建一个array1,array2的数组
//(2)使用大括号{},把array1初始化为8个素数:2,3,5,7,11,13,17,19。
int[] array1 = {2, 3, 5, 7, 11, 13, 17, 19};
int[] array2 = {2, 3, 5, 7, 11, 13, 17, 19};
//(3)显示array1的内容。
for (int i = 0; i < array1.length; i++) {
System.out.print(array1[i] + "\t");
}
//(4)赋值array2变量等于array1,修改array2中的偶索引元素,使其等于索引值(如array[0]=0,array[2]=2)。
array2 = array1;
for (int i = 0; i < array2.length; i++) {
if (i % 2 == 0) {
array2[i] = i;
}
}
System.out.println();
//(5)打印出array1
for (int i = 0; i < array1.length; i++) {
System.out.print(array2[i] + "\t");
}
}
}
9.10 数组的赋值
package com.ygc.Exercise;
/**
* @author: YGC
* @createTime: 2023/08/20 11:32
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 数组的复制与赋值
*/
public class ArrayExerCopy {
public static void main(String[] args) {
//1.创建一个array1,array2的数组
//(2)使用大括号{},把array1初始化为8个素数:2,3,5,7,11,13,17,19。
int[] array1 = {2, 3, 5, 7, 11, 13, 17, 19};
int[] array2 = new int[array1.length];
//(3)显示array1的内容。
for (int i = 0; i < array1.length; i++) {
System.out.print(array1[i] + "\t");
}
//(4)赋值array2变量等于array1,修改array2中的偶索引元素,使其等于索引值(如array[0]=0,array[2]=2)。
for (int i = 0; i < array2.length; i++) {
array2[i] = array1[i];
if (i % 2 == 0) {
array2[i] = i;
}
}
System.out.println();
//(5)打印出array1
for (int i = 0; i < array1.length; i++) {
System.out.print(array1[i] + "\t");
}
}
}
9.11数组的反转
- 需要注意的是,反转到中间位置就不需要在反转了
arr.length / 2
package com.ygc.Exercise;
/**
* @author: YGC
* @createTime: 2023/08/20 15:10
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 数组的反转
* 公式:a[i]<---->a[length-1-i]
*/
public class ArrayExer_Rollback {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int temp = 0;
for (int i = 0; i < arr.length / 2; i++) {
temp = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = arr[i];
arr[i] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
9.12 判断是否是对称数组
package com.ygc.Exercise.exer02;
/**
* @author: YGC
* @createTime: 2023/08/20 15:33
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 判断数组是否是对称 数组
*/
public class ArrayExer_duicheng {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 4, 3, 2, 1};
boolean flag = true;
for (int i = 0; i < arr.length / 2; i++) {
while (true) {
if (arr[i] != arr[arr.length - 1 - i]) {
flag = false;
break;
} else {
flag = true;
break;
}
}
}
if (flag == true) {
System.out.println("是");
} else {
System.out.println("不是");
}
}
}
10数组的扩容与缩容(常见的数组算法)
10.1 扩容
package com.ygc.Exercise.exer03;
/**
* @author: YGC
* @createTime: 2023/08/20 15:50
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 数组的扩容与缩容
* 需求:将数组长度扩大一倍,并将10,20,30放入数组
*/
public class ArrarTest_enlargeAndShrink {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
//扩大数组的长度
int[] arr1 = new int[arr.length << 1];
//将旧数组的值赋值给新数组
for (int i = 0; i < arr.length; i++) {
arr1[i] = arr[i];
}
//在5后面增加新元素
arr1[arr.length] = 10;
arr1[arr.length + 1] = 20;
arr1[arr.length + 2] = 30;
//将新数组的地址赋值给旧数组
arr = arr1;
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
10.2 缩容
package com.ygc.Exercise.exer03;
/**
* @author: YGC
* @createTime: 2023/08/20 16:06
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 数组的缩容
*/
public class ArrayTest_Shrink {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7};
//删除4位置的元素
//1.找到4的下标,从4开始往后替换,到最后一个位置置为0(常用)
int deleteIndex = 3;
//后一个元素替换前一个元素
// for (int i = deleteIndex; i < arr.length - 1; i++) {
// arr[i] = arr[i + 1];
// }
//方式2.重新定义一个数组,长度是原来-1个
int[] newArr = new int[arr.length - 1];
for (int i = 0; i < deleteIndex; i++) {
newArr[i] = arr[i];
}
for (int i = deleteIndex; i < newArr.length; i++) {
newArr[i] = arr[i + 1];
}
arr = newArr;
//最后一个数重置为0
// arr[arr.length - 1] = 0;
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
10.3 顺序查找相应的元素值
- 优点:算法简单易懂
- 缺点:算法的复杂度高O(n)
package com.ygc.Exercise.exer03;
/**
* @author: YGC
* @createTime: 2023/08/20 16:25
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 线性查找
*/
public class ArrayTest_LinearSearch {
public static void main(String[] args) {
int count = 0;
int[] arr = {34, 54, 3, 2, 5, 7, 34, 5, 76, 34, 67};
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 9) {
count++;
}
}
if (count == 0) {
System.out.println("未找到该元素");
} else {
System.out.println("该元素出现了" + count + "次");
}
}
}
10.4 二分查找相应元素的值
- 优点:执行效率高O(logN)
- 缺点:相较于顺序查找难一点,前提是数组必须是有序的
package com.ygc.Exercise.exer03;
/**
* @author: YGC
* @createTime: 2023/08/20 16:50
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 二分查找
*/
public class ArrayTest_BinarySearch {
public static void main(String[] args) {
int[] arr = {2, 4, 5, 8, 12, 15, 19, 26, 37, 49, 51, 66, 89, 100, 101};
//定义头指针
int head = 0;
//定义尾指针
int end = arr.length - 1;
//定义需要寻找的数字
int target = 26;
boolean isFlag = false;
while (true) {
int mid = (head + end) / 2;
if (target == arr[mid]) {
System.out.println("找到了该元素在第" + (mid + 1) + "位置");
isFlag = true;
break;
} else if (target < arr[mid]) {
end = mid - 1;
} else {
head = mid + 1;
}
}
if (!isFlag) {
System.out.println("未找到");
}
}
}
10.5 冒泡排序(n^2)
package com.ygc.Exercise.exer03;
/**
* @author: YGC
* @createTime: 2023/08/20 17:31
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 冒泡排序
*/
public class ArrayExer_Bubbles {
public static void main(String[] args) {
int[] arr = {1, 5, 6, 9, 10, 4, 18, 3, 45, 3};
int temp = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
10.5 冒泡排序(2)(n^2)
package com.ygc.Exercise.exer03;
/**
* @author: YGC
* @createTime: 2023/08/20 17:31
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 冒泡排序
*/
public class ArrayExer_Bubbles {
public static void main(String[] args) {
int[] arr = {1, 5, 6, 9, 10, 4, 18, 3, 45, 3};
int temp = 0;
for (int j = 0; j < arr.length - 1; j++) {
for (int i = 0; i < arr.length - 1 - j; i++) {
if (arr[i] > arr[i + 1]) {
temp = arr[i + 1];
arr[i + 1] = arr[i];
arr[i] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
10.6 快速排序(nlog(n))
二维数组
1. 二维数组的理解
- 二维数组可以看成是一维数组的嵌套了一个其他的数组,以一个新的数组形式存在
- 从运行机制上看,并没有所谓的多维数组
2. 二维数组的使用
- 二维数组的声明和初始化
- 调用数组的指定元素
- 数组的属性:length,(数组的长度)
- 数组的遍历
- 数组的默认初始值
- 二维数组的内存解析
-
//1.静态初始化 int[][] arr1 = new int[][]{{1, 2, 3}, {1, 2, 3}, {1, 2, 3}}; //2.动态初始化 String[][] arr2 = new String[3][4]; double[][] arr3 = new double[3][]; /*省略的写法*/ int[][] arr4 = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}}; /*其他写法*/ int[] arr5[] = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
-
//2.数组元素的调用 System.out.println(arr1[1][2]);//6
-
//3.数组的调用 System.out.println(arr1.length);//3 System.out.println(arr1[0].length);//3
-
//4.遍历数组 //1,2,3,4,5,6,7,8,9 for (int i = 0; i < arr1.length; i++) { for (int j = 0; j < arr1[i].length; j++) { if (j == 0 && i == 0) { System.out.print(arr1[i][j]); } else { System.out.print("," + arr1[i][j]); } } }
-
//5.1二维数组的初始化值(外层是地址,内层与一维数组的初始值一样) //外层元素的初始值 int[][] arr6 = new int[3][2]; System.out.println(arr6[0]);//[I@776ec8df //内层元素的初始值 System.out.println(arr6[0][0]);//0 //5.2 //外层 int[][] arr7 = new int[3][]; System.out.println(arr7[0]);//null //内层(由于内层未开辟一个空间存放数组,所以外层是null,内层报错 ) System.out.println(arr7[0][0]);//java.lang.NullPointerException
3. 二维数组练习
3.1 输出二维数组的和
package com.ygc.Exercise;
/**
* @author: YGC
* @createTime: 2023/08/19 17:02
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 二维数组的练习题
* 输出二维数组的和
*/
public class TwoArrayExer {
public static void main(String[] args) {
int sum = 0;
int[][] arr = {{3, 5, 8}, {12, 9}, {7, 0, 6, 4}};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
sum += arr[i][j];
}
}
System.out.println(sum);
}
}
3.2 二维数组找出最大值
package com.ygc.Exercise;
/**
* @author: YGC
* @createTime: 2023/08/19 17:08
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 二维数组找出最大值
*/
public class TwoArrExer01 {
public static void main(String[] args) {
int[][] arr = {{3, 5, 8}, {12, 9}, {7, 0, 6, 4}};
int max = arr[0][0];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (max < arr[i][j]) {
max = arr[i][j];
}
}
}
System.out.println(max);
}
}
3.2输出员工
package com.ygc.Exercise;
/**
* @author: YGC
* @createTime: 2023/08/19 17:22
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 二维数组练习
* 输出员工
*/
public class TwoArrExer02 {
public static void main(String[] args) {
String[][] employees = {
{"10", "1", "段 誉", "22", "3000"},
{"13", "2", "令狐冲", "32", "18000", "15000", "2000"},
{"11", "3", "任我行", "23", "7000"},
{"11", "4", "张三丰", "24", "7300"},
{"12", "5", "周芷若", "28", "10000", "5000"},
{"11", "6", "赵 敏", "22", "6800"},
{"12", "7", "张无忌", "29", "10800", "5200"},
{"13", "8", "韦小宝", "30", "19800", "15000", "2500"},
{"12", "9", "杨 过", "26", "9800", "5500"},
{"11", "10", "小龙女", "21", "6600"},
{"11", "11", "郭 靖", "25", "7100"},
{"12", "12", "黄 蓉", "27", "9600", "4800"}
};
System.out.println("员工类型\t编号\t姓 名\t\t年龄\t薪资\t\t奖金\t\t股票");
for (int i = 0; i < employees.length; i++) {
for (int j = 0; j < employees[i].length; j++) {
switch (employees[i][0]) {
case "10":
employees[i][0] = "普通员工";
System.out.println(employees[i][0]);
break;
case "11":
employees[i][0] = "程序员";
System.out.println(employees[i][0]);
break;
case "12":
employees[i][0] = "设计师";
System.out.println(employees[i][0]);
break;
case "13":
employees[i][0] = "架构师";
System.out.println(employees[i][0]);
break;
}
System.out.print(employees[i][j] + "\t");
}
System.out.println();
}
}
}
3.4 杨辉三角
package com.ygc.Exercise;
/**
* @author: YGC
* @createTime: 2023/08/19 18:42
* @blogs: https://www.cnblogs.com/ygcDiary
* @description: 杨辉三角
*/
public class ArrayExer_yanghuisanjiao {
public static void main(String[] args) {
/*定义一个二维数组*/
int[][] triangle = new int[10][];
/*动态确定内层元素的个数*/
for (int i = 0; i < triangle.length; i++) {
/*为内层确定元素*/
triangle[i] = new int[i + 1];
/*1.1为内层元素赋值
* 首位和末尾都是1
* */
triangle[i][0] = triangle[i][i] = 1;
/*1.2为中间元素赋值
* 等于肩上两数之和
* */
if (i >= 2) {
for (int j = 1; j < triangle[i].length - 1; j++) {
triangle[i][j] = triangle[i - 1][j] + triangle[i - 1][j - 1];
}
}
}
/*遍历二维数组输出结果*/
for (int i = 0; i < triangle.length; i++) {
for (int j = 0; j < triangle[i].length; j++) {
System.out.print(triangle[i][j]);
}
System.out.println();
}
}
}
4. 注意事项
- 数组的地址记录了数组的类型
- 不能将不同类型的数组相互赋值
- 不满足自动类型提升
- 不同维度的数组也不能相互赋值
数组常用操作的API使用
1.数组的自述
System.out.println(Arrays.toString(arr));
2.数组的比较
//比较两个数组是否相等
boolean isEquals = Arrays.equals(arr, arr2);
System.out.println(isEquals);
3.数组的排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
4.二分查找
//二分查找,(参数:数组,查找的值)
System.out.println(Arrays.binarySearch(arr, 9));//找到了返回下标,未找到返回负数
常见的异常
-
数组下标越界IndexOutOfboundsException
public class TestArrayIndexOutOfBoundsException { public static void main(String[] args) { int[] arr = {1,2,3}; // System.out.println("最后一个元素:" + arr[3]);//错误,下标越界 // System.out.println("最后一个元素:" + arr[arr.length]);//错误,下标越界 System.out.println("最后一个元素:" + arr[arr.length-1]);//对 } }
-
空指针异常(NullPointerException)(数组为空,调用API,实则引用数据类型都满足此条件。)
public class TestNullPointerException {
public static void main(String[] args) {
//定义数组
int[][] arr = new int[3][];
System.out.println(arr[0][0]);//NullPointerException
}
}