每天进步一点点,每天一个小算法(数据结构和算法持续更新中)

1、数组和稀疏数组,绝不啰嗦,手敲代码,自己理解

public class Main {
    
    
    public static void main(String[] args) {
    
    
        //数据结构
        //数组与稀疏数组的转换
        //新建一个数组
        int [][] sz = new int[10][10];
        sz[2][6] = 2;
        sz[7][5] = 1;
        sz[5][5] = 2;
        sz[6][6] = 1;
        //其他地方均为0
        System.out.println("这是一个二维数组======================》》》》》》》》》》》");
        for (int[] ints : sz) {
    
    
            for (int anInt : ints) {
    
    
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }
        //将这个二维数组转成稀疏数组
        //1、先求出不为0的值
        int sum = 0;
        int szrow = 0;
        int szcol = 0;
        for (int[] ints : sz) {
    
    
            szrow = sz.length;
            for (int anInt : ints) {
    
    
                szcol = ints.length;
                if (anInt!=0){
    
    
                    sum  += 1;
                }
            }
        }
        //2、创建这个稀疏数组
        int[][] xssz = new int[sum + 1][3];
        //3、设置这个稀疏数组的第一行
        xssz[0][0] = szrow;
        xssz[0][1] = szcol;
        xssz[0][2] = sum;
        //计数使用
        int count =0;
        for (int i = 0; i < sz.length; i++) {
    
    
            for (int j = 0; j < sz[i].length; j++) {
    
    
                if (sz[i][j]!=0){
    
    
                    count = count + 1;
                    xssz[count][0] = i;
                    xssz[count][1] = j;
                    xssz[count][2] = sz[i][j];
                }
            }
        }
        //输出看看是个什么妖怪
        System.out.println("稀疏数组======================》》》》》》》》》》》");
        for (int[] ints : xssz) {
    
    
            for (int anInt : ints) {
    
    
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }
        //数据存盘
        File file = new File("D:\\test\\map.data");
        FileOutputStream outPutStream = new FileOutputStream(file);
        StringBuilder stringBuilder = new StringBuilder();//使用长度可变的字符串对象;
        for (int[] ints : xssz) {
    
    
            for (int anInt : ints) {
    
    
                stringBuilder.append(anInt+"\t");//追加文件内容
            }
            stringBuilder.append("\n");
        }
        String context = stringBuilder.toString();//将可变字符串变为固定长度的字符串,方便下面的转码;
        byte[]  bytes = context.getBytes("UTF-8");//因为中文可能会乱码,这里使用了转码,转成UTF-8;
        outPutStream.write(bytes);//开始写入内容到文件;
        outPutStream.close();//一定要关闭输出流;
    }
}

效果图如下,自己去研究哈,的确少占用了很多空间
在这里插入图片描述
二、环形队列

import java.util.Scanner;

/*环形列表*/
public class CirleArrayDemo {
    
    
    public static void main(String[] args) {
    
    
        //测试一把
        System.out.println("测试数组模拟环形队列的案例~~~");

        // 创建一个环形队列
        CirleArray queue = new CirleArray(5); //说明设置4, 其队列的有效数据最大是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("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);// 接收一个字符
            switch (key) {
    
    
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("输出一个数");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g': // 取出数据
                    try {
    
    
                        int res = queue.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
    
    
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h': // 查看队列头的数据
                    try {
    
    
                        int res = queue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
    
    
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e': // 退出
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~~");


}

    public static class CirleArray{
    
    
        private int maxSize;//表示列表的大小
        private int front; //表示起始值, front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素
        private int real; //队列尾,变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置. 因为希望空出一个空间做为约定.初始值为0
        private int[] arr; //模拟队列

        public CirleArray() {
    
    
        }

        public CirleArray(int arrMaxSize) {
    
    
           this.maxSize = arrMaxSize;
           this.arr = new int [arrMaxSize];
        }

        //判断队列是否满
        public  boolean isFull(){
    
    
            return (real+1) % maxSize==front;
        }

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


        // 添加数据到队列
        public void addQueue(int n) {
    
    
            // 判断队列是否满
            if (isFull()) {
    
    
                System.out.println("队列满,不能加入数据~");
                return;
            }
            //直接将数据加入
            arr[real] = n;
            //将 rear 后移, 这里必须考虑取模
            real = (real + 1) % maxSize;
        }

        // 获取队列的数据, 出队列
        public int getQueue() {
    
    
            // 判断队列是否空
            if (isEmpty()) {
    
    
                // 通过抛出异常
                throw new RuntimeException("队列空,不能取数据");
            }
            // 这里需要分析出 front是指向队列的第一个元素
            // 1. 先把 front 对应的值保留到一个临时变量
            // 2. 将 front 后移, 考虑取模
            // 3. 将临时保存的变量返回
            int value = arr[front];
            front = (front + 1) % maxSize;
            return value;
        }


        // 显示队列的所有数据
        public void showQueue() {
    
    
            // 遍历
            if (isEmpty()) {
    
    
                System.out.println("队列空的,没有数据~~");
                return;
            }
            // 思路:从front开始遍历,遍历多少个元素
            // 动脑筋
            for (int i = front; i < front + size() ; i++) {
    
    
                System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
            }
        }




        // 求出当前队列有效数据的个数
        public int size() {
    
    
            // rear = 2
            // front = 1
            // maxSize = 3
            return (real + maxSize - front) % maxSize;
        }

        // 显示队列的头数据, 注意不是取出数据
        public int headQueue() {
    
    
            // 判断
            if (isEmpty()) {
    
    
                throw new RuntimeException("队列空的,没有数据~~");
            }
            return arr[front];
        }



    }

}

猜你喜欢

转载自blog.csdn.net/qq_38132995/article/details/114606330
今日推荐