JavaSE-04数组,除此之外还有,二维数
分享于 点击 8890 次 点评:175
JavaSE-04数组,除此之外还有,二维数
JavaSE-04数组
- [ 任务列表 ]
- 1.一维数组
- 1.1. 静态初始化一维数组
- 1.2. 动态初始化一维数组
- 1.3. 一维数组的随机访问
- 2.二维数组
- 2.1. 静态初始化二维数组
- 2.2. 动态初始化二维数组
- 2.3. 塔牌类游戏随机的思路
- 3.其他
- 3.1. 初始化时,数组元素的默认值
- 3.2. 数组的快速遍历
- 3.3. 求数组元素的最值的优化
- 4.参考资料
1.一维数组
-
数组:存储批量数据。除此之外还有,二维数组,对象,集合……
-
数组使用的原因:
用变量存储批量数据,代码会非常臃肿;
对于类型相同的大批量数据,使用数组存储,明显优于用多个变量进行存储。
-
数组是一个数据容器,用来存储一批同类型的数据。
数组的访问,为数组某个位置赋值,获取数组的长度(元素个数)
1.1.静态初始化一维数组
-
数组的静态初始化:String [] names = new String[] {};
其中,等式左边的names
数组名和 []
位置可以互换,一般采用上述定义。
// 1>. 定义一个数组,用来存储5个学生的姓名
// 静态初始化一个数组:定义数组的时候,数据已经确定好了
String names [] = new String[] {"张三","李四","王五","赵六","孙七"};
String [] names = {"张三","李四","王五","赵六","孙七"};
1.2.动态初始化一维数组
- 数组的动态初始化:
数据类型 [] 数组名 = new 数据类型 [数组容量];
// 1). 需要一个数组来存储8名同学的成绩
// 动态初始化数组,只确定数组的类型和存储数据的容量
// 数据类型 [] 数组名 = new 数据类型 [数组容量];
double [] scores = new double [8];
// scores = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
// 0 1 2 3 4 5 6 7
Scanner sc = new Scanner(System.in);
// 2). 录入8个同学的java成绩,并把成绩存入数组中
for (int i = 0; i < scores.length; i++) {
System.out.println("请输入第" + (i + 1) + "个同学的java成绩:");
scores[i] = sc.nextDouble();
}
1.3.一维数组的随机访问
- 一维数组的随机访问:
// 2>. 随机获取一个索引值
// Math.random() : [0 - 1)
// names.length : 是数组长度,即元素个数 15
// Math.random() * names.length : [0 - 15) =====> [0, 14]
int index = (int)(Math.random() * names.length);
// 3>. 打印数组中的元素
System.out.println("总共有" + names.length +"个学生,随机抽一个同学是:" + names[index]);
System.out.println(names);
2.二维数组
-
二维数组中每个元素使一个一维数组。
-
二维数组运用:桌面类型游戏的开发——塔牌游戏。
2.1.静态初始化二维数组
- 静态初始化:
String [][] classmates = {{},{},……};
// 数据类型 [] [] 数组名 = new 数据类型 [] [] { { },{ },{ }...}
// 其中 new 数据类型 [] [] 可以省略
String [][] classmates = {
{"张无忌", "赵敏","周芷若"},
{"张三丰", "宋远桥", "殷梨亭"},
{"灭绝师太", "陈坤","玄冥二老","金毛狮王"},
{"杨逍", "纪晓芙"}
};
2.2.动态初始化二维数组
- 动态初始化:
数据类型 [] [] 数组名 = new 数据类型 [长度1] [长度2];
// 数据类型 [ ] [ ] 数组名 = new 数据类型 [长度1] [长度2]
// 动态初始化数组
int [][] arr = new int [3][5];
2.3.塔牌类游戏随机的思路
- 塔牌类游戏随机的思路:(假设数组中有n个数据)
思路一:遍历数组,每次从数组中随机一个位置,然后将当前位置的数和随机位置交换;
思路二:遍历n次,每次随机两个位置出来,将两个位置的数交换。
// 思路一:
for (int i = 0; i < poker.length; i++) {
int index = (int)(Math.random() * 54); // =====> 0 ~ 53
// 每次随机一个位置,将两个位置的数交换
String temp = poker[i];
poker[i] = poker[index];
poker[index] = temp;
}
// 思路二:
for (int i = 0; i < poker.length; i++) {
int index1 = (int)(Math.random() * 54); // =====> 0 ~ 53
int index2 = (int)(Math.random() * 54); // =====> 0 ~ 53
// 每次随机两个位置,将两个位置的数交换
String temp = poker[index1];
poker[index1] = poker[index2];
poker[index2] = temp;
}
3.其他
3.1.初始化时,数组元素的默认值
-
动态初始化时,数组元素的默认值。
数据类型
类型
表示
默认值
基本类型
整型
byte,short,int(默认值),long
0
基本类型
字符型
char
0
基本类型
浮点型
float,double(默认值)
0.0
基本类型
布尔型
boolean
false
引用类型
类,接口,数组……
class,interface,……
null
3.2.数组的快速遍历
- 数组的快速遍历:
Arrays.toString()
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
// 方式一:用Arrays.toString(数组名) 方法
System.out.println(Arrays.toString(numbers));
}
}
3.3.求数组元素最值的优化
- 求最值代码的优化:
找数组比较浪费性能,所以多用变量进行代码运算,少用数组参与运算
// 遍历数组,统计总分,统计最高分,统计最低分,算平均成绩
// 找最值代码的优化:找数组比较浪费性能
public static void FindMaxAndMin(double scores []) {
double grade = scores[0]; // 优化一
double allScore = grade ;
double max = grade ;
double min = grade ;
// 从数组的第二个位置开始遍历
for (int i = 1; i < scores.length; i++) {
// 通过一个变量,把当前遍历到的这个分数保存起来,只需要找一次数组
double score = scores[i]; // 优化二
if (score > max) {
max = score;
}
if (score < min) {
min = score;
}
allScore += score;
}
System.out.println("总分:" + allScore);
System.out.println("平均分:" + allScore / scores.length);
System.out.println("最高分:" + max);
System.out.println("最低分:" + min);
}
4. 参考资料
- 黑马程序员:黑马程序员Java+AI智能辅助编程全套视频教程,java零基础入门到大牛一套通关_哔哩哔哩_bilibili
数组:存储批量数据。除此之外还有,二维数组,对象,集合……
数组使用的原因:
用变量存储批量数据,代码会非常臃肿;
对于类型相同的大批量数据,使用数组存储,明显优于用多个变量进行存储。
数组是一个数据容器,用来存储一批同类型的数据。
数组的访问,为数组某个位置赋值,获取数组的长度(元素个数)
数组的静态初始化:String [] names = new String[] {};
其中,等式左边的names
数组名和 []
位置可以互换,一般采用上述定义。
// 1>. 定义一个数组,用来存储5个学生的姓名
// 静态初始化一个数组:定义数组的时候,数据已经确定好了
String names [] = new String[] {"张三","李四","王五","赵六","孙七"};
String [] names = {"张三","李四","王五","赵六","孙七"};
数据类型 [] 数组名 = new 数据类型 [数组容量];
// 1). 需要一个数组来存储8名同学的成绩
// 动态初始化数组,只确定数组的类型和存储数据的容量
// 数据类型 [] 数组名 = new 数据类型 [数组容量];
double [] scores = new double [8];
// scores = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
// 0 1 2 3 4 5 6 7
Scanner sc = new Scanner(System.in);
// 2). 录入8个同学的java成绩,并把成绩存入数组中
for (int i = 0; i < scores.length; i++) {
System.out.println("请输入第" + (i + 1) + "个同学的java成绩:");
scores[i] = sc.nextDouble();
}
// 2>. 随机获取一个索引值
// Math.random() : [0 - 1)
// names.length : 是数组长度,即元素个数 15
// Math.random() * names.length : [0 - 15) =====> [0, 14]
int index = (int)(Math.random() * names.length);
// 3>. 打印数组中的元素
System.out.println("总共有" + names.length +"个学生,随机抽一个同学是:" + names[index]);
System.out.println(names);
二维数组中每个元素使一个一维数组。
二维数组运用:桌面类型游戏的开发——塔牌游戏。
String [][] classmates = {{},{},……};
// 数据类型 [] [] 数组名 = new 数据类型 [] [] { { },{ },{ }...}
// 其中 new 数据类型 [] [] 可以省略
String [][] classmates = {
{"张无忌", "赵敏","周芷若"},
{"张三丰", "宋远桥", "殷梨亭"},
{"灭绝师太", "陈坤","玄冥二老","金毛狮王"},
{"杨逍", "纪晓芙"}
};
数据类型 [] [] 数组名 = new 数据类型 [长度1] [长度2];
// 数据类型 [ ] [ ] 数组名 = new 数据类型 [长度1] [长度2]
// 动态初始化数组
int [][] arr = new int [3][5];
思路一:遍历数组,每次从数组中随机一个位置,然后将当前位置的数和随机位置交换;
思路二:遍历n次,每次随机两个位置出来,将两个位置的数交换。
// 思路一:
for (int i = 0; i < poker.length; i++) {
int index = (int)(Math.random() * 54); // =====> 0 ~ 53
// 每次随机一个位置,将两个位置的数交换
String temp = poker[i];
poker[i] = poker[index];
poker[index] = temp;
}
// 思路二:
for (int i = 0; i < poker.length; i++) {
int index1 = (int)(Math.random() * 54); // =====> 0 ~ 53
int index2 = (int)(Math.random() * 54); // =====> 0 ~ 53
// 每次随机两个位置,将两个位置的数交换
String temp = poker[index1];
poker[index1] = poker[index2];
poker[index2] = temp;
}
动态初始化时,数组元素的默认值。
数据类型 | 类型 | 表示 | 默认值 |
---|---|---|---|
基本类型 | 整型 | byte,short,int(默认值),long | 0 |
基本类型 | 字符型 | char | 0 |
基本类型 | 浮点型 | float,double(默认值) | 0.0 |
基本类型 | 布尔型 | boolean | false |
引用类型 | 类,接口,数组…… | class,interface,…… | null |
Arrays.toString()
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
// 方式一:用Arrays.toString(数组名) 方法
System.out.println(Arrays.toString(numbers));
}
}
找数组比较浪费性能,所以多用变量进行代码运算,少用数组参与运算
// 遍历数组,统计总分,统计最高分,统计最低分,算平均成绩
// 找最值代码的优化:找数组比较浪费性能
public static void FindMaxAndMin(double scores []) {
double grade = scores[0]; // 优化一
double allScore = grade ;
double max = grade ;
double min = grade ;
// 从数组的第二个位置开始遍历
for (int i = 1; i < scores.length; i++) {
// 通过一个变量,把当前遍历到的这个分数保存起来,只需要找一次数组
double score = scores[i]; // 优化二
if (score > max) {
max = score;
}
if (score < min) {
min = score;
}
allScore += score;
}
System.out.println("总分:" + allScore);
System.out.println("平均分:" + allScore / scores.length);
System.out.println("最高分:" + max);
System.out.println("最低分:" + min);
}
用户点评