数组

稀疏数组

应用场景:当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。(五子棋盘)

处理方法:

  1. 记录数组一共有几行几列,有多少个不同分值 (第一行)
  2. 把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模

二维数组转稀疏数组的思路:

  1. 遍历原始的二维数组,得到有效数据的个数sum
  2. 根据sum就可以创建稀疏数组sparseArr int [sum + 1] [3]
  3. 将二维数组的有效数据存入稀疏数组

稀疏数组转原始的二维数组:

  1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
  2. 再读取稀疏数组后几行的数据,并赋值给原始的二维数组即可
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
public class SparseArray {
public static void main(String[] args) {
//创建一个原始二维数据11*11
//0表示没有棋子,1表示黑子,2表示白子
int[][] chessArr = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
chessArr[2][4] = 2;
System.out.println("初始创建的二维数组=============");
for (int[] row : chessArr) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}

//将二维数组转换为稀疏数组
//1.先遍历二维数组,得到非零数据的个数
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j] != 0) {
sum++;
}
}
}
//2.创建对应的稀疏数组
int[][] sparseArr = new int[sum + 1][3];
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = sum;
//遍历二维数组得到非零元素存放在sparseArr中
int count = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j] != 0) {
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr[i][j];
}
}
}
//输出稀疏数组
System.out.println();
System.out.println("稀疏数组如下===========");
for (int i = 0; i < sparseArr.length; i++) {
System.out.printf("%d\t%d\t%d\n",sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]);
}
//将稀疏数组还原成二维数组
//1.先读取稀疏数组的第一行,创建原始数据
//2.再读取稀疏数组后面几行,将数组元素赋值给二维数组即可
int chessArr2[][] = new int [sparseArr[0][0]][sparseArr[0][1]];
for (int i = 1; i <= sparseArr[0][2]; i++) {
chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
//输出原始二维数组
System.out.println("还原后的二维数组==========");
for (int[] row : chessArr2) {
for (int data : row) {
System.out.printf("%d\t", data);
}
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
public class SparseArrayExercise {
public static void main(String[] args) {
String []words = {"at", "", "", "", "ball", "", "", "car", "", "","dad", "", ""};
String s = "at";
Solution solution = new Solution();
System.out.println(solution.findString(words,s));
}
}

class Solution {
public int findString(String[] words, String s) {
int sum = 0;
for (int i = 0; i < words.length; i++) {
if (!words[i].equals("")){
sum++;
}
}
int count = 0;
String[][] sparseArr = new String[sum+1][2];
sparseArr[0][0] = String.valueOf(words.length);
sparseArr[0][1] = String.valueOf(sum);

for (int i = 0; i < words.length; i++) {
if (!words[i].equals("")){
count++;
sparseArr[count][0] = String.valueOf(i);
sparseArr[count][1] = words[i];
}
}
for (int i = 1; i <= sum; i++) {
if (sparseArr[i][1].equals(s)){
return Integer.parseInt(sparseArr[i][0]);
}
}
return -1;
}
}

数组模拟队列

队列数组的声明:

maxSize : 队列的最大容量

front : 队列前端 rear : 队列后端

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
import java.util.Scanner;

public class ArrayQueue {
public static void main(String[] args) {
ArrQueue arrQueue = new ArrQueue(3);
char key = ' '; //接受用户输入
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while (loop){
System.out.println("s(show):显示队列");
System.out.println("e(exit):退出程序");
System.out.println("a(add):添加数据到队列");
System.out.println("h(head):查看头部数据");
System.out.println("g(get):取数据");

key = scanner.next().charAt(0);
switch (key){
case 's':
arrQueue.showQueue();
break;
case 'e':
System.exit(-1);
break;
case 'a':
System.out.print("请输入要添加的数据:");
arrQueue.addQueue(scanner.nextInt());
break;
case 'h':
try {
System.out.println("头数据为:" + arrQueue.headQueue());
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'g':
try {
int res = arrQueue.getQueue();
System.out.println("取出的数据为:" + res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
default:
System.out.println("输入有误");
break;
}
}
}
}

//使用数组模拟队列
class ArrQueue {
private int maxSize;
private int front; //头
private int rear; //尾
private int[] arr; //存放数据,模拟队列

//创建队列的构造器
public ArrQueue(int maxSize) {
this.maxSize = maxSize;
arr = new int[maxSize];
front = -1; //指向队列头部的前一个位置
rear = -1; //指向队列尾部
}

//判断队列是否为满
public boolean isFull() {
return rear == maxSize - 1;
}

//判断队列是否为空
public boolean isEmpty() {
return rear == front;
}

//添加数据到队列
public void addQueue(int n) {
if (isFull()) {
System.out.println("队列满,不能添加数据");
return;
}
rear++;
arr[rear] = n;
}

//出队列
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,不能取元素\n");
}
front++;
return arr[front];
}

//显示队列的所有数据
public void showQueue() {
if (isEmpty()) {
System.out.println("队列为空,没有数据");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}

//显示队列头数据
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,没有数据\n");
}
return arr[front + 1];
}
}

目前数组只能用一次,需改进为环形队列