【问题标题】:When using ArrayList<int[][]> how to see if an element is already in the list?使用 ArrayList<int[][]> 时如何查看一个元素是否已经在列表中?
【发布时间】:2014-12-10 21:21:02
【问题描述】:

在这种情况下,使用 ArrayList 库中的 public boolean contains(Object o) 不起作用。考虑

ArrayList<int[][]> test = new ArrayList<>();

int[][] one = {
    {1,2,3},
    {4,5,6}
};
int[][] two = {
    {1,2,3},
    {4,5,6}
};
int[][] three = {
    {9,7,5},
    {1,2,4},
    {5,6,7}
};

test.add(one);
System.out.println(test.contains(one));
System.out.println(test.contains(two));
System.out.println(test.contains(three));

以上代码返回

true
false
false

有没有办法检查两者之间的相等性并确保没有重复的值进入列表?

【问题讨论】:

    标签: java arraylist multidimensional-array


    【解决方案1】:

    我知道的最简单的方法是将其提取到使用 Arrays.deepEquals(Object[], Object[]) 的方法中,例如 -

    public static boolean contains(List<int[][]> al, int[][] toFind) {
        for (int[][] arr : al) {
            if (Arrays.deepEquals(arr, toFind)) {
                return true;
            }
        }
        return false;
    }
    

    然后你可以像这样测试它

    public static void main(String[] args) {
        ArrayList<int[][]> test = new ArrayList<int[][]>();
        int[][] one = { { 1, 2, 3 }, { 4, 5, 6 } };
        int[][] two = { { 1, 2, 3 }, { 4, 5, 6 } };
        int[][] three = { { 9, 7, 5 }, { 1, 2, 4 }, { 5, 6, 7 } };
        test.add(one);
        if (contains(test, two)) {
            System.out.println("Found two");
        }
    }
    

    输出是

    Found two
    

    【讨论】:

    • +1,比起编写包装类的工作,我更喜欢这种简洁的实用方法。
    • 轻松解决了我的问题,并且是一个干净的解决方案。谢谢! +1
    【解决方案2】:

    一种解决方案是将数组包装在一个提供适当equals 实现的类中。

    class SquareArray {
        private int[][] array;
    
        public SquareArray(int[][] array) {
            this.array = array;
        }
    
        public int[][] getArray() {
            return array;
        }
    
        @Override
        public boolean equals(Object o) {
            return (o instanceof SquareArray) && 
                   Arrays.deepEquals(array, ((SquareArray)o).array);
        }
    
        @Override
        public int hashCode() {
            return Arrays.deepHashCode(array);
        }
    
        @Override
        public String toString() {
            return Arrays.deepToString(array);
        }
    }
    

    现在你可以使用List&lt;SquareArray&gt;;例如:

    int[][] a = {{1,2,3}, {4,5,6}};
    int[][] b = {{1,2},{3,4},{5,6}};
    int[][] c = {{1,2,3}, {4,5,6}};
    
    SquareArray x = new SquareArray(a);
    SquareArray y = new SquareArray(b);
    SquareArray z = new SquareArray(c);
    
    List<SquareArray> list = new ArrayList<>();
    list.add(x);
    System.out.println(list.contains(x));
    System.out.println(list.contains(y));
    System.out.println(list.contains(z));
    
    真的 错误的 真的

    参考:

    【讨论】:

      【解决方案3】:

      我想提出另一个使用 Java 8 流和谓词的解决方案:

      Stream#anyMatch 方法可用于检查给定列表是否包含某个元素。可以使用Arrays#deepEquals 简洁地构建所需的谓词:

      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;
      
      public class GenericContainsTest
      {
          public static void main(String[] args)
          {
              List<int[][]> list = new ArrayList<int[][]>();
              int[][] one =
              { { 1, 2, 3 },
                { 4, 5, 6 } };
              int[][] two =
              { { 1, 2, 3 },
                { 4, 5, 6 } };
              list.add(one);
              if (list.stream().anyMatch(e->Arrays.deepEquals(e, two)))
              {
                  System.out.println("Found two");
              }
          }
      }
      

      但是,你提到你的意图是……

      ...确保没有重复的值进入列表

      在这种情况下,您至少应该考虑不使用List,而是使用Set - 特别是使用SquareArray 类的Set&lt;SquareArray&gt; arashajii proposed in his answer

      【讨论】:

        【解决方案4】:

        contains 方法使用 equals(e) 方法,当您在数组上使用 equals 时,它与使用 == 相同,因此您检查的是引用相等,而不是内容。 要检查两个数组是否相等,您必须对嵌套数组使用 Arrays.equals(array1, array2)Arrays.deepEquals(nestedArray1, nestedArray2)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-07-07
          • 1970-01-01
          • 2013-05-06
          • 2013-01-04
          • 1970-01-01
          • 1970-01-01
          • 2023-02-22
          • 1970-01-01
          相关资源
          最近更新 更多