【384-424】数组
2022-01-20 21:01:00 # JavaSE

数组概述

  1. Java中数组是一种应用数据类型。
  2. 数组一旦创建,长度不可变。
  3. 数组存储在堆中。
  4. 数组存储的是引用类型时,实际上存储对象的内存地址。
  5. 数组都有length属性。
  6. 内存地址连续。
  7. 数组将第一个内存地址当整个数组对象的内存地址。
  8. 二维数组每个元素存放的是一个一维数组

一维数组

初始化一维数组

1
2
3
4
5
6
7
8
9
public class _388_初始化一维数组 {
public static void main(String[] args) {
// 静态初始化
int array1[] = {1,2,3,4,5};
// 动态初始化
int array2[] = new int[5];
}
}

对数组的修改与遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class _389_对一维数组中元素的访问 {
public static void main(String[] args) {
// 静态初始化
int p[] = {1,2,3,4,5};

p[p.length-1] = 555;

System.out.println("p.length = " + p.length);
for (int i = 0; i < p.length; i++) {
System.out.println("p[" + i + "] = " + p[i]);
}
}
}

方法的参数是数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class _392_方法的参数是数组 {
public static void main(String[] args) {
int[] p = {1,3,2,4};
printArray(p);
int[] a = new int[3];
printArray(a);

System.out.println("==========");
printArray(new int[3]);
}

public static void printArray(int[] array){
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}

}



main方法的String数组

运行命令

1
java Test str1 str2 str3

此时 JVM 会将 “str1 str2 str3” 以空格分隔传入 args[] 中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class _394_main方法的String数组 {
public static void main(String[] args) {
// JVM默认传递的 String[] args 长度默认为 0
// args 不是 null
System.out.println(args.length);

System.out.println("===============");
String[] s = new String[0];
printLength(s);
}

public static void printLength(String[] s){
System.out.println(s.length);
}
}

main方法String参数的案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class _395_main方法String参数的案例 {
public static void main(String[] args) {
if(args.length != 2){
System.out.println("请输入账号和密码,如 zhangsan 123");
return;
}

String username = args[0];
String password = args[1];

// 防止空指针异常~
// if(username.equals("zhangsan") && password.equals("123")){
if("zhangsan".equals(username) && "123".equals(password)){
System.out.println("登陆成功!");
}
else{
System.out.println("登陆失败!");
}
}
}


数组中存储引用数据类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class _396_数组中存储引用数据类型 {
public static void main(String[] args) {
// 静态初始化
Animal[] p = {new Animal(), new Animal()};
for (int i = 0; i < p.length; i++) {
p[i].move();
}

System.out.println("=============");

// 动态初始化
Animal[] animals = new Animal[3];
for (int i = 0; i < animals.length; i++) {
// 还需要 new 一下
animals[i] = new Animal();
animals[i].move();
}

System.out.println("=============");
Animal[] a = {new Cat(), new Bird()};
for (int i = 0; i < a.length; i++) {
a[i].move();
}

}
}

class Animal{
public void move(){
System.out.println("move~");
}
}

class Cat extends Animal{
@Override
public void move() {
System.out.println("cat move~");
}
}

class Bird extends Animal{
@Override
public void move() {
System.out.println("bird fly~");
}
}

数组扩容与拷贝

就是数组拷贝一份…

1
System.arraycopy(...);

源码

1
2
3
4
5
6
/*
* public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
*
* */
1
2
3
4
5
6
7
8
9
10
11
12
13
public class _398_数组拷贝 {
public static void main(String[] args) {
int[] src = {1, 11, 22, 3, 4};
int[] dst = new int[10];

System.arraycopy(src,1,dst,3,2);

for (int i = 0; i < dst.length; i++) {
System.out.println(i + " " + dst[i]);
}
}
}

二维数组

初始化二维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class _399_对二维数组的理解 {
public static void main(String[] args) {
int[][] a = {
{1,2,3},
{2,3,4,5},
{22,3}
};

int[][] b = new int[5][];
b[0] = new int[3];
b[1] = new int[4];
b[2] = new int[2];
}
}

二维数组的length属性

1
2
3
4
5
6
7
8
9
10
11
12
public class _400_二维数组的length属性 {
public static void main(String[] args) {
int[][] a = {
{1,2,3},
{2,3,4,5},
{22,3}
};
System.out.println(a.length); // 3
System.out.println(a[1].length); // 4
}
}

二维数组的元素访问与遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class _401_二维数组的元素访问 {
public static void main(String[] args) {
int[][] a = {
{1,2,3},
{2,3,4,5},
{22,3}
};
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.printf(a[i][j] + " ");
}
System.out.println();
}
}
}


方法的参数是一个二维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class _403_方法的参数是一个二维数组 {
public static void main(String[] args) {
int[][] a = {
{1,2,3},
{2,3,4,5},
{22,3}
};
printArrays(a);
}

public static void printArrays(int[][] a){
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.printf(a[i][j] + " ");
}
System.out.println();
}
}
}

作业

数组模拟栈数据结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class _410_数组模拟栈数据结构 {
public static void main(String[] args) {
MyStack myStack = new MyStack();

myStack.pop();
myStack.push(1);
myStack.push(2);
myStack.push(3);
myStack.push(4);
myStack.push(5);
myStack.push(6);
myStack.push(7);
myStack.push(8);
myStack.push(9);
myStack.push(10);
myStack.push(11);


}
}

class MyStack{
private Object[] elements;
private int index;

// 默认初始化容量是 10
public MyStack() {
elements = new Object[10];
index = 0;
}

public void push(Object o){
if(index >= 10){
System.out.println("栈满了~");
}
else{
elements[index++] = o;
System.out.println("push " + o);
}
}

public void pop(){
if(index <= 0){
System.out.println("栈已经空了~");
}
else{
--index;
}
}

// set get 也许用不上,但是必须写...
public Object[] getElements() {
return elements;
}

public void setElements(Object[] elements) {
this.elements = elements;
}

public int getIndex() {
return index;
}

public void setIndex(int index) {
this.index = index;
}



}

酒店管理系统部分功能实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.Objects;
import java.util.Scanner;

public class _413_酒店管理系统部分功能实现 {
public static void main(String[] args) {

Hotel hotel = new Hotel(3,6);

System.out.println("欢迎使用酒店管理系统,请认真使用以下说明");
System.out.println("功能对应编号:[1]表示查看房间列表, [2]订房, [3]退房");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入房间编号~");

while(true){
int op = scanner.nextInt();
if(op == 1){
hotel.printRooms();
}
else if(op == 2){
System.out.println("请输入房间ID");
int roomID = scanner.nextInt();
hotel.order(roomID);
}
else if(op == 3){
System.out.println("请输入房间ID");
int roomID = scanner.nextInt();
hotel.exit(roomID);
}
else{
System.out.println("输入有误~");
}
}


}
}

class Hotel{
private Room[][] rooms;

public Hotel(int x, int y) {
rooms = new Room[x][y];
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if(i == 0) rooms[i][j] = new Room((i+1)*100+j+1,"标准间",true);
else if(i == 1) rooms[i][j] = new Room((i+1)*100+j+1,"单人间",true);
else if(i == 2) rooms[i][j] = new Room((i+1)*100+j+1,"双人间",true);
else if(i == 3) rooms[i][j] = new Room((i+1)*100+j+1,"三人间",true);
}
}
}

public void printRooms(){
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
System.out.printf(rooms[i][j].toString() + " ");
}
System.out.println();
}
}

public void order(int roomId){
rooms[roomId/100 - 1][roomId%100 - 1].setStatus(false);
}

public void exit(int roomId){
rooms[roomId/100 - 1][roomId%100 - 1].setStatus(true);
}
}

class Room{
private int roomId; // ex: 101, 102, 201, 503 ...
private String type; // ex: 标准间, 单人间...
private boolean status; // true 空闲, flase 占用

public Room() {
}

public Room(int roomId, String type, boolean status) {
this.roomId = roomId;
this.type = type;
this.status = status;
}



// 以下是素养...
public int getRoomId() {
return roomId;
}

public void setRoomId(int roomId) {
this.roomId = roomId;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

// IDEA 默认 isStatus
public boolean getStatus() {
return status;
}

public void setStatus(boolean status) {
this.status = status;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Room room = (Room) o;
return roomId == room.roomId && status == room.status && Objects.equals(type, room.type);
}

@Override
public String toString() {
return "Room{" +
"roomId=" + roomId +
", type='" + type + '\'' +
", status=" + status +
'}';
}
}

Arrays工具类中的sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Arrays;

public class _417_Arrays工具类 {
public static void main(String[] args) {
int[] a = {3,2,4,5,1};

// 源码中 sort 前 static 修饰, 类名直接调用
Arrays.sort(a);
for (int i = 0; i < a.length; i++) {
System.out.printf(a[i] + " ");
}
System.out.println();
}
}


冒泡排序算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class _418_冒泡排序算法 {
public static void main(String[] args) {
int[] a = {5,4,2,1,3};
for (int i = 0; i < a.length; i++) {
for (int j = i+1; j < a.length; j++) {
if(a[i] > a[j]){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.printf(a[i] + " ");
}
System.out.println();
}
}


选择排序算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class _420_选择排序算法 {
public static void main(String[] args) {
int[] a = {4,2,1,5,3};
for (int i = 0; i < a.length; i++) {
int min = a[i];
int pos = i;
for (int j = i+1; j < a.length; j++) {
if(a[j] < min){
min = a[j];
pos = j;
}
}
int t = a[i];
a[i] = min;
a[pos] = t;
}
for (int i = 0; i < a.length; i++) {
System.out.printf(a[i] + " ");
}

}
}


二分查找法代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class _423_二分法查找代码实现 {
public static void main(String[] args) {
int[] p = {1,3,5,7,8,21,67};
System.out.println(lowerBound(p,20));
System.out.println(lowerBound(p,21));
System.out.println(lowerBound(p,22));
}

// 找到第一个大于等于 tar 的下标
public static int lowerBound(int[] p, int tar){
int l = 0;
int r = p.length-1;
int res = l;
while(l <= r){
int mid = l+r>>1;
if(p[mid] >= tar){
res = mid;
r = mid-1;
}
else{
l = mid+1;
}
}
return res;
}

}


Arrays工具类中的binarySearch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Arrays;

public class _424_Arrays工具类的使用 {
public static void main(String[] args) {
int[] p = {1,3,5,7,8,21,67};

// < 0 代表找不到, 返回下标
System.out.println(Arrays.binarySearch(p,20)); // -6
System.out.println(Arrays.binarySearch(p,21)); // 5
System.out.println(Arrays.binarySearch(p,22)); // -8
}
}


Prev
2022-01-20 21:01:00 # JavaSE
Next