文章目录
数组
1、引入数组
1.1、数组定义
数组:可以看成是相同类型元素的一个集合。在内存中是一段连续的空间。
- 数组中存放的元素其类型相同
- 数组的空间是连在一起的
- 每个空间有自己的编号,其实位置的编号为0,即数组的下标。
1.2、创建数组
int:表示数组中存放元素的类型
int[ ]:表示数组的类型
10:表示数组的长度
array :表示数组名
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7,8,9};
int[] array2 = new int[]{1,2,3,4,5,6,7,8,9,10};
int[] array3 = new int[10];//没有初始化,默认值为0
array3[0] = 10;
System.out.println(array.length);//获取数组名.length来自动获取 数组长度
}
调试结果: 可以看出来 没有初始化的数组我们可以自己添加元素
1.3、数组的初始化
数组的初始化主要分为动态初始化以及静态初始化。
- 动态初始化:在创建数组时,直接指定数组中元素的个数
int[] array3 = new int[10];//没有初始化,默认值为0
- 静态初始化:在创建数组时不直接指定数据元素个数,而直接将具体的数据内容进行指定
int[] array = {1,2,3,4,5,6,7,8,9};
int[] array2 = new int[]{1,2,3,4,5,6,7,8,9,10};
String[] array4 = {"ar wei"," zhenzhen"};
【注意事项】
1、静态初始化虽然没有指定数组的长度,编译器在编译时会根据{}中元素个数来确定数组的长度。 2、静态初始化时,
{}中数据类型必须与[]前数据类型一致。 3、静态初始化可以简写,省去后面的new T[]。
4、如果不确定数组当中内容时,使用动态初始化,否则建议使用静态态初始化。 5、静态和动态初始化也可以分为两步,但是省略格式不可以。
6、如果没有对数组进行初始化,数组中元素有其默认值 7、如果数组中存储元素类型为基类类型,默认值为基类类型对应的默认值
8、如果数组中存储元素类型为引用类型,默认值为null
1.4、数组的使用
1.41、数组元素访问
数组在内存中是一段连续的空间,空间的编号都是从0开始的,依次递增,该编号称为数组的下标,数组可以通过下标访问其任意位置的元素。
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] array2 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
String[] array4 = {"ar wei", " zhenzhen"};
int[] array3 = new int[10];//没有初始化,默认值为0
array3[0] = 10;
System.out.println(array.length);//获取数组名.length来自动获取 数组长度
System.out.print(array[0]+" ");
System.out.print(array[1]+" ");
System.out.print(array[2]+" ");
System.out.print(array[3]+" ");
System.out.print(array[4]+" ");
System.out.print(array[5]+" ");
运行代码:
9
1 2 3 4 5 6
Process finished with exit code 0
}
【注意事项】
- 数组是一段连续的内存空间,因此支持随机访问,即通过下标访问快速访问数组中任意位置的元素
- 下标从0开始,介于[0, N)之间不包含N,N为元素个数,不能越界,否则会报出下标越界异常。
举例子:
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println(array[10]);//执行会抛出异常
}
运行结果:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at Test.main(Test.java:4)
1.42、遍历数组
1、普通遍历数组
在数组中可以通过 数组对象.length 来获取数组的长度
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+" ");
}
运行结果;
1 2 3 4 5 6 7 8 9
Process finished with exit code 0
2、使用 for-each 遍历数组
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
for (int x: array) {
System.out.print(x +" ");
}
运行结果;
1 2 3 4 5 6 7 8 9
Process finished with exit code 0
}
for-each 是 for 循环的另外一种使用方式. 能够更方便的完成对数组的遍历. 可以避免循环条件和更新语句写错.
3、借助java本身提供的方法来完成数组的打印
Arrars : 工具类 可以帮忙操作数组
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
String ret = Arrays.toString(array);
System.out.println(ret);
//System.out.println(Arrays.toString(array)); //把参数的数组转化为 字符串作为输出
}
运行代码:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Process finished with exit code 0
最后一行代码效果一样
2、数组的引用
2.1、基本变量与引用变量
基本数据类型创建的变量,称为基本变量,该变量空间中直接存放的是其所对应的值;
而引用数据类型创建的变量,一般称为对象的引用,其空间中存储的是对象所在空间的地址。
public static void func() {
int a = 10;
int[] arr = new int[]{1,2,3};
}
如图:
引用变量并不直接存储对象本身,可以简单理解成存储的是对象在堆中空间的起始地址。通过该地址,引用变量便可以去操作对象
public static void main(String[] args) {
int[] array = {1,2,3};
System.out.println(array);
System.out.println(Arrays.toString(array));
int a = 10;
System.out.println(a);
}
运行结果:
[I@1b6d3586
[1, 2, 3]
10
Process finished with exit code 0
其中@表示真实地址的哈希值
array—>引用变量 存的是地址
给引用类型不能赋值0 对应赋值null 空
代表array这个对象 不指向任何对象
public static void main(String[] args) {
int a = 0;
System.out.println(a);
int[] array = null;
System.out.println(array);
运行代码:
0
null
Process finished with exit code 0
}
如果运行下面代码:
public static void main(String[] args) {
int a = 0;
System.out.println(a);
int[] array = null;
System.out.println(array.length);
}
运行结果:
0
Exception in thread "main" java.lang.NullPointerException
at class202205.demo20.demo20.demo20220520.main(demo20220520.java:12)
Process finished with exit code 1
因为array不指向任何对象 运行数组长度 出现空指针异常 NullPointerException
2.2、引用指向
一个引用array2 指向另一个引用 所指向的对象
public static void main(String[] args) {
int[] array1 = {1,2,3,4,5,6};
int[] array2 = array1;
System.out.println("array1:"+Arrays.toString(array1));
System.out.println("array2:"+Arrays.toString(array2));
System.out.println("=============");
array2[0] = 99;
System.out.println("array1:"+Arrays.toString(array1));
System.out.println("array2:"+Arrays.toString(array2));
}
运行代码:
array1:[1, 2, 3, 4, 5, 6]
array2:[1, 2, 3, 4, 5, 6]
=============
array1:[99, 2, 3, 4, 5, 6]
array2:[99, 2, 3, 4, 5, 6]
Process finished with exit code 0
插图:
还有经典反例:掩耳盗铃
public static void main(String[] args) {
int[] array1 = {1,2,3,4,5,6};
int[] array2 = {1,2,3,4,5,6};
System.out.println(array1);
System.out.println(array2);
}
运行结果:
[I@1b6d3586
[I@4554617c
Process finished with exit code 0
插图:
2.3、 回收机制
当array1引用array2所指向的对象时
array1的对象没有被引用了 就会被回收
代码验证:
public static void main(String[] args) {
int[] array1 = {1,2,3,4,5,6};
int[] array2 = {1,2,3,4,5,6};
System.out.println(array1);
System.out.println(array2);
System.out.println("=========");
array1 = array2;
System.out.println(array1);
System.out.println(array2);
}
代码运行:
[I@1b6d3586
[I@4554617c
=========
[I@4554617c
[I@4554617c
Process finished with exit code 0
到这里 提出几个疑惑 xdm 也可以思考一下
1.引用能指向引用吗?
不能 array1 = array2 代表 array1引用array2所指向的对象
引用只能指向对象.
2.一个引用 能同时指向多个对象吗
public static void main(String[] args) {
int[] array1 = {1,2,3};
System.out.println(array1);
System.out.println("array1:"+Arrays.toString(array1));
System.out.println("=======");
array1 = new int[]{99,66,55};
System.out.println(array1);
System.out.println("array1:"+Arrays.toString(array1));
System.out.println("===========");
array1 = new int[]{65,53,5451,5,6};
System.out.println(array1);
System.out.println("array1:"+Arrays.toString(array1));
}
运行代码:
[I@1b6d3586
array1:[1, 2, 3]
=======
[I@4554617c
array1:[99, 66, 55]
===========
[I@74a14482
array1:[65, 53, 5451, 5, 6]
Process finished with exit code 0
3.引用一定在栈上吗
不一定 局部变量在 成员变量 就不一定了
4.引用赋值null代表啥
int[] array = null; 代表这个引用不指向任何对象
另外:
堆里面的叫对象
引用存的是地址
3、数组应用
3.1、函数的参数
- 参数传基本数据类型
public static void func1(int a){
a = 20;
}
public static void main(String[] args) {
int x = 10;
func1(x);
System.out.println(x);
int[] array1 = {1,2,3,4,5,6,7,8};
}
运行代码:
10
Process finished with exit code 0
- 参数传数组类型(引用数据类型)
public static void func2(int[] array) {
array = new int[]{11,12,13,14};
}
public static void func3(int[] array) {
array[0] = 99;
}
public static void main(String[] args) {
int[] array1 ={1,2,3,4};
func2(array1);
System.out.println(Arrays.toString(array1));
int[] arr = {100,99,88,77};
func3(arr);
System.out.println(Arrays.toString(arr));
}
运行代码:
[1, 2, 3, 4]
[99, 99, 88, 77]
Process finished with exit code 0
3.2、返回值
public static int[] fun(){
int[] array = {1,2,3,4,5};
return array;
}
public static void main(String[] args) {
int[] ret = fun();
System.out.println(Arrays.toString(ret));
}
运行代码:
[1, 2, 3, 4, 5]
Process finished with exit code 0
4、数组练习
4.1、数组转字符串
public static String myToString(int[] array){
String str = "[ ";
for (int i = 0; i < array.length; i++) {
str +=array[i];
if (i !=array.length-1){
str +=",";
}
}
str +=" ]";
return str;
}
运行代码:
[ 1,2,3,4 ]
Process finished with exit code 0
4.2、数组的拷贝
4.21、遍历数组拷贝
使用for循环拷贝
int[] array = {1,2,3,4};
int[] copy = new int[array.length];
for (int i = 0; i < array.length; i++) {
copy[i] = array[i];
}
System.out.println(Arrays.toString(copy));
}
运行代码:
[1, 2, 3, 4]
Process finished with exit code 0
4.22、Array.copy拷贝
public static void main(String[] args) {
int[] array = {1,2,3,4};
//虽然发生了拷贝,但是可以看作扩容
int[] copy = Arrays.copyOf(array,array.length);
System.out.println(Arrays.toString(copy));
}
运行代码:
[1, 2, 3, 4]
Process finished with exit code 0
扩容情况:
public static void main(String[] args) {
int[] array = {1,2,3,4};
//虽然发生了拷贝,但是可以看作扩容
int[] copy = Arrays.copyOf(array,2*array.length);
System.out.println(Arrays.toString(copy));
}
运行代码:
[1, 2, 3, 4, 0, 0, 0, 0]
Process finished with exit code 0
4.23、System.arraycopy拷贝
public static void main(String[] args) {
int[] array = {1,2,3,4};
int[] copy = new int[array.length];
//你要拷贝的数组,从0下标开始,拷贝到copy,从0下标开始,拷贝到array.length
System.arraycopy(array,0,copy,0,array.length);
System.out.println(Arrays.toString(copy));
}
运行结果:
[1, 2, 3, 4]
Process finished with exit code 0
4.24、通过数组名克隆
数组名.克隆方法.
public static void main(String[] args) {
int[] array = {1,2,3,4};
int[] copy = array.clone();
System.out.println(Arrays.toString(copy));
}
运行代码:
[1, 2, 3, 4]
Process finished with exit code 0
4.3、求数组中元素的平均值
给定一个数组,求数组中元素平均值.
public static void main(String[] args) {
int[] arr = {15,85,65,45,25,32};
System.out.println(avg(arr));
}
public static double avg(int[] arr) {
int sum = 0;
for (int x : arr) {
sum += x;
}
return (double) sum / (double) arr.length;
}
运行代码:
44.5
Process finished with exit code 0