数组
什么是数组?
数组(array)是代表一组数据。
如果有一组相同数据类型的数据,可以考虑使用统一的名称对他们进行管理,每一个数据又通过编号(或索引、下标、脚标(index))的方式来区分它们,每一个数据又称为元素(element)。这组数的总个数,称为数组的长度(length)。通过这种方式简化一组数的存储和管理。
数组的好处:便于管理一组相同数据类型的数
public class TestNoArray {
public static void main(String[] args) {
//存储一个小组的成绩,例如一个小组有5个人。
int s1 = 89;
int s2 = 96;
int s3 = 75;
int s4 = 88;
int s5 = 79;
//存储可以
//管理有点麻烦
/*
例如:把这组学员的成绩,进行排序。
找最大值,求平均分等
再设想一下,如果不是5个人,是50个人呢?
*/
/* for(int i=0; i<5; i++) {
System.out.println("第1个同学的成绩:" + s[i]);
}*///错误
System.out.println("第1个同学的成绩:" + s1);
System.out.println("第2个同学的成绩:" + s2);
System.out.println("第3个同学的成绩:" + s3);
System.out.println("第4个同学的成绩:" + s4);
System.out.println("第5个同学的成绩:" + s5);
}
}
public class TestUseArray {
public static void main(String[] args) {
int[] scores = {89, 96, 75, 88, 79};
for (int i = 0; i < scores.length; i++) {
System.out.println("第" + (i+1) +"个同学的成绩:" + scores[i]);
}
}
}
数组的声明和使用
数组:一维数组(今天),二维数组等
- 一维数组:代表一组数
- 二维数组:代表多组数,类似于二维表
数组的声明
元素的数据类型[] 数组名; //推荐
元素的数据类型 数组名[];
例如:
//存储一组商品的价格
double[] price;
//存储一组字母
char[] letters;
//存储一组姓名
String[] names;
//存储一组身高:
int[] height;
数组的初始化
(1)静态初始化
在声明完数组之后,直接在{}中列出数组的元素值。
元素的数据类型[] 数组名 = {元素值1, 元素值2, ...};
例如:
int[] scores = {89, 96, 75, 88, 79};
元素的数据类型[] 数组名; //声明
数组名 = new 元素的数据类型[] {元素值1, 元素值2, ...}; //静态初始化
int[] grades;
grades = new int[]{89, 96, 75, 88, 79};//静态初始化
(2)动态初始化
在声明数组时,只确定数组的长度,在运行时再一一确定数组的元素值。
元素的数据类型[] 数组名 = new 元素的数据类型[长度];
数组名[index] = 元素的值;
int[] num = new int[5];//数组的长度为5
num[0] = 89;
num[1] = 96;
num[2] = 75;
num[3] = 88;
num[4] = 79;
public class TestArrayInitialize {
public static void main(String[] args) {
int[] scores = {89, 96, 75, 88, 79};//静态初始化
int[] grades;
grades = new int[]{89, 96, 75, 88, 79};//静态初始化
System.out.println("scores数组的长度:" + scores.length);//5
System.out.println("grades数组的长度:" + grades.length);//5
System.out.println("--------------------");
int[] num = new int[5];//数组的长度为5
num[0] = 89;
num[1] = 96;
num[2] = 75;
num[3] = 88;
num[4] = 79;
System.out.println("num数组的长度:" + num.length);//5
}
}
数组的遍历
遍历:挨个查看元素的意思。
理论上,for,while,do_while都可以用来遍历数组,但是习惯上用for最多。
for(int i=0; i<数组名.length; i++){
}
IDEA遍历数组的快捷键:数组名.fori, 或 itar
注意:
- 数组的长度:数组名.length
- 数组的下标范围:[0, 数组名.length-1]
public class TestArrayIterate {
public static void main(String[] args) {
int[] scores = {89, 96, 75, 88, 79};
System.out.println(scores);//[I@776ec8df 首地址
for (int i = 0; i < scores.length; i++) {
System.out.println(scores[i]);
}
//System.out.println(scores[5]);
/*
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
数组的下标越界异常。下标5超过边界,长度5. 下标范围[0, 4]
*/
}
}
数组的常见异常
1、下标越界异常:ArrayOutOfBoundsException
当下标值不在[0, 数组名.length-1]范围,就会发生这个异常。
数组的内存分析
1、数组的特点
-
数组的长度一旦确定,就不能更改了,除非你创建新的数组
-
数组的元素是“连续”存储的,因此数组的访问效率是非常高
- 比喻:大家一起住酒店,如果房间是挨着的,找人更快。
-
数组的长度确定了,元素如果没有指定值,会有默认值
- byte,short,int,long:0
- float,double:0.0
- char:\u0000的字符,空字符
- boolean:false
- String等引用数据类型:null
- 数组名中存储了数组的首地址。
2、数组的内存
public class TestArrayMemory {
public static void main(String[] args) {
int x = 1;
int y = 2;
int[] arr = new int[5];
arr[0] = 56;
arr[1] = 45;
arr[4] = 4;
System.out.println(arr);//[I@776ec8df
System.out.println(arr[0]);//56
System.out.println(arr[3]);//0
}
}
JVM的运行时内存:(实际中包含元空间、堆、栈等)(今天先只讨论堆和栈)
- 栈:存储方法的局部变量值的。例如:main方法(主方法)的局部变量,x, y, arr等。
- 堆:存储new出来的对象。
new:像JVM在堆中申请一整块内存空间,用来存储对象的元素或属性等信息。
JVM会对内存每一个字节编一个内存地址。例如:0x2456.
[0]);//56
System.out.println(arr[3]);//0
}
}
JVM的运行时内存:(实际中包含元空间、堆、栈等)(今天先只讨论堆和栈)
- 栈:存储方法的局部变量值的。例如:main方法(主方法)的局部变量,x, y, arr等。
- 堆:存储new出来的对象。
new:像JVM在堆中申请一整块内存空间,用来存储对象的元素或属性等信息。
JVM会对内存每一个字节编一个内存地址。例如:0x2456.
![在这里插入图片描述](https://img-blog.csdnimg.cn/e043032b0d6349dc8791a0b052571800.png#pic_center)