欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA動態(tài)維度笛卡爾積輸出的實(shí)現(xiàn)

 更新時間:2024年02月18日 09:35:37   作者:118路司機(jī)  
本文主要介紹了JAVA動態(tài)維度笛卡爾積輸出的實(shí)現(xiàn),通過動態(tài)生成笛卡爾積,可以方便地處理多維數(shù)據(jù)集,提高數(shù)據(jù)處理效率,具有一定的參考價值,感興趣的可以了解一下

需求

有若干個數(shù)據(jù)序列,把這些數(shù)據(jù)序列的組合全部遍歷輸出,比如提供
[“A”, “B”, “C”],[“I”, “II”], [“1”, “2”, “3”, “4”] 三個序列,能夠輸出以下結(jié)果:

A-I-1
A-I-2
A-I-3
A-I-4

C-II-2
C-II-3
C-II-4

思路

如果是是固定維度,有幾個維度,for循環(huán)幾遍即可,但是如果是不定序列,則遍歷的時候需要指定前置維度和后置維度進(jìn)行計算從而遍歷到所有的數(shù)據(jù)。

public void test() {
        // 定義數(shù)據(jù)
        List<List<String>> datList = new ArrayList<>();
        datList.add(List.of("A", "B", "C"));
        datList.add(List.of("I", "II"));
        datList.add(List.of("1", "2", "3", "4"));

        // 定義總行數(shù)
        int row = datList.stream().map(List::size).reduce((a, b) -> a * b).get();

        // 定義總列數(shù)
        int size = datList.size();

        // 定義數(shù)據(jù)矩陣
        Object[][] matrix = new Object[row][size];

        // 前置維度
        int prevDim = 1;

        // 遍歷所有維度數(shù)據(jù)
        for (int i = 0; i < datList.size(); i++) {

            // 當(dāng)前數(shù)據(jù)
            List<String> currDat = datList.get(i);

            // 當(dāng)前數(shù)據(jù)長度
            int currDatLen = currDat.size();

            // 當(dāng)前維度
            int currDim = row / prevDim;

            // 后置維度
            int nextDim = currDim / currDatLen;

            // 根據(jù)前后維度進(jìn)行矩陣填充
            for (int j = 0; j < currDatLen; j++) {
                Object data = currDat.get(j);
                for (int k = 0; k < prevDim; k++) {
                    for (int m = 0; m < nextDim; m++) {
                        matrix[currDim * k + nextDim * j + m][i] = data;
                    }
                }
            }
            prevDim = prevDim * currDatLen;
        }

        // 輸出結(jié)果
        for (int i = 0; i < matrix.length; i++) {
            System.out.println(String.join("-", Arrays.stream(matrix[i]).map(Object::toString).collect(Collectors.toList())));
        }
    }

輸出結(jié)果

A-I-1
A-I-2
A-I-3
A-I-4
A-II-1
A-II-2
A-II-3
A-II-4
B-I-1
B-I-2
B-I-3
B-I-4
B-II-1
B-II-2
B-II-3
B-II-4
C-I-1
C-I-2
C-I-3
C-I-4
C-II-1
C-II-2
C-II-3
C-II-4

 到此這篇關(guān)于JAVA動態(tài)維度笛卡爾積輸出的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)JAVA動態(tài)笛卡爾積內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論