All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.utils.ArrayUtils Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
package com.github.dennisit.vplus.data.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayUtils {


    /**
     * 原始数组转换为稀疏数组
     *
     * @param arr 原始数组
     * @return 原始数字对应的稀疏数组
     */
    public static int[][] sparseCompress(int[][] arr) {
        int row = arr.length, col = arr[0].length;

        // 标记不为空的元素
        List list = new ArrayList<>();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (arr[i][j] != 0) {
                    list.add(new ArrItem(i, j, arr[i][j]));
                }
            }
        }

        // 定义稀疏数组长度
        int[][] tar = new int[list.size() + 1][3];
        int index = 0;
        for (int i = 0; i < tar.length; i++) {
            // 稀疏数组第一行存储元素数组的行、列、非零元素总个数
            if (i == 0) {
                tar[i][0] = row;
                tar[i][1] = col;
                tar[i][2] = list.size();
                continue;
            }
            // 稀疏数组第>=2行存储原始数组非零元素信息(行、列、元素值)
            ArrItem item = list.get(index++);
            tar[i][0] = item.getI();
            tar[i][1] = item.getJ();
            tar[i][2] = item.getVal();
        }

        // 返回压缩后的稀疏数组
        return tar;
    }

    /**
     * 稀疏数组转换为原始数组
     *
     * @param arr 稀疏数组
     * @return 复员后的目标数组
     */
    public static int[][] sparseRecovery(int[][] arr) {
        // 稀疏数组第一行存储原始数组的:总行数、总列数、非空元素总数
        int[][] tar = new int[arr[0][0]][arr[0][1]];
        // 从稀疏数组的第二行开始复原数组的元素,
        for (int i = 1; i < arr.length; i++) {
            int row = arr[i][0];
            int col = arr[i][1];
            int val = arr[i][2];
            tar[row][col] = val;
        }
        return tar;
    }

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

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ArrItem implements Serializable {

        // 元素横坐标
        private int i;

        // 元素纵坐标
        private int j;

        // 数组元素值
        private int val;

    }


    public static void main(String[] args) {
        String expression = "11 + ((2 + 3) * 14) - 5";
        List suffixExpression = toSuffixExpression(toExpressionList(expression));
        suffixExpression.toArray();
        System.out.println(suffixExpression);
    }

    public static List toSuffixExpression(List coll) {
        return coll;
    }

    /**
     * 将中缀表达式转换成表达式集合
     * 例如中缀表达式: "11 + ((2 + 3) * 14) - 5" 转换后 "[11, +, (, (, 2, +, 3, ), *, 14, ), -, 5]"
     *
     * @param source 表达式串
     * @return 中缀表达式集合
     */
    public static List toExpressionList(String source) {
        List list = new ArrayList<>();
        int k = 0;
        while (k < source.length()) {
            Character c = source.charAt(k);
            // 如果不是数字
            if (!Character.isDigit(c)) {
                if (c != ' ') {
                    list.add(String.valueOf(c));
                }
                k++;
            } else {
                String temp = "";
                while (k < source.length() && Character.isDigit(source.charAt(k))) {
                    temp += source.charAt(k);
                    k++;
                }
                list.add(temp);
            }
        }
        return list;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy