1、什么是稀疏数组?
稀疏数组就是将一个普通的数组中重复的部分给去除的一种数组,能充分的减少空间的使用。
比如:
一维数组:a[10]:0 0 0 3 5 5 6 8 0 1
对应的稀疏数组:b[数组的有效长度][2]:
3 3
4 5
5 5
6 8
9 1
稀疏数组的第一列对应位置,第二列对应值。
二维数组:a[5][10]:
0 0 0 3 5 0 0 0 0 1
0 0 0 3 0 0 0 8 0 1
0 5 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
对应的稀疏数组:b[数组的有效长度][3]:
0 3 3
0 4 5
0 9 1
1 3 3
1 7 8
1 9 1
2 1 5
稀疏数组的第一列对应行位置,第二列对应列位置,第三列对应值。
三维等等依次类推
2、稀疏数组的使用
以一个下棋游戏为例,其中游戏具有:
(1)、下棋
(2)、悔棋
(3)、保存
(4)、退出
四个功能,在保存中,我将棋盘(二维数组)保存为稀疏数组进行存储,代码如下:
public class ChessBoard implements Serializable {
private static final long serialVersionUID = 8656128222714547171L;
private int[][] sparseArray;
private int count;
public int[][] getSparseArray() {
return sparseArray;
}
public void setSparseArray(int[][] arr) {
this.sparseArray = arr;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
功能代码如下:
public class SparseArray {
private static int count;
private static int[][] arr = new int[10][10];
public static void main(String[] args) throws IOException, ClassNotFoundException {
File file = new File("board");
if (!file.exists()){
count = 1;
}else {
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
ChessBoard chessBoard = (ChessBoard) inputStream.readObject();
int[][] sparseArray = chessBoard.getSparseArray();
for (int i=0; i < sparseArray.length;i++){
arr[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
}
count = chessBoard.getCount();
}
showBoard(arr);
Scanner sc = new Scanner(System.in);
while (true){
System.out.println("1、下棋");
System.out.println("2、悔棋");
System.out.println("3、保存");
System.out.println("4、退出");
System.out.println("请输入你的步骤:");
int input = sc.nextInt();
switch (input){
case 1:
System.out.println("请输入你想输入的位置(空格分开):");
int x = sc.nextInt();
int y = sc.nextInt();
if (playChess(x, y, arr)){
count++;
}
break;
case 2:
returnChess(arr);
break;
case 3:
saveBoard(arr);
break;
case 4:
return;
default:
break;
}
showBoard(arr);
}
}
//显示棋盘
public static void showBoard(int[][] arr){
for (int i=0; i < arr.length; i++){
for (int j=0; j < arr[i].length; j++){
System.out.print(arr[i][j]);
System.out.print('\t');
}
System.out.println();
}
}
//下棋
public static boolean playChess(int x,int y,int[][] arr){
if (arr[x][y] !=0 ){
System.out.println("输入位置错误,请重新输入!");
return false;
}
arr[x][y] = count;
return true;
}
//悔棋
public static boolean returnChess(int[][] arr){
if (count == 0){
System.out.println("悔棋失败");
return false;
}
count--;
for (int i=0; i < arr.length; i++){
for (int j=0; j < arr[i].length; j++){
if (arr[i][j] == count){
arr[i][j] = 0;
break;
}
}
}
return true;
}
//将棋盘收缩保存(稀疏数组)
public static void saveBoard(int[][] arr){
int k = 0;
int[][] sparseArray = new int[count-1][3];
for (int i=0; i < arr.length; i++){
for (int j=0; j < arr[i].length; j++){
if (arr[i][j] != 0){
sparseArray[k][0] = i;
sparseArray[k][1] = j;
sparseArray[k][2] = arr[i][j];
k++;
}
}
}
for (int i=0; i < sparseArray.length; i++){
for (int j=0; j < 3; j++){
System.out.print(sparseArray[i][j]);
System.out.print('\t');
}
System.out.println();
}
ChessBoard chessBoard = new ChessBoard();
chessBoard.setCount(count);
chessBoard.setSparseArray(sparseArray);
File file = new File("board");
try {
if (!file.exists()){
file.createNewFile();
}
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
outputStream.writeObject(chessBoard);
} catch (IOException e) {
e.printStackTrace();
}
}
}
最后希望大家一起努力,冲冲冲!