第四章 数组Array
数组(Array)是有序的元素序列。 若将有限个类型相同的变量的集合命名,那么这个名称为数组名。组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量/12713827)。用于区分数组的各个元素的数字编号称为下标。数组是在程序设计中,为了处理方便, 把具有相同类型的若干元素按有序的形式组织起来的一种形式。 这些有序排列的同类数据元素的集合称为数组。
数组是用于储存多个相同类型数据的集合。数组是对象类型(引用类型)
数组是固定长度,下标是从0开始,结束下标是,length属性-1,下标不存在系统会抛出异常。
4.1 声明数组对象
声明数组,是一个有序元素集合。java中数组元素的个数是固定的(声明后不能增加元素,不能删除元素)
类型[] 变量名 = new 类型[];
/声明数组,是一个有序元素集合。java中数组元素的个数是固定的(声明后不能增加元素,不能删除元素)
//声明数组,自动初始化,元素的个数是 3个下标是从0开始到 0 1 2
int[] a1 = new int[3];
var a2 = new int[3];
//声明数组并直接赋值
int[] a3 = {10, 20, 30};
int a4[] = new int[]{1, 2, 3, 4, 5};
//声明字符串数组,并直接赋值,元素个数5个 下标是0-4
String[] sts = new String[]{"李四", "王五", "张三"};
byte[] b1 = new byte[3];
short[] s1 = new short[3];
long[] lon1 = new long[]{1, 2, 3};
boolean[] bool1 = new boolean[5];
double[] d1 = new double[5];
Date[] ds = new Date[6];
char[] cs = "hello".toCharArray();
char[] c2 = new char[]{'a', 'c', 'd'};
//判断变量a2 是不是int数组
System.out.println(a2 instanceof int[]);
4.2 使用数组
数组初始化、数组赋值、读取值
数组对象有.length 属性返回数组元素的个数千万不写
length()
遍历数组
package cn.yuan;
import java.util.Arrays;
public class Arr01 {
public static void main(String[] args) {
var ss = "java,html,css,javascript,mysql,vue,python".split(",");
System.out.println(ss.length);
System.out.println(ss);//输出地址
System.out.println(Arrays.toString(ss));//以数组格式输出
for (String s : ss) {//foreach 遍历数组
System.out.println(s);
//System.out.println(ss[index++]);
}
}
}
运行结果:
7
[Ljava.lang.String;@3b07d329
[java, html, css, javascript, mysql, vue, python]
java
html
css
javascript
mysql
vue
python
4.3 数组排序
数组排升序数组排降序数组洗牌
冒泡排序算法、洗牌算法
package cn.yuan;
import java.util.Arrays;
import java.util.Random;
public class Arr02 {
public static void main(String[] args) {
//声明十个元素的int数组,并赋值10个数(1-100)随机数,直接遍历输出,排序后,在输出
Random random = new Random();
int[] num = new int[10];
for (int i = 1; i < num.length; i++) num[i] = random.nextInt(1, 101);
System.out.println(Arrays.toString(num));
//冒泡排序
for (int a = 0; a < num.length; a++) {
for (int b = 0; b < num.length - a - 1; b++) {
if (num[b] > num[b + 1]) {//>升序 <降序 Math.random()>.5 乱序
int t;
t = num[b];
num[b] = num[b + 1];
num[b + 1] = t;
}
}
//System.out.println(Arrays.toString(num));
}
System.out.println(Arrays.toString(num));
}
}
运行结果:
[0, 77, 80, 93, 68, 64, 64, 70, 55, 38]
[0, 38, 55, 64, 64, 68, 70, 77, 80, 93]
洗牌算法:
package cn.yuan;
import java.util.Arrays;
import java.util.Random;
public class Arr02 {
public static void main(String[] args) {
//声明十个元素的int数组,并赋值10个数(1-100)随机数,直接遍历输出,排序后,在输出
Random random = new Random();
int[] num = new int[10];
for (int i = 1; i < num.length; i++) num[i] = random.nextInt(1, 101);
System.out.println(Arrays.toString(num));
for (int i = 0; i < num.length * 2; i++) {
int x = random.nextInt(num.length);
int y = random.nextInt(num.length);
if (x == y) {
--i;
continue;
} else {
int t = num[x];
num[x] = num[y];
num[y] = t;
}
}
System.out.println(Arrays.toString(num));
}
}
运行结果:
[0, 26, 98, 30, 84, 91, 28, 66, 99, 8]
[30, 26, 66, 99, 84, 8, 91, 28, 0, 98]
Arrays.sort()
package cn.yuan;
public class Student implements Comparable<Student> {
private int id;
private String name;
private int score;
private String address;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", score=" + score +
", address='" + address + '\'' +
'}';
}
public Student() {
}
public Student(int id, String name, int score, String address) {
this.id = id;
this.name = name;
this.score = score;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public int compareTo(Student o) {
//通过id和address排序
//return id - o.id;
return address.compareTo(o.address);
}
}
测试类:
package cn.yuan;
import java.util.Arrays;
import java.util.Random;
public class ArrSort1 {
public static void main(String[] args) {
Student student = new Student();
student.setId(10);
student.setName("奈何");
student.setScore(90);
student.setAddress("新乡");
System.out.println(student);
Student[] stus = new Student[]{
new Student(99, "张一", 80, "郑州"),
new Student(2, "张二", 90, "洛阳"),
new Student(3, "张三", 89, "郑州"),
new Student(4, "张四", 62, "北京"),
new Student(5, "张五", 45, "上海"),
new Student(6, "张六", 79, "苏州")
};
System.out.println(Arrays.toString(stus));
System.out.println("----------------------------------------------------------------");
//根据对象的成绩降序
Arrays.sort(stus, (a, b) -> b.getScore() - a.getScore());
System.out.println(Arrays.toString(stus));
System.out.println("----------------------------------------------------------------");
//根据id排序
Arrays.sort(stus);
System.out.println(Arrays.toString(stus));
System.out.println("----------------------------------------------------------------");
//System.exit(0);
//字符串排序
String[] ss = {"java", "c", "c++", "rust", "ruby", "python", "c#"};
System.out.println("排序前:" + Arrays.toString(ss));
Arrays.sort(ss);
System.out.println("升序:" + Arrays.toString(ss));
Arrays.sort(ss, (a, b) -> b.compareTo(a));
System.out.println("降序:" + Arrays.toString(ss));
//根据字符长度升序
Arrays.sort(ss, (a, b) -> a.length() - b.length());
System.out.println("字符长度升序升序:" + Arrays.toString(ss));
//根据字符长度降序
Arrays.sort(ss, (a, b) -> b.length() - a.length());
System.out.println("字符长度升序降序:" + Arrays.toString(ss));
//洗牌
Random random = new Random();
for (int i = 0; i < ss.length * 5; i++) {
int a = random.nextInt(ss.length);
int b = random.nextInt(ss.length);
if (a == b) {
--i;
continue;
} else {
String t = ss[a];
ss[a] = ss[b];
ss[b] = t;
}
}
System.out.println("洗牌:" + Arrays.toString(ss));
//数字
Integer[] n1 = {1, 3, 0, 6, 8, 4, 2};//对象类型,基本类型报错
System.out.println("排序前:" + Arrays.toString(n1));
// Object[] objects = new Object[]{1, 5, new Date(), LocalDate.now()};//object是几乎所有对象的父类,万能数组
//Arrays.sort(n1);
Arrays.sort(n1, (x, y) -> x - y);
System.out.println("升序:" + Arrays.toString(n1));
Arrays.sort(n1, (x, y) -> y - x);
System.out.println("降序:" + Arrays.toString(n1));
}
}
运行结果:
Student{id=10, name='奈何', score=90, address='新乡'}
[Student{id=99, name='张一', score=80, address='郑州'}, Student{id=2, name='张二', score=90, address='洛阳'}, Student{id=3, name='张三', score=89, address='郑州'}, Student{id=4, name='张四', score=62, address='北京'}, Student{id=5, name='张五', score=45, address='上海'}, Student{id=6, name='张六', score=79, address='苏州'}]
----------------------------------------------------------------
[Student{id=2, name='张二', score=90, address='洛阳'}, Student{id=3, name='张三', score=89, address='郑州'}, Student{id=99, name='张一', score=80, address='郑州'}, Student{id=6, name='张六', score=79, address='苏州'}, Student{id=4, name='张四', score=62, address='北京'}, Student{id=5, name='张五', score=45, address='上海'}]
----------------------------------------------------------------
[Student{id=5, name='张五', score=45, address='上海'}, Student{id=4, name='张四', score=62, address='北京'}, Student{id=2, name='张二', score=90, address='洛阳'}, Student{id=6, name='张六', score=79, address='苏州'}, Student{id=3, name='张三', score=89, address='郑州'}, Student{id=99, name='张一', score=80, address='郑州'}]
----------------------------------------------------------------
排序前:[java, c, c++, rust, ruby, python, c#]
升序:[c, c#, c++, java, python, ruby, rust]
降序:[rust, ruby, python, java, c++, c#, c]
字符长度升序升序:[c, c#, c++, rust, ruby, java, python]
字符长度升序降序:[python, rust, ruby, java, c++, c#, c]
洗牌:[python, c#, rust, c, java, c++, ruby]
排序前:[1, 3, 0, 6, 8, 4, 2]
升序:[0, 1, 2, 3, 4, 6, 8]
降序:[8, 6, 4, 3, 2, 1, 0]
4.4 多维数组
package cn.yuan;
import java.util.Arrays;
public class Arrs01 {
public static void main(String[] args) {
//一二三维数组
String[] s1;
String[][] s2;
String[][][] s3;
//初始化和遍历数组
int[][] i1 = {{1, 2, 3}, {2, 3}, {3}};
var i2 = new int[][]{{1, 2, 3}, {2}, {3, 5}};
System.out.println(i1[2][0]);
System.out.println(i2[0][2]);
for (int[] nn : i2) {
for (int tt : nn) {
System.out.print(tt + " ");
}
}
System.out.println();
//遍历
System.out.println("---------------------------------");
for (int i = 0; i < i2.length; i++) {
for (int j = 0; j < i2[i].length; j++) {
System.out.printf("i2[%d] [%d]= %d%n", i, j, i2[i][j]);
}
}
int[][] n2 = new int[3][5];
System.out.println(Arrays.toString(n2));//输出地址
System.out.println(Arrays.deepToString(n2));
System.out.println(Arrays.deepToString(i2));
String[][][] ss3 = new String[][][]{{{"a", "b"}, {"e", "r"}, {"d", "s"}}};
System.out.println(Arrays.deepToString(ss3));
}
}
运行结果:
3
3
1 2 3 2 3 5
---------------------------------
i2[0] [0]= 1
i2[0] [1]= 2
i2[0] [2]= 3
i2[1] [0]= 2
i2[2] [0]= 3
i2[2] [1]= 5
[[I@312b1dae, [I@7530d0a, [I@27bc2616]
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
[[1, 2, 3], [2], [3, 5]]
[[[a, b], [e, r], [d, s]]]
4.5 数组工具类
Arrays.fill()、Arrays.toString()、Arrays.sort()、Arrays.deepToString(nums)
package cn.yuan;
import java.util.Arrays;
import java.util.List;
public class Arr {
public static void main(String[] args) {
Integer[] n1 = {1, 2, 3};
System.out.println(n1);//输出地址
//数组转换为List
List<Integer> ints = Arrays.asList(n1);
System.out.println(ints);
//asList()返回List
List<Integer> list = Arrays.asList(1, 2, 4);
System.out.println(list);
int[] nn = new int[6];
System.out.println(Arrays.toString(nn));
//数组填充值
Arrays.fill(nn, 6);
//从1-3 不包括3的位置填充5
Arrays.fill(nn, 1, 3, 5);
System.out.println(Arrays.toString(nn));
//stream
int sum = 0;
for (int i : nn
) {
sum += i;
}
System.out.println(sum);
int sum2 = Arrays.stream(nn).sum();
System.out.println(sum2);
//Arrays.deepToString()
String[][][] ss3 = new String[][][]{{{"a", "b"}, {"e", "r"}, {"d", "s"}}};
System.out.println(Arrays.deepToString(ss3));
//Arrays.toString()、Arrays.sort()
String[] ss = {"java", "c", "c++", "rust", "ruby", "python", "c#"};
System.out.println("排序前:" + Arrays.toString(ss));
Arrays.sort(ss);
System.out.println("升序:" + Arrays.toString(ss));
Arrays.sort(ss, (a, b) -> b.compareTo(a));
System.out.println("降序:" + Arrays.toString(ss));
}
}
运行结果:
[Ljava.lang.Integer;@3b07d329
[1, 2, 3]
[1, 2, 4]
[0, 0, 0, 0, 0, 0]
[6, 5, 5, 6, 6, 6]
34
34
[[[a, b], [e, r], [d, s]]]
排序前:[java, c, c++, rust, ruby, python, c#]
升序:[c, c#, c++, java, python, ruby, rust]
降序:[rust, ruby, python, java, c++, c#, c]