Java数组
1数组的概念
数组(Array),是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并通过编号的方式对这些数据进行统一管理。
数组的相关概念:
数组名元素角标、下标、索引数组的长度:元素的个数
数组的特点:
数组是有序排列的数组属于引用数据类型的变量;而数组内的元素既可以是基本数据类型,也可以是引用数据类型创建数组对象会在内存中开辟一整块连续的空间数组的长度一旦确定,就不能修改了
数组的分类:
按照维数:一维数组、二维数组按照数组元素的类型:基本数据类型元素的数组、引用数据类型的数组
2 一维数组
①一维数组的声明和初始化
int[] ids
;
ids
= new int[]{1001,1002,1003,1004};
int[] arr1
= new int[]{1001, 1002, 1003};
String
[] names
= new String[5];
names
[0] = "王铭";
names
[1] = "王赫";
names
[2] = "张学良";
names
[3] = "孙居龙";
names
[4] = "王宏志";
②数组的属性:length
System
.out
.println(names
.length
);
System
.out
.println(ids
.length
);
数组一旦初始化,其长度就是确定的。
数组长度一旦确定,就不可以修改。这是因为
public class ArrayTest{
public static void main(String args
[]){
int[] arr1
= new int[]{1, 2, 3};
String
[] arr2
= new String[10];
}
}
③一维数组元素的默认初始化值
数组元素为整型:0数组元素为浮点型:0.0数组元素为字符型:0或\u0000,而非‘0’数组元素为boolean:false数组元素为引用数据类型:null
④一维数组的遍历
for(int i
= 0; i
< names
.length
; i
++){
System
.out
.println(names
[i
]);
}
⑤一维数组的内存解析
3 二维数组
①二维数组的概念
对于二维数组的理解,我们可以看成是一维数组array1又作为另一个一维数组array2的元素而存在。
其实,从数组底层的运行机制来看,其实没有多维数组。
②二维数组的声明与初始化
int[] arr
= new int[]{1,2,3};
int[][] arr1
= new int[][]{{1,2,3},{4,5},{6,7,8}};
String
[][] arr2
= new String[3][2];
String
[][] arr3
= new String[3][];
③如何调用二维数组的指定位置的元素
System
.out
.println(arr1
[0][1]);
System
.out
.println(arr2
[1][1]);
System
.out
.println(arr2
[0]);
④如何获取数组的长度
System
.out
.println(arr4
.length
);
System
.out
.println(arr4
[0].length
);
System
.out
.println(arr4
[1].length
);
⑤如何遍历数组
for(int i
= 0;i
< arr4
.length
;i
++){
for(int j
= 0;j
< arr4
[i
].length
;j
++){
System
.out
.print(arr4
[i
][j
] + " ");
}
System
.out
.println();
}
⑥二维数组的内存解析
4 数组常见算法
①数组的赋值与复制
赋值
array2 = array1;
如何理解:将array1保存的数组的地址值赋给了array2,使得array1和array2共同指向堆空间中的同一个数组实体。
复制
如何理解:我们通过new的方式,给array2在堆空间中新开辟了数组的空间。将array1数组中的元素值一个一个的赋值到array2数组中。
array2
= new int[array1
.length
];
for(int i
= 0;i
< array2
.length
;i
++){
array2
[i
] = array1
[i
];
}
②数组元素的反转
③数组元素的查找
5.1 线性查找: 实现思路:通过遍历的方式,一个一个的数据进行比较、查找。 适用性:具有普遍适用性。
int goal
= 1;
boolean flag
= true;
for (int i
= 0; i
< arr
.length
; i
++) {
if (goal
== arr
[i
]){
System
.out
.println("找到了~");
flag
= false;
break;
}
}
if (flag
){
System
.out
.println("没有找到~");
}
5.2 二分法查找: 实现思路:每次比较中间值,折半的方式检索。 适用性:(前提:数组必须有序)—> 二分查找
int goal
= 98;
int head
= 0;
int end
= arr
.length
-1;
boolean flag
= true;
while(head
<= end
){
int middle
= (head
+ end
) / 2;
if(arr
[middle
] == goal
){
System
.out
.println("找到了指定的元素:"+goal
+",索引为:"+middle
);
flag
= false;
break;
}else if(arr
[middle
] < goal
){
head
= middle
+ 1;
}else{
end
= middle
- 1;
}
}
if(flag
){
System
.out
.println("没有找到了指定的元素");
}
④数组的排序算法
冒泡排序
public class Bubble{
public static void main(String args
[]){
int[] arr
= new int[]{43,32,76,-98,0,64,33,-21,32,99};
for(int i
= 0; i
< arr
.length
- 1; i
++){
for(int j
= 0; j
< arr
.length
- 1 - i
; j
++){
if (arr
[j
] > arr
[j
+ 1]){
int temp
= arr
[j
];
arr
[j
] = arr
[j
+1];
arr
[j
+1] = temp
;
}
}
}
}
}
选择排序
public class SelectSort{
public static void main(String
[] args
){
int[] arr
= new int[]{43,32,76,-98,0,64,33,-21,32,99};
for (int i
= 0; i
< arr
.length
- 1; i
++) {
int max
= 0;
int maxIndex
= 0;
for (int j
= 0; j
< arr
.length
- i
; j
++) {
if (arr
[j
] > max
){
max
= arr
[j
];
maxIndex
= j
;
}
}
int temp
= arr
[arr
.length
- 1 - i
];
arr
[arr
.length
- 1 - i
] = max
;
arr
[maxIndex
] = temp
;
}
}
}
快速排序
5 Arrays工具类的使用
1.理解:
① 定义在java
.util包下。
② Arrays
:提供了很多操作数组的方法。
2.使用:
int[] arr1
= new int[]{1,2,3,4};
int[] arr2
= new int[]{1,3,2,4};
boolean isEquals
= Arrays
.equals(arr1
, arr2
);
System
.out
.println(isEquals
);
System
.out
.println(Arrays
.toString(arr1
));
Arrays
.fill(arr1
,10);
System
.out
.println(Arrays
.toString(arr1
));
Arrays
.sort(arr2
);
System
.out
.println(Arrays
.toString(arr2
));
int[] arr3
= new int[]{-98,-34,2,34,54,66,79,105,210,333};
int index
= Arrays
.binarySearch(arr3
, 210);
if(index
>= 0){
System
.out
.println(index
);
}else{
System
.out
.println("未找到");
}
6 数组常见异常
1.数组角标越界异常:ArrayIndexOutOfBoundsException
int[] arr
= new int[]{1,2,3,4,5};
2.空指针异常:NullPointerException
String
[] arr3
= new String[]{"AA","BB","CC"};
arr3
[0] = null
;
System
.out
.println(arr3
[0].toString());
小知识:一旦程序出现异常,未处理时,就终止执行。
7 练习题
①一维数组
package com
.atguigu
.exer
;
import java
.util
.Scanner
;
public class ArrayDemo1 {
public static void main(String
[] args
) {
Scanner scanner
= new Scanner(System
.in
);
System
.out
.println("请输入学生人数:");
int number
= scanner
.nextInt();
int[] scores
= new int[number
];
System
.out
.println("请输入" + number
+ "个学生成绩:");
int maxScore
= 0;
for(int i
= 0;i
< scores
.length
;i
++){
scores
[i
] = scanner
.nextInt();
if(maxScore
< scores
[i
]){
maxScore
= scores
[i
];
}
}
char level
;
for(int i
= 0;i
< scores
.length
;i
++){
if(maxScore
- scores
[i
] <= 10){
level
= 'A';
}else if(maxScore
- scores
[i
] <= 20){
level
= 'B';
}else if(maxScore
- scores
[i
] <= 30){
level
= 'C';
}else{
level
= 'D';
}
System
.out
.println("student " + i
+
" score is " + scores
[i
] + ",grade is " + level
);
}
}
}
②二维数组
package com
.atguigu
.exer
;
public class ArrayDemo {
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
);
}
}