根据 int 数组中的数据填充二维整数数组

我有一个 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));

                }

            }

        }

这里有什么帮助吗?谢谢


MYYA
浏览 41回答 1
1回答

慕工程0101907

如果数据位于arrays中,如问题中指定的,您的代码应该是:int[][] otherOptions = new int[neighbors.length][];for (int nodeIdx = 0; nodeIdx < neighbors.length; nodeIdx++) {&nbsp; &nbsp; otherOptions[nodeIdx] = new int[neighbors[nodeIdx].length - 1];&nbsp; &nbsp; for (int i = 0, j = 0; i < neighbors[nodeIdx].length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (neighbors[nodeIdx][i] != preliminaryAssignments[nodeIdx]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; otherOptions[nodeIdx][j++] = neighbors[nodeIdx][i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}测试int[] preliminaryAssignments = {6, 7, 7, 7};int[][] neighbors = {{5, 6}, {5, 7, 8, 9}, {5, 7, 9}, {5, 7, 8, 9}};// code from above hereSystem.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++) {&nbsp; &nbsp; List<Integer> others = new ArrayList<>(neighbors.get(nodeIdx));&nbsp; &nbsp; others.remove(preliminaryAssignments.get(nodeIdx));&nbsp; &nbsp; otherOptions.add(others);}测试List<Integer> preliminaryAssignments = Arrays.asList(6, 7, 7, 7);List<List<Integer>> neighbors = Arrays.asList(Arrays.asList(5, 6),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(5, 7, 8, 9),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(5, 7, 9),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(5, 7, 8, 9));// code from above hereSystem.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(); ) {&nbsp; &nbsp; List<Integer> others = new ArrayList<>(neighborIter.next());&nbsp; &nbsp; others.remove(prelimIter.next());&nbsp; &nbsp; otherOptions.add(others);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java