第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

是否有一個 Java 函數(shù)可以將有序?qū)?shù)組轉(zhuǎn)換為整數(shù)數(shù)組?

是否有一個 Java 函數(shù)可以將有序?qū)?shù)組轉(zhuǎn)換為整數(shù)數(shù)組?

森欄 2023-05-24 17:40:12
我正在開發(fā)一個 Java 項目,我必須將有序?qū)Φ亩S數(shù)組轉(zhuǎn)換為整數(shù)數(shù)組。為了闡明我需要什么,請將以下數(shù)組作為示例:int [][] arrayUno = {{0,1},{1,0},{2,1},{2,2},{1,1},{1,2},{0,2},{2,0},{0,0}}假設(shè)我們有另一個相同長度的數(shù)組:int [][] arrayDos = {{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}}每個有序?qū)υ诿總€數(shù)組中都是唯一的(表示作業(yè)/機器的特定組合,即 {0,2} 是作業(yè) 0 在機器 2 中的操作)。我想要 arrayDos 中 arrayUno 的每個元素(有序?qū)Γ┑奈恢?。結(jié)果必須是:{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條經(jīng)驗 獲得超10個贊

您已將內(nèi)環(huán)放在外面,將外環(huán)放在里面!


里面說:


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

            ...

所以為了讓你的代碼工作,你只需要以相反的順序傳遞參數(shù):


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

或者,我建議這樣做,切換循環(huán):


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;

}


查看完整回答
反對 回復(fù) 2023-05-24
?
茅侃侃

TA貢獻1842條經(jīng)驗 獲得超21個贊

如果您可以創(chuàng)建對象并使用更多內(nèi)存,那么下一步很容易:


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

  }


}

創(chuàng)建一個代表每一對的類并實現(xiàn) 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;


查看完整回答
反對 回復(fù) 2023-05-24
  • 2 回答
  • 0 關(guān)注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號