用一个棋盘游戏展示稀疏数组的作用

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();
        }

    }
}

最后希望大家一起努力,冲冲冲!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值