亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據 int 數組中的數據填充二維整數數組

根據 int 數組中的數據填充二維整數數組

MYYA 2024-01-05 15:20:56
我有一個 int 數組preliminaryAssignments = [6,7,7,7],其中每個索引都是與不同節點配對的節點。即,節點 0 與 6 配對,節點 1-4 與 7 配對,并且二維數組的鄰居 = [[5, 6], [5, 7, 8, 9], [5, 7, 9], [5, 7, 8, 9]] 表示每個索引的所有可能的節點配對。即節點 0 可以與 5 或 5 配對,節點 1 可以與 5、7、8、9 等配對。我想為每個節點的未配對的備用選項創建一個二維整數數組“otherOptions”。即 [[5],[5,8,9],[5,9],[5,8,9]]我在填充 otherOptions 時遇到問題。這是我一直在研究的一些代碼。ArrayList<ArrayList<Integer>> otherOptions = new ArrayList<ArrayList<Integer>>(n-1);        for (int j = 0; j < n-1; j++) {            otherOptions.add(new ArrayList<Integer>());        }        for (int x = 0; x < n-1; x++ ) {            for (int y = 0; y< k; y++) {                if (neighbors.get(x).get(y) != preliminaryAssignment[x]) {                    otherOptions.get(x).add(neighbors.get(x).get(y));                }            }        }這里有什么幫助嗎?謝謝
查看完整描述

1 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

如果數據位于arrays中,如問題中指定的,您的代碼應該是:


int[][] otherOptions = new int[neighbors.length][];

for (int nodeIdx = 0; nodeIdx < neighbors.length; nodeIdx++) {

    otherOptions[nodeIdx] = new int[neighbors[nodeIdx].length - 1];

    for (int i = 0, j = 0; i < neighbors[nodeIdx].length; i++) {

        if (neighbors[nodeIdx][i] != preliminaryAssignments[nodeIdx]) {

            otherOptions[nodeIdx][j++] = neighbors[nodeIdx][i];

        }

    }

}

測試


int[] preliminaryAssignments = {6, 7, 7, 7};

int[][] neighbors = {{5, 6}, {5, 7, 8, 9}, {5, 7, 9}, {5, 7, 8, 9}};

// code from above here

System.out.println(Arrays.deepToString(otherOptions));

輸出


[[5], [5, 8, 9], [5, 9], [5, 8, 9]]

如果數據位于lists中,就像問題代碼中使用的那樣,您的代碼應該是:


List<List<Integer>> otherOptions = new ArrayList<>();

for (int nodeIdx = 0; nodeIdx < neighbors.size(); nodeIdx++) {

    List<Integer> others = new ArrayList<>(neighbors.get(nodeIdx));

    others.remove(preliminaryAssignments.get(nodeIdx));

    otherOptions.add(others);

}

測試


List<Integer> preliminaryAssignments = Arrays.asList(6, 7, 7, 7);

List<List<Integer>> neighbors = Arrays.asList(Arrays.asList(5, 6),

                                              Arrays.asList(5, 7, 8, 9),

                                              Arrays.asList(5, 7, 9),

                                              Arrays.asList(5, 7, 8, 9));

// code from above here

System.out.println(otherOptions);

輸出


[[5], [5, 8, 9], [5, 9], [5, 8, 9]]

如果數據位于未知類型的列表中,即get(int)可能很慢,您的代碼應該是:


List<List<Integer>> otherOptions = new ArrayList<>();

Iterator<Integer> prelimIter = preliminaryAssignments.iterator();

for (Iterator<List<Integer>> neighborIter = neighbors.iterator(); neighborIter.hasNext(); ) {

    List<Integer> others = new ArrayList<>(neighborIter.next());

    others.remove(prelimIter.next());

    otherOptions.add(others);

}


查看完整回答
反對 回復 2024-01-05
  • 1 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號