创建数组
数组是一个固定长度的,包含了相同类型数据的 容器
1. 声明数组
int[] a;
声明了一个数组变量。
[]
表示该变量是一个数组
int
表示数组里的每一个元素都是一个整数
a
是变量名
但是,仅仅是这一句声明,不会创建数组
有时候也会写成 int a[];
没有任何区别,就是你看哪种顺眼的问题
public class HelloWorld {
public static void main(String[] args) {
// 声明一个数组
int[] a;
}
}
2. 创建数组
创建数组的时候,要指明数组的长度。new int[5]
引用概念: 如果变量代表一个数组,比如a,我们把a叫做 引用
与基本类型不同
int c = 5;
这叫给c 赋值 为5
声明一个引用 int[] a;
a = new int[5];
让a这个引用,指向 数组
public class HelloWorld {
public static void main(String[] args) {
// 声明一个引用
int[] a;
// 创建一个长度是5的数组,并且使用引用a指向该数组
a = new int[5];
int[] b = new int[5]; // 声明的同时,指向一个数组
}
}
3. 访问数组
数组下标 基0
下标0,代表数组里的第一个数
public class HelloWorld {
public static void main(String[] args) {
int[] a;
a = new int[5];
a[0] = 1; // 下标0,代表数组里的第一个数
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;
}
}
4. 数组长度
.length属性 用于访问一个数组的长度
数组访问下标范围是0到长度-1
一旦超过这个范围,就会产生数组下标越界异常
public class HelloWorld {
public static void main(String[] args) {
int[] a;
a = new int[5];
System.out.println(a.length); // 打印数组的长度
a[4] = 100; // 下标4,实质上是“第5个”,即最后一个
a[5] = 101; // 下标5,实质上是“第6个”,超出范围 ,产生数组下标越界异常
}
}
5. 练习 - 数组最小值
首先创建一个长度是5的数组
然后给数组的每一位赋予随机整数
通过for循环,遍历数组,找出最小的一个值出来
0-100的 随机整数的获取办法有多种,下面是参考办法之一:
(int) (Math.random() * 100)
Math.random() 会得到一个0-1之间的随机浮点数,然后乘以100,并强转为整型即可。
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[5];
a[0] = (int) (Math.random() * 100);
a[1] = (int) (Math.random() * 100);
a[2] = (int) (Math.random() * 100);
a[3] = (int) (Math.random() * 100);
a[4] = (int) (Math.random() * 100);
System.out.print("数组中的各个随机数是:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
初始化数组
1. 分配空间与赋值分步进行
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[5]; // 分配了长度是5的数组,但是没有赋值
// 没有赋值,那么就会使用默认值
// 作为int类型的数组,默认值是0
System.out.println(a[0]); // 0
// 进行赋值
a[0] = 100;
a[1] = 101;
a[2] = 103;
a[3] = 120;
a[4] = 140;
}
}
2. 分配空间,同时赋值
public class HelloWorld {
public static void main(String[] args) {
// 写法一: 分配空间同时赋值
int[] a = new int[] {100, 102, 444, 836, 3236};
// 写法二: 省略了new int[],效果一样
int[] b = {100, 102, 444, 836, 3236};
// 写法三:同时分配空间,和指定内容
// 在这个例子里,长度是3,内容是5个,产生矛盾了
// 所以如果指定了数组的内容,就不能同时设置数组的长度
// int[] c = new int[3] {100, 102, 444, 836, 3236}; // error
}
}
3. 练习 - 数组反转
首先创建一个长度是5的数组,并填充随机数。
使用for循环或者while循环,对这个数组实现反转效果
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[5];
for (int i = 0; i < a.length; i++ ) {
a[i] = (int) (Math.random() * 100);
}
System.out.println("原数组:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
for (int i = 0; i < a.length / 2; i++) {
int temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i -1] = temp;
}
System.out.println("现在数组:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
排序
1. 选择法排序
选择法排序的思路:
把第一位 和其他所有的进行比较,只要比第一位小的,就换到第一个位置来
比较完后,第一位就是最小的
然后再从 第二位 和剩余的其他所有进行比较,只要比第二位小,就换到第二个位置来
比较完后, 第二位就是第二小的
以此类推
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[] { 18, 62, 68, 82, 65, 9 };
// 排序前,先把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
// 选择法排序
// 第一步: 把第一位和其他所有位进行比较
// 如果发现其他位置的数据比第一位小,就进行交换
for (int i = 1; i < a.length; i++) {
if (a[i] < a[0]) {
int temp = a[0];
a[0] = a[i];
a[i] = temp;
}
}
// 把内容打印出来
// 可以发现,最小的一个数,到了最前面
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
// 第二步: 把第二位的和剩下的所有位进行比较
for (int i = 2; i < a.length; i++) {
if (a[i] < a[1]) {
int temp = a[1];
a[1] = a[i];
a[i] = temp;
}
}
// 把内容打印出来
// 可以发现,倒数第二小的数,到了第二个位置
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
// 可以发现一个规律
// 移动的位置是从0 逐渐增加的
// 所以可以在外面套一层循环
for (int j = 0; j < (a.length - 1); j++) {
for (int i = j + 1; i < a.length; i++) {
if (a[i] < a[j]) {
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
// 把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
}
}
2. 冒泡法排序
冒泡法排序的思路:
第一步:从第一位开始,把相邻两位进行比较
如果发现前面的比后面的大,就把大的数据交换在后面,循环比较完毕后,最后一位就是最大的
第二步:再来一次,只不过不用比较最后一位
以此类推
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[] {18, 62, 68, 82, 65, 9};
// 排序前,先把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
// 冒泡法排序
// 第一步:从第一位开始,把相邻两位进行比较
// 如果发现前面的比后面的大,就把大的数据交换在后面
for (int i = 0; i < a.length-1; i++) {
if (a[i] > a[i+1]) {
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
// 把内容打印出来
// 可以发现,最大的到了最后面
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
// 第二步:再来一次,只不过不用比较最后一位
for (int i = 0; i < a.length-2; i++) {
if (a[i] > a[i+1]) {
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
// 把内容打印出来
// 可以发现,倒数第二大的到了倒数第二个位置
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
// 可以发现一个规律
// 后边界在收缩
// 所以可以在外面套一层循环
for (int j = 0; j < a.length; j++) {
for (int i = 0; i < a.length-j-1; i++) {
if (a[i] > a[i+1]) {
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
// 把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
3. 练习- 排序
首先创建一个长度是5的数组,并填充随机数。
首先用选择法正排序,然后再对其使用冒泡法倒排序
所谓的正排序就是从小到大排序,倒排序就是从大到小排序
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[5];
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 100);
System.out.print(a[i] + " ");
}
System.out.println();
// 选择排序正排序,从小到大
int temp = 0;
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("正向排序结果:");
for (int i : a) {
System.out.print(i + " ");
}
System.out.println();
// 冒泡法逆排序,从大到小
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] < a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
System.out.println("逆向排序结果:");
for (int i : a) {
System.out.print(i + " ");
}
}
}
增强型for循环
增强型for循环在遍历一个数组的时候会更加快捷
- 增强型for循环
增强型for循环只能用来取值,却不能用来修改数组里的值
public class HelloWorld {
public static void main(String[] args) {
int[] values = new int[] {18, 62, 68, 82, 65, 9};
// 常规遍历
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
// 增强型for循环遍历
for (int each : values) {
System.out.println(each);
}
}
}
- 练习 - 最大值
用增强型for循环找出最大的那个数
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[5];
System.out.print("数组中的数据为:");
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 100);
System.out.print(a[i] + " ");
}
int max = a[0];
for (int each : a) {
if (each > max) {
max = each;
}
}
System.out.println("\n数组中的最大值为:" + max);
}
}
复制数组
数组的长度是不可变的,一旦分配好空间,是多长,就多长,不能增加也不能减少
1. 复制数组
把一个数组的值,复制到另一个数组中
System.arraycopy(src, srcPos, dest, destPos, length);
src:源数组
srcPos:从源数组复制数据的起始位置
dest:目标数组
destPos:复制到目标数组的起始位置
length:复制的长度
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[] {18, 62, 68, 82, 65, 9};
int[] b = new int[3]; // 分配了长度是3的空间,但是没有赋值
// 通过数组赋值把a数组的前3位赋值到b数组
// 方法一:for循环
for (int i = 0; i < b.length; i++) {
b[i] = a[i];
System.out.print(b[i] + " "); // 18 62 68
}
// 方法二:System.arraycopy(src, srcPos, dest, destPos, length);
System.arraycopy(a, 0, b, 0, 3);
for (int each : b) {
System.out.print(each + " "); // 18 62 68
}
}
}
2. 练习 - 合并数组
首先准备两个数组,他俩的长度是5-10之间的随机数,并使用随机数初始化这两个数组
然后准备第三个数组,第三个数组的长度是前两个的和
通过System.arraycopy 把前两个数组合并到第三个数组中
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[6];
int[] b = new int[7];
int[] c = new int[a.length + b.length];
System.out.println("数组a的内容:");
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 100);
System.out.print(a[i] + " ");
}
System.out.println("\n数组b的内容:");
for (int i = 0; i < b.length; i++) {
b[i] = (int) (Math.random() * 100);
System.out.print(b[i] + " ");
}
System.out.println("\n数组c的内容:");
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
for (int each : c) {
System.out.print(each + " ");
}
}
}
二维数组
这是一个 一维数组,里面的每一个元素,都是一个基本类型int
int[] a = new int[] {1, 2, 3, 4, 5};
这是一个 二维数组,里面的每一个元素,都是一个一维数组
所以二维数组又叫 数组的数组
int[][] b = new int[][] {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
1. 初始化二维数组
public class HelloWorld {
public static void main(String[] args) {
// 初始化二维数组
int[][] a = new int[2][3]; // 有两个一维数组,每个一维数组的长度是3
a[1][2] = 5; // 可以直接访问一维数组,因为已经分配了空间
// 只分配了二维数组
int[][] b = new int[2][]; // 有两个一维数组,每个一维数组的长度暂未分配
b[0] = new int[3]; // 必须事先分配长度,才可以访问
b[0][2] = 5;
// 指定内容的同时,分配空间
int[][] c = new int[][] {
{1, 2, 4},
{4, 5},
{6,7,8,9}
};
}
}
2. 练习 - 二维数组
定义一个5X5的二维数组。然后使用随机数填充该二维数组。
找出这个二维数组里,最大的那个值,并打印出其二维坐标
(int) (Math.random() * 100);
Math.random() 会得到一个0-1之间的随机浮点数,然后乘以100,并强转为整型即可。
public class HelloWorld {
public static void main(String[] args) {
int[][] a = new int[5][5];
int max = -1;
int x = 0;
int y = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = (int) (Math.random() * 100);
System.out.print(a[i][j] + "\t\t");
if (a[i][j] > max) {
max = a[i][j];
x = i;
y = j;
}
}
System.out.println();
}
System.out.println("找出来最大的是:" + max + "\n其坐标是:[" + x + "] [" + y + "]");
}
}
Arrays
Arrays是针对数组的工具类,可以进行 排序,查找,复制填充等功能。
关键字 | 简介 |
---|---|
copyOfRange | 数组复制 |
toString() | 转换为字符串 |
sort | 排序 |
binarySearch | 搜索 |
equals | 判断是否相同 |
fill | 填充 |
1. 数组复制
与使用System.arraycopy进行数组复制类似的,Arrays提供了一个copyOfRange方法进行数组复制。
不同的是System.arraycopy,需要事先准备好目标数组,并分配长度。copyOfRange只需要源数组就可以了,通过返回值,就能够得到目标数组了。
除此之外,需要注意的是copyOfRange的 第3个参数,表示数组的结束位置,是 取不到的。
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[] {18, 62, 68, 65, 9};
// copyOfRange(int[] original, int from, int to)
// 第一个参数表示源数组
// 第二个参数表示开始位置(取得到)
// 第三个参数表示结束位置(取不到)
int[] b = Arrays.copyOfRange(a, 0, 3);
for (int each : b) {
System.out.print(each + " "); // 18 62 68
}
}
}
2. 转换为字符串
如果要打印一个数组的内容,就需要通过for循环来挨个遍历,逐一打印
但是Arrays提供了一个toString()方法,直接把一个数组,转换为字符串,这样方便观察数组的内容
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[] {18, 62, 68, 82, 65, 9};
String content = Arrays.toString(a);
System.out.println(content); // [18, 62, 68, 82, 65, 9]
}
}
3. 排序
在前面章节学习了选择法排序和冒泡排序,Arrays工具类提供了一个sort方法,只需要一行代码即可完成排序功能。
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[] { 18, 62, 68, 82, 65, 9 };
System.out.println("排序之前:");
System.out.println(Arrays.toString(a)); // [18, 62, 68, 82, 65, 9]
Arrays.sort(a);
System.out.println("排序之后:");
System.out.println(Arrays.toString(a)); // [9, 18, 62, 65, 68, 82]
}
}
4. 搜索
查询元素出现的位置
需要注意的是,使用binarySearch进行查找之前,必须使用sort进行排序
如果数组中有多个相同的元素,查找结果是不确定的
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int [] a = new int[] {18, 62, 68, 82, 65, 9};
Arrays.sort(a);
System.out.println(Arrays.toString(a)); // [9, 18, 62, 65, 68, 82]
// 使用binarySearch之前,必须先使用sort进行排序
System.out.println("数字 62出现的位置:" + Arrays.binarySearch(a, 62)); // 2
}
}
5. 判断是否相同
比较两个数组的内容是否一样
第二个数组的最后一个元素 是8,和第一个数组不一样,所以比较结果是false
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[] { 18, 62, 68, 82, 65, 9 };
int[] a = new int[] { 18, 62, 68, 82, 65, 8 };
System.out.println(Arrays.equals(a, b)); // false
}
}
6. 填充
使用同一个值,填充整个数组
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[10];
Arrays.fill(a, 5);
System.out.println(Arrays.toString(a)); // [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
}
}
7. 练习 - 二维数组排序
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int a[][] = new int[5][8];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = (int) (Math.random() * 100);
}
}
System.out.println("打印二维数组");
for (int[] i : a) {
System.out.println(Arrays.toString(i));
}
// 把二维数组复制到一维数组
int b[] = new int[a.length * a[0].length];
for (int i = 0; i < a.length; i++) {
System.arraycopy(a[i], 0, b, i * a[i].length, a[i].length);
}
// 对一维数组进行排序
Arrays.sort(b);
// 把一维数组复制到二维数组
for (int i = 0; i < a.length; i++) {
System.arraycopy(b, a[i].length * i, a[i], 0, a[i].length);
}
System.out.println("打印排序后的二维数组");
for (int[] i : a) {
System.out.println(Arrays.toString(i));
}
}
}