1.数组的概述
一、数组的理解
数组,可以看做是多个相同数据类型的变量按照一定顺序排列的组合(或集合)
二、变量的分类:基本数据类型(8种) vs 引用数据类型(类、接口、数组)
变量的声明格式:数据类型 变量名 = 变量值
三、数组相关的概念
- 数组名
- 数组元素
- 数组的索引(角标、下角标)
- 数组的长度:数组中元素的个数
四、说明
- 不管是静态初始化,还是动态初始化,只要数组初始化完成,其长度就是确定的。
- 数组一旦初始化完成,其长度就不可变。
- 数组属于引用数据类型;
数组的元素既可以是基本数据类型,也可以是引用数据类型。比如:int[] \ boolean[] \ char[] \ double[] \ String[]
2.一维数组
2.1 数组的声明与定义
//数据类型 变量名 = 变量值
double price1 = 34.32;
double price2 = 23.32;
//1. 定义数组
//1.1 数组的声明
double[] prices;
//1.2 数组的初始化
//静态初始化方式:数组变量的赋值与数组元素的赋值同时进行。
prices = new double[]{34.32,23.32,12.35};
int[] nums = new int[]{1,2,3,4,5};
//动态初始化方式:数组变量的赋值与数组元素的赋值分开进行。
String[] names = new String[5];
//错误的定义方式
// int[] ids1 = new int[3]{3,4,5};
// int[3] ids2 = new int[]{3,4,5};
// int[3] ids3 = new int[3];
//也是正确的方式
int[] arr1 = {1,2,3}; //类型推断
int arr2[] = new int[]{1,2,3};
2.2 数组元素的调用
//2. 数组元素的调用:使用角标的方式进行调用
//数组元素的角标是从0开始的,到数组的长度-1结束。
System.out.println(prices[0]);
System.out.println(nums[3]);
names[0] = "张飞";
names[1] = "关羽";
names[4] = "曹操";
// names[5] = "诸葛亮"; 运行报错
2.3 数组的属性:长度
//3. 数组的长度:length
System.out.println(names.length);//5
System.out.println(nums.length);//5
2.4 数组的遍历
for(int i = 0;i < nums.length;i++){//for(int i = 0;i < 5;i++){
System.out.println(nums[i]);
}
2.5 数组元素的默认值
测试数组元素的默认值
- 如果数组的元素是整型,则数组元素的默认值为:0
- 如果数组的元素是浮点类型,则数组元素的默认值为:0.0
- 如果数组的元素是boolean类型,则数组元素的默认值为:false
- 如果数组的元素是char类型,则数组元素的默认值为:0 或 \u0000
- 如果数组的元素是引用数据类型,则数组元素的默认值为:null
- - 说明:数组元素的类型可以是基本数据类型,也可以是引用数据类型。
int[] arr1 = new int[5];
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);
}
System.out.println();
//byte[] \ short[] \ long[]
long[] arr2 = new long[3];
System.out.println(arr2[0]);
System.out.println();
double[] arr3 = new double[10];
System.out.println(arr3[0]);
System.out.println();
boolean[] arr4 = new boolean[10];
System.out.println(arr4[0]);
System.out.println();
char[] arr5 = new char[5];
for(int i = 0;i < arr5.length;i++){
System.out.println("****" + arr5[i] + "####");
}
System.out.println(arr5[0] == ' ');//false
System.out.println(arr5[0] == '0');//false
System.out.println(arr5[0] == 0);//true
System.out.println();
String[] arr6 = new String[5];
for(int i = 0;i < arr6.length;i++){
System.out.println(arr6[i]);
}
System.out.println(arr6[0] == null);//true
System.out.println(arr6[0] == "null");//false
2.6 数组的内存解析
-
内存的主要部分,及储存的结构
-
内存解析举例
-
练习1
升景坊单间短期出租4个月,550元/月(水电煤公摊,网费35元/月),空调、卫生间、厨房齐全。屋内均是IT行业人士,喜欢安静。所以要求来租者最好是同行或者刚毕业的年轻人,爱干净、安静,
public class ArrayTest1 {
public static void main(String[] args) {
int[] arr = new int[]{8, 2, 1, 0, 3};
int[] index = new int[]{2, 0, 3, 2, 4, 0, 1, 3, 2, 3, 3};
String tel = "";
for (int i = 0; i < index.length; i++) {
tel += arr[index[i]];
}
System.out.println("联系方式:" + tel);//18013820100
}
}
/**
* 从键盘读入学生成绩,找出最高分,并输出学生成绩等级。
* 成绩>=最高分-10 等级为’A’
* 成绩>=最高分-20 等级为’B’
* 成绩>=最高分-30 等级为’C’
* 其余 等级为’D’
* 提示:先读入学生人数,根据人数创建int数组,存放学生成绩。
*/
public class ArrayTest2 {
public static void main(String[] args) {
//1. Scanner的实例化
Scanner scann = new Scanner(System.in);
//2. 根据提示,获取学生的人数
System.out.print("请输入学生的人数:");
int number = scann.nextInt();
//3. 根据长度,创建指定类型的数组:int[]
int[] scores = new int[number];
//4. 遍历数组,给数组的每一个元素赋值:arr[i] = scanner.nextInt()
System.out.println("请输入" + number + "个成绩:");
int maxScore = 0;//记录最高分
for(int i = 0;i < scores.length;i++){
scores[i] = scann.nextInt();
//5. 获取最高分
if(maxScore < scores[i]){
maxScore = scores[i];
}
}
System.out.println("最高分为:" + maxScore);
//6. 遍历数组,获取每个学生成绩,以及与最高分的差值,并得出等级。
char grade;
for(int i = 0;i < scores.length;i++){
if(scores[i] >= maxScore - 10){
grade = 'A';
}else if(scores[i] >= maxScore - 20){
grade = 'B';
}else if(scores[i] >= maxScore - 30){
grade = 'C';
}else{
grade = 'D';
}
System.out.println("student " + i + " score is " + scores[i] + " grade is " + grade);
}
}
}
3. 二维数组的使用
数组属于引用数据类型;
数组的元素既可以是基本数据类型,也可以是引用数据类型。如果数组的元素还是数组的话,则构成了多维数组
3.1 数组的声明与定义
//理解
String[] star1 = new String[]{"陈伟霆","13121341234"};
String[] star2 = new String[]{"刘诗诗","13221341234"};
//一维数组star1,star2又作为了另一个一维数组stars的元素,则构成二维数组
String[][] stars = new String[][]{star1,star2};//二维数组
//1. 数组的声明与定义
int[][] arr1;
arr1 = new int[][]{{1,2,3},{5,6,7},{7,8}};//静态初始化
//动态初始化方式1
int[][] arr2 = new int[4][3];
//动态初始化方式2
int[][] arr3 = new int[5][];
//错误的写法
// int[][] arr4 = new int[4][3]{{1,2,3},{5,6,7},{7,8}};
// int[][] arr5 = new int[][3];
//正确的其他写法
int arr6[][] = new int[4][3];
int[] arr7[] = new int[4][];
int[][] arr8 = {{1,2,3},{5,6,7},{7,8}};//类型推断
3.2 数组元素的调用
//2. 数组元素的调用
// 通过角标的方式进行调用
System.out.println(arr1[0][0]);
System.out.println(arr1[2][0]);
arr6[1][2] = 12;
3.3 数组的属性:长度
//3. 数组的属性:长度
System.out.println(arr1.length);//3
System.out.println(arr1[2].length);//2
3.4 数组的遍历
for(int i = 0;i < arr1.length;i++){
for(int j = 0;j < arr1[i].length;j++){
System.out.print(arr1[i][j] +"\t");
}
System.out.println();
}
3.5 数组元素的默认值
* 测试二维数组元素的默认值
* 约定:arr1的元素称为外层元素,arr1[0]的元素称为内层元素
* * 针对格式1:int[][] arr1 = new int[4][3];
* 外层元素的值为:数组的类型和对应的指向一维数组的地址
* 内层元素的值为:与一维数组的默认值相同
* 如果数组的元素是整型,则数组元素的默认值为:0
* 如果数组的元素是浮点类型,则数组元素的默认值为:0.0
* 如果数组的元素是boolean类型,则数组元素的默认值为:false
* 如果数组的元素是char类型,则数组元素的默认值为:0 或 \u0000
* 如果数组的元素是引用数据类型,则数组元素的默认值为:null
* * 针对格式2:int[][] arr2 = new int[4][];
* 外层元素的值为:null
* 内层元素的值为:报空指针异常的错误。
public static void main(String[] args) {
int[][] arr1 = new int[4][3];
System.out.println(arr1[0]);//[I@1540e19d
System.out.println(arr1[1]);//[I@677327b6
System.out.println(arr1[2]);//[I@14ae5a5
System.out.println(arr1[3]);//[I@7f31245a
double[][] arr2 = new double[3][2];
System.out.println(arr2[0]);//[D@6d6f6e28
String[][] arr3 = new String[3][4];
System.out.println(arr3[0]);//[Ljava.lang.String;@135fbaa4
System.out.println(arr1[0][0]);//0
System.out.println(arr2[0][0]);//0.0
System.out.println(arr3[0][0]);//null
System.out.println("############针对格式2##############");
int[][] arr4 = new int[4][];
System.out.println(arr4[0]);//null
System.out.println(arr4[1]);//null
System.out.println(arr4[2]);//null
System.out.println(arr4[3]);//null
double[][] arr5 = new double[3][];
System.out.println(arr5[0]);//null
//System.out.println(arr4[0][0]);//报错
//最后一行代码报错,但是在开发中,我们确实有可能要遍历或操作二维数组的内层元素。
//当报错的话,表明没有给外层元素进行赋值。则赋值之后即可。
arr4[0] = new int[4];
arr4[1] = new int[]{1,2,4};
System.out.println(arr4[0][0]);//0
}
3.6 数组的内存解析
4. 练习题
4.1 练习一(for循环计算二维数组之和)
public class ArrayTest3 {
public static void main(String[] args) {
int[][] arr = new int[][]{{3,5,8},{12,9},{7,0,6,4}};
int sum = 0;//记录数组元素的和
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);
}
}
4.2 练习二 (杨辉三角)
-
使用二维数组打印一个 10 行杨辉三角.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
…
-
开发提示
- 第一行有 1 个元素, 第 n 行有 n 个元素
- 每一行的第一个元素和最后一个元素都是 1
- 从第三行开始, 对于非第一个元素和最后一个元素的元素.
yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j];
public class Test15 {
public static void main(String[] args){
//(1)先确定行数
int[][] yanghui = new int[10][];
//(2)再确定每一行的列数
//第一行有 1 个元素, 第 n 行有 n 个元素
for(int i=0; i<yanghui.length; i++){
yanghui[i] = new int[i+1];
}
//(3)再确定元素
for(int i=0; i<yanghui.length; i++){
//每一行的第一个和最后一个元素都是1
yanghui[i][0] = 1;
yanghui[i][i] = 1;
//中间的元素
for(int j=1; j<yanghui[i].length-1; j++){
yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j];
}
}
//(4)打印显示
for(int i=0; i<yanghui.length; i++){
for(int j=0; j<yanghui[i].length; j++){
System.out.print(yanghui[i][j] + "\t");
}
System.out.println();
}
}
}
或者
public class YangHuiTest {
public static void main(String[] args) {
// 1.初始化二维数组
int[][] yangHui = new int[10][];
for (int i = 0; i < yangHui.length; i++) {
yangHui[i] = new int[i + 1];
// 2.给数组的元素赋值
// 2.1 给每行的首元素和尾元素赋值
yangHui[i][0] = yangHui[i][i] = 1;
// 2.2 给每行非首元素和非尾元素赋值
//if(i > 1){
for (int j = 1; j < yangHui[i].length - 1; j++) {
yangHui[i][j] = yangHui[i - 1][j] + yangHui[i - 1][j - 1];
}
//}
}
// 3.遍历输出二维数组的元素
for (int i = 0; i < yangHui.length; i++) {
for (int j = 0; j < yangHui[i].length; j++) {
System.out.print(yangHui[i][j] + "\t");
}
System.out.println();
}
}
}