为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

森栏 2023-05-24 17:40:12
我正在开发一个 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}
查看完整描述

2 回答

?
动漫人物

TA贡献1815条经验 获得超10个赞

您已将内环放在外面,将外环放在里面!


里面说:


For each pair x in array one

    For each pair y in array two

        If x and y are equal

            ...

你做了:


For each pair x in array two

    For each pair y in array one

        If x and y are equal

            ...

所以为了让你的代码工作,你只需要以相反的顺序传递参数:


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

或者,我建议这样做,切换循环:


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

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


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

        int[] dictionaryPair = dictionary[dictionaryIndex];


        int indexOf = -1;


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

            int[] pair = lookup[index];

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

                indexOf = index;

                break;

            }

        }


        if (indexOf != -1) {

            result[dictionaryIndex] = indexOf;

        }

    }

    return result;

}


查看完整回答
反对 回复 2023-05-24
?
茅侃侃

TA贡献1842条经验 获得超21个赞

如果您可以创建对象并使用更多内存,那么下一步很容易:


public class Main {


  static class Pair {

    private int[] a;


    Pair(int[] a) {

      this.a = a;

    }


    @Override

    public boolean equals(Object o) {

      if (this == o) return true;

      if (!(o instanceof Pair)) return false;

      Pair pair = (Pair) o;

      return Arrays.equals(a, pair.a);

    }


    @Override

    public int hashCode() {

      return Arrays.hashCode(a);

    }

  }


  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}};


    List<Pair> lookup = Stream.of(arrayTwo).map(Pair::new).collect(Collectors.toList());

    List<Pair> dictionary = Stream.of(arrayOne).map(Pair::new).collect(Collectors.toList());


    List<Integer> result = dictionary.stream().map(lookup::indexOf).collect(Collectors.toList());

    System.out.println(result);

  }


}

创建一个代表每一对的类并实现 equals 和 hashCode 方法,这样我们就可以使用 indexOf 在查找集合中找到所需对的索引。


没有额外的对象:


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

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

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

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

        if (lookup[j][0] == dictionary[i][0] && lookup[j][1] == dictionary[i][1]) {

          result[i] = j;

          break;

        }

      }

    }

    return result;


查看完整回答
反对 回复 2023-05-24
  • 2 回答
  • 0 关注
  • 124 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信