java 从交错数组和多维数组中打印两个数组之间的公共元素

nwsw7zdq  于 2023-10-14  发布在  Java
关注(0)|答案(2)|浏览(81)
int[][] arr1 = new int[][] {{1,2,1},{9,7,2},{7,3,6}};

    int arr2[][] = new int[][] {{2,6,8},{0,1,7},{7,2,0},{8,3}};

    boolean duplicate= false;

    for(int i=0;i<arr1.length;i++) { 
        for(int j=0;j<arr1[i].length;j++) {
            int transpose = 0;
            transpose = arr1[i][j];

            for(int k=0;k<arr2.length;k++) {
                duplicate = false; 
                for(int  l=0;l<arr2[k].length;l++) { 
                    if(transpose == arr2[k][l]) {
                        System.out.println(transpose);
                        duplicate = true;
                        break;
                    }
                }
            
                if(duplicate)
                    break;
            }
        }

    }

}

}
我试着不使用任何快捷方式,只使用for循环和if语句逻辑地打印常见元素。逻辑是,在数组1中,我迭代每个元素,并检查它是否在数组2中,如果它在那里,我们必须在控制台中打印它。但问题是数组1中的元素“1,2,7”迭代了2次并打印了2次。
得到这个输出:1 2 1 7 2 7 3 6
预期输出:1 2 7 3 6
在这里,我必须停止迭代两次,因为一旦打印出来就已经检查过了。注意:我只想回答循环语句和条件语句没有散列集或快捷方式。

qv7cva1a

qv7cva1a1#

你需要为每个 array 执行一个 loop,然后为每个 sub-array 执行一个 loop
此外,我在这里使用 * Set * 来防止重复值。

Set<Integer> d = new TreeSet<>();
int i, ni, j, nj;
for (int[] x : arr1)
    for (i = 0, ni = x.length; i < ni; i++)
        for (int[] y : arr2)
            for (j = 0, nj = y.length; j < nj; j++)
                if (x[i] == y[j]) d.add(x[i]);

输出

[1, 2, 3, 6, 7]

理想情况下,只需将每个值添加到 * List * 中,首先检查 List

List<Integer> d = new ArrayList<>();
int i, ni, j, nj;
for (int[] x : arr1)
    for (i = 0, ni = x.length; i < ni; i++)
        for (int[] y : arr2)
            for (j = 0, nj = y.length; j < nj; j++)
                if (x[i] == y[j] && !d.contains(x[i])) d.add(x[i]);
hvvq6cgz

hvvq6cgz2#

  • 注意:此解决方案假设我们必须仅使用原始类型,循环和if/else语句 *
import java.util.*;

public class Main {
  public static void main(String[] args) {
    // Note: If not limited to primitive types, loops, and if statements.
    //  A better option would be using a Set<int>. It would be a similar process
    //  to the below solution, but far more efficient as insertion and retrieval
    //  of values would be O(1) instead of O(n).

    // when limited to using only primitives, arrays, and if statements
    int[][] arr1 = new int[][] {{1,2,1},{9,7,2},{7,3,6}};
    int[][] arr2 = new int[][] {{2,6,8},{0,1,7},{7,2,0},{8,3}};

    // find one of the array's flattened size
    int seenSize = 0;
    for (int i = 0; i < arr1.length; i++) {
      for (int j = 0; j < arr1[i].length; j ++) {
        seenSize++;
      }
    }
    int[] seen = new int[seenSize];

    // iterate over the same array to create an array of any unique seen element
    int seenIndex = 0;
    for (int i = 0; i < arr1.length; i++) {
      for (int j = 0; j < arr1[i].length; j++) {
        if (!valueIsPresentIn(arr1[i][j], seen, seenIndex)) {
          seen[seenIndex] = arr1[i][j];
          seenIndex++;
        }
      }
    }

    // Note: sorting the list of seen items would make this next step more efficient, then binary search could be used.

    int[] printed = new int[seenSize];
    int printedIndex = 0;

    // iterate over the second array searching for a similar occurrence in the list of
    // seen values, if something is found and it has not been printed, we need to print
    // it and add to the list of already printed values
    for (int i = 0; i < arr2.length; i++) {
      for (int j = 0; j < arr2[i].length; j++) {
        if (valueIsPresentIn(arr2[i][j], seen, seenIndex)) {
          if (!valueIsPresentIn(arr2[i][j], printed, printedIndex)) {
            printed[printedIndex] = arr2[i][j];
            System.out.println(printed[printedIndex]);
            printedIndex++;
          }
        }
      }
    }
  }

  // Method to search for a value, this is unnecessary if no longer limited to
  // primitive types as there are built-in methods and countless libraries/solutions
  // to this.
  public static boolean valueIsPresentIn(int value, int[] array, int maxIndex) {
    for (int searchIndex = 0; searchIndex < maxIndex; searchIndex++) {
      if (array[searchIndex] == value) {
        return true;
      }
    }

    return false;
  }
}

相关问题