Java 使用ArrayList时<;int[][]>;如何查看列表中是否已存在元素?
在这种情况下,在ArrayList库中使用Java 使用ArrayList时<;int[][]>;如何查看列表中是否已存在元素?,java,arraylist,multidimensional-array,Java,Arraylist,Multidimensional Array,在这种情况下,在ArrayList库中使用公共布尔包含(对象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
公共布尔包含(对象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));
有没有办法检查两者之间是否相等,并确保列表中没有重复的值?一种解决方案是将数组封装在一个类中,该类提供适当的
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);
}
}
现在您将使用一个列表
;例如:
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));
int[]a={{1,2,3},{4,5,6};
int[]b={{1,2},{3,4},{5,6};
int[]c={{1,2,3},{4,5,6};
方形阵列x=新的方形阵列(a);
方形阵列y=新的方形阵列(b);
方形阵列z=新的方形阵列(c);
列表=新的ArrayList();
增加(x);
System.out.println(list.contains(x));
System.out.println(list.contains(y));
System.out.println(list.contains(z));
真的
假的
真的
参考资料:
public static boolean contains(List<int[][]> al, int[][] toFind) {
for (int[][] arr : al) {
if (Arrays.deepEquals(arr, toFind)) {
return true;
}
}
return false;
}
方法使用该方法,当您在数组上使用equals时,它与使用=
是一样的,因此您检查引用的相等性,而不是内容。
要检查两个数组是否相等,必须使用
(array1,array2)
或(nestedArray1,nestedArray2)
来检查嵌套数组。我想提出另一种使用Java 8流和谓词的解决方案:
该方法可用于检查给定列表是否包含某个元素。所需谓词可以使用以下方法简洁地生成:
import java.util.ArrayList;
导入java.util.array;
导入java.util.List;
公共类GenericContainsTest
{
公共静态void main(字符串[]args)
{
列表=新的ArrayList();
int[][]一=
{ { 1, 2, 3 },
{ 4, 5, 6 } };
int[]2=
{ { 1, 2, 3 },
{ 4, 5, 6 } };
列表。添加(一个);
if(list.stream().anyMatch(e->Arrays.deepEquals(e,two)))
{
System.out.println(“找到两个”);
}
}
}
然而,你提到你的意图是 。。。确保列表中没有重复的值
在这种情况下,你至少应该考虑不要使用<代码>列表< />代码,但是<<代码>设置 >特别是,<代码>设置< /C> >使用<代码> SavaReals类。p> +1,与编写包装类相比,我更喜欢这样一个简洁的实用方法。轻松地解决了我的问题,是一个干净的解决方案。谢谢+1. true false true
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
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");
}
}
}