是否有一个 Java 函数可以将有序对数组转换为整数数组?

我正在开发一个 Java 项目,我必须将有序对的二维数组转换为整数数组。

为了阐明我需要什么,请将以下数组作为示例:

int [][] arrayUno = {{0,1},{1,0},{2,1},{2,2},{1,1},{1,2},{0,2},{2,0},{0,0}}

假设我们有另一个相同长度的数组:

int [][] arrayDos = {{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}}

每个有序对在每个数组中都是唯一的(表示作业/机器的特定组合,即 {0,2} 是作业 0 在机器 2 中的操作)。

我想要 arrayDos 中 arrayUno 的每个元素(有序对)的位置。结果必须是:

{2,4,8,9,5,6,3,7,1}

例如arrayUno({0,1})的第一个元素在arrayDos的2°位置;arrayUno的元素{1,0}在arrayDos的4°位置;arrayUno的元素{2,1}在arrayDos的8°位置,依此类推。

import java.util.Arrays;

public class OrderedPair {


    public static void main(String[] args) {

        int[][] arrayOne = {{0, 1}, {1, 0}, {2, 1}, {2, 2}, {1, 1}, {1, 2}, {0, 0}, {2, 0}, {0, 2}};


        int[][] arrayTwo = {{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}};


        OrderedPair pair = new OrderedPair();


        int[] transformed = pair.transform(arrayOne, arrayTwo);


        System.out.println(Arrays.toString(transformed));

    }


    private int[] transform(int[][] dictionary, int[][] lookup) {

        int[] result = new int[dictionary.length];


        for (int index = 0; index < lookup.length; index++) {

            int[] pair = lookup[index];


            int indexOf = -1;


            for (int dictionaryIndex = 0; dictionaryIndex < dictionary.length; dictionaryIndex++) {

                int[] dictionaryPair = dictionary[dictionaryIndex];


                if (dictionaryPair[0] == pair[0] && dictionaryPair[1] == pair[1]) {

                    indexOf = dictionaryIndex;

                    break;

                }

            }


            if (indexOf != -1) {

                result[index] = indexOf;

            }

        }

        return result;

    }

}

我期望输出:{2,4,8,9,5,6,3,7,1}


但输出是:{8,0,6,1,4,5,7,2,3}


森栏
浏览 167回答 2
2回答

动漫人物

您已将内环放在外面,将外环放在里面!里面说:For each pair x in array one&nbsp; &nbsp; For each pair y in array two&nbsp; &nbsp; &nbsp; &nbsp; If x and y are equal&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...你做了:For each pair x in array two&nbsp; &nbsp; For each pair y in array one&nbsp; &nbsp; &nbsp; &nbsp; If x and y are equal&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...所以为了让你的代码工作,你只需要以相反的顺序传递参数:int[] transformed = pair.transform(arrayTwo, arrayOne);或者,我建议这样做,切换循环:private int[] transform(int[][] dictionary, int[][] lookup) {&nbsp; &nbsp; int[] result = new int[dictionary.length];&nbsp; &nbsp; for (int dictionaryIndex = 0; dictionaryIndex < dictionary.length; dictionaryIndex++) {&nbsp; &nbsp; &nbsp; &nbsp; int[] dictionaryPair = dictionary[dictionaryIndex];&nbsp; &nbsp; &nbsp; &nbsp; int indexOf = -1;&nbsp; &nbsp; &nbsp; &nbsp; for (int index = 0; index < lookup.length; index++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] pair = lookup[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dictionaryPair[0] == pair[0] && dictionaryPair[1] == pair[1]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indexOf = index;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (indexOf != -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[dictionaryIndex] = indexOf;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result;}

茅侃侃

如果您可以创建对象并使用更多内存,那么下一步很容易:public class Main {&nbsp; static class Pair {&nbsp; &nbsp; private int[] a;&nbsp; &nbsp; Pair(int[] a) {&nbsp; &nbsp; &nbsp; this.a = a;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean equals(Object o) {&nbsp; &nbsp; &nbsp; if (this == o) return true;&nbsp; &nbsp; &nbsp; if (!(o instanceof Pair)) return false;&nbsp; &nbsp; &nbsp; Pair pair = (Pair) o;&nbsp; &nbsp; &nbsp; return Arrays.equals(a, pair.a);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int hashCode() {&nbsp; &nbsp; &nbsp; return Arrays.hashCode(a);&nbsp; &nbsp; }&nbsp; }&nbsp; public static void main(String[] args) {&nbsp; &nbsp; int[][] arrayOne = {{0, 1}, {1, 0}, {2, 1}, {2, 2}, {1, 1}, {1, 2}, {0, 0}, {2, 0}, {0, 2}};&nbsp; &nbsp; int[][] arrayTwo = {{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}};&nbsp; &nbsp; List<Pair> lookup = Stream.of(arrayTwo).map(Pair::new).collect(Collectors.toList());&nbsp; &nbsp; List<Pair> dictionary = Stream.of(arrayOne).map(Pair::new).collect(Collectors.toList());&nbsp; &nbsp; List<Integer> result = dictionary.stream().map(lookup::indexOf).collect(Collectors.toList());&nbsp; &nbsp; System.out.println(result);&nbsp; }}创建一个代表每一对的类并实现 equals 和 hashCode 方法,这样我们就可以使用 indexOf 在查找集合中找到所需对的索引。没有额外的对象:private int[] transform(int[][] dictionary, int[][] lookup) {&nbsp; &nbsp; int[] result = new int[dictionary.length];&nbsp; &nbsp; for (int i = 0; i < dictionary.length; i++) {&nbsp; &nbsp; &nbsp; for (int j = 0; j < lookup.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (lookup[j][0] == dictionary[i][0] && lookup[j][1] == dictionary[i][1]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[i] = j;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java