数组的概述
数组
数组是一个容器。
作用:可以放置一批特定类型的数据。
数组的每个数据称为数组的一个元素。数据==元素
开发中经常需要存储,分析,处理一批类型的数据。
例如:一个班级全部学生的成绩,如果使用变量存储,那么就需要定义400多个变量。
这个时候采用一个数组容器存储更加方便,合适,所以开发中数组用的很多!
数组的定义格式
a.静态初始化数组:
格式1:
数据类型[] 数组名称 = new 数据类型[]{元素1,元素2,元素3,…};
数组的名称:
首字母建议小写,“小驼峰模式”,要有意义。不能是关键字,必须是合法标识符。
静态初始化是在数组定义出来以后,元素就也应该确定出来!
什么类型的数组只能存放什么类型的数据,否则报错!
public class ArrayDemo {
public static void main(String[] args) {
// 格式1.定义一个数组存储5个学生的年龄。
// 数据类型[] 数组名称 = new 数据类型[]{元素1,元素2,元素3,......};
int[] ages = new int[]{21 ,23 ,21 ,25 ,40 };
//int ages[] = new int[]{21 ,23 ,21 ,25 ,40}; // 不建议这样书写
// 格式2.
// 数据类型[] 数组名称 = {元素1,元素2,元素3,......}
int[] ages1 = {21 ,23 ,21 ,25 ,40};
}
}
b.动态初始化数组:
数据类型[] 数组名称 = new 数据类型[数组的长度];
1.在定义的时候并不会给数组申明元素值。
2.但是要在定义的时候申明数组的元素个数,我们称为数组的长度!
小结:
定义的时候只申明数组的长度和类型,不写具体的元素值,具体元素值后续确定!
拓展:
数组类型定义的其他写法!
数据类型[] 数组名称 = …
数据类型 数组名称[] = …
public class ArrayDemo02 {
public static void main(String[] args) {
// 需求:定义一个数组存储400个学生的成绩!
// 数据类型[] 数组名称 = new 数据类型[数组的长度];
double[] scores = new double[400];
//double scores[] = new double[400]; // 不建议这样写!
// 需求:定义一个数组存储4个学生的年龄
int[] ages = new int[4];
//int ages[] = new int[4]; // 不建议这样写!
// 注意:静态初始化和动态初始化只能选择其中一种定义!
//int[] ages1 = new int[4]{21 , 23 , 21 , 32}; // 错误的!
int[] ages1 = new int[]{21 , 23 , 21 , 32}; // 静态初始化
}
}
数组的访问
索引:
数组是存在元素编号的,这个编号是从0开始的,这个编号称为索引。
访问数组的元素格式:
取值:数组名称[元素索引];
赋值:数组名称[元素索引] = 元素值;
访问数组的长度:
格式:数组名称.length.
动态初始化数组的默认值
引入:
数组的动态初始化虽然在定义的时候只是申明了数组的长度,没有申明具体的元素值,
但是里面其实是存在元素默认值得。
默认值的规则:
1、所有的数值类型(byte , short ,int ,long ,char)的默认是0或者(float ,double)0.0,整形是0 ,浮点型是0.0
2.布尔类型的默认值是false
3.char类型的默认值的编号是0 (char是字符,字符本身就是整数值)
4.引用类型的默认值是null 了解
小结:
数组的动态初始化虽然在定义的时候只是申明了数组的长度,没有申明具体的元素值,
但是里面其实是存在元素默认值得。具体默认值规则大家了解即可!
public class ArrayDemo01 {
public static void main(String[] args) {
// 1.定义一个数组存储4个学生的年龄
int[] ages = new int[4];
System.out.println(ages[0]); // 0
System.out.println(ages[3]); // 0
System.out.println("---------------------");
double[] scores = new double[5];
System.out.println(scores[0]); // 0.0
System.out.println(scores[4]); // 0.0
System.out.println("---------------------");
boolean[] flags = new boolean[4];
System.out.println(flags[0]); // false
System.out.println(flags[3]); // false
System.out.println("---------------------了解");
char[] chars = new char[4]; // [0 , 0 , 0, 0]
System.out.println((int)chars[0]);
System.out.println((int)chars[3]);
System.out.println("---------------------了解");
String[] names = new String[8]; // names [null , null , null , null , null , null , null , null]
System.out.println(names[0]); // null
System.out.println(names[7]); // null
}
}
数组的遍历
public class ArrayDemo01 {
public static void main(String[] args) {
// a.定义一个数组,存储一个班级4个学生的成绩
double[] scores = {85.0 , 89.5 , 100 , 59.5};
// 0 1 2 3
// b.原始遍历
// System.out.println(scores[0]);
// System.out.println(scores[1]);
// System.out.println(scores[2]);
// System.out.println(scores[3]);
// c.使用for循环改进
for(int i = 0 ; i < scores.length ; i++ ){
// i = 0 1 2 3
System.out.println(scores[i]);
}
}
}
数组的遍历求最值
1.定义一个整型数组。
2.定义一个变量存储最终的最大值,变量的初始值必须使用数组中的某个元素作为参照,建议用数组第一个元素!
3.遍历数组。
4.拿着当前遍历的元素值依次与最大值变量的值进行比较,如果比最大值变量的值大,替换最大值变量的值为当前值!
public class ArrayExecDemo02 {
public static void main(String[] args) {
// 1.定义一个整型数组。
int[] faceScores = {5 , 15 , 200 , 10000 , 100 , 400};
// 0 1 2 3 4 5
// 2.定义一个变量存储最终的最大值,变量的初始值必须使用数组中的某个元素作为参照,建议用数组第一个元素!
int max = faceScores[0];
// 3.遍历数组中全部元素。 注意:循环建议从第二个,也就是索引为1开始比较,因为第一个元素已经作为参照!
for(int i = 1 ; i < faceScores.length ; i++ ){
// i = 1 2 3 4 5
// 拿到当前遍历的值
int num = faceScores[i];
// 4.拿着当前遍历的值依次与最大值变量的值进行比较,如果比最大值变量的值大,替换最大值变量的值为当前值!
if(num > max){
max = num ; //赋值: 当前值替换最大值变量!
}
}
System.out.println("最大值:"+max);
}
}