Java 如何检查数组是否是二维数组中的元素之一
我试图使用Hamcrest库提供的标准Collection.isInmatcher断言字符串元素数组是二维数组的元素之一。很遗憾,收到以下断言异常:Java 如何检查数组是否是二维数组中的元素之一,java,junit,hamcrest,Java,Junit,Hamcrest,我试图使用Hamcrest库提供的标准Collection.isInmatcher断言字符串元素数组是二维数组的元素之一。很遗憾,收到以下断言异常: java.lang.AssertionError: Expected: one of {["A", "B", "C"], ["A", "B", "C"]} but: was ["A", "B", "C"] 代码: String[][] expected = new String[][] { { "A", "B", "C" }, { "A"
java.lang.AssertionError:
Expected: one of {["A", "B", "C"], ["A", "B", "C"]}
but: was ["A", "B", "C"]
代码:
String[][] expected = new String[][] { { "A", "B", "C" }, { "A", "B", "C" } };
String[] actual = new String[] { "A", "B", "C" };
assertThat(actual, isIn(expected));
我可以以这种方式验证使用hamcrest吗?或者我需要为给定场景创建自己的匹配器吗?您的数组可能包含与
预期中的数组相同的内容,但它不是相同的对象。您的数组可能包含与预期中的数组相同的内容,但是它不是同一个对象。我猜问题是因为该方法比较的是对象,而不是内容。基本上,尽管两者的内容相同,但它们不是同一个对象
改为这样做:
String[]actual=新字符串[]{“A1 C1 E1 F1 J1”、“A1 C1 E1 F1 K1”、“A1 B1 G1 H1”};
字符串[][]预期值=新字符串[][{实际值,{“A1 C1 E1 F1 J1”、“A1 C1 E1 F1 K1”、“A1 B1 G1 H1”}代码>我猜问题是因为该方法比较对象,而不是内容。基本上,尽管两者的内容相同,但它们不是同一个对象
改为这样做:
String[]actual=新字符串[]{“A1 C1 E1 F1 J1”、“A1 C1 E1 F1 K1”、“A1 B1 G1 H1”};
字符串[][]预期值=新字符串[][{实际值,{“A1 C1 E1 F1 J1”、“A1 C1 E1 F1 K1”、“A1 B1 G1 H1”}代码>首先,最好使用列表而不是数组
其次,是的,如果您坚持使用数组,则需要编写自己的“array contains element”函数。您可以使用数组主维度上的循环来实现此函数,调用Arrays.equals()
方法来比较两个一维数组的内容。首先,最好使用List
而不是数组
其次,是的,如果您坚持使用数组,则需要编写自己的“array contains element”函数。您可以使用数组主维度上的循环来实现此函数,调用Arrays.equals()
方法来比较两个一维数组的内容。您上下文中的collection.IsIn问题,列表中的元素是一个数组,它将使用数组#equals来比较每个元素
更具体地说
// It will print false, because Array.equals check the reference
// of objects, not the content
System.out.println(actual.equals(new String[]{"A1 C1 E1 F1 J1", "A1 C1 E1 F1 K1", "A1 B1 G1 H1"}));
因此,我建议创建一个自定义匹配器,使用java中的array.equals。它将为您比较数组的内容。类似下面的代码:
public boolean matches(Object item) {
final String[] actualStringArray = (String [])item;
List<String[]> listOfStringArrays = Arrays.asList(expectedStringMatrix);
for (String[] stringArray : listOfStringArrays) {
// Arrays.equals to compare the contents of two array!
if (Arrays.equals(stringArray, actualStringArray)) {
return true;
}
}
return false;
}
公共布尔匹配(对象项){
最终字符串[]actualStringArray=(字符串[])项;
List listOfStringArrays=Arrays.asList(expectedStringMatrix);
对于(字符串[]stringArray:ListofStringArray){
//Arrays.equals比较两个数组的内容!
if(Arrays.equals(stringArray,actualStringArray)){
返回true;
}
}
返回false;
}
在上下文中,collection.IsIn的问题在于列表中的元素是一个数组,它将使用数组#equals来比较每个元素
更具体地说
// It will print false, because Array.equals check the reference
// of objects, not the content
System.out.println(actual.equals(new String[]{"A1 C1 E1 F1 J1", "A1 C1 E1 F1 K1", "A1 B1 G1 H1"}));
因此,我建议创建一个自定义匹配器,使用java中的array.equals。它将为您比较数组的内容。类似下面的代码:
public boolean matches(Object item) {
final String[] actualStringArray = (String [])item;
List<String[]> listOfStringArrays = Arrays.asList(expectedStringMatrix);
for (String[] stringArray : listOfStringArrays) {
// Arrays.equals to compare the contents of two array!
if (Arrays.equals(stringArray, actualStringArray)) {
return true;
}
}
return false;
}
公共布尔匹配(对象项){
最终字符串[]actualStringArray=(字符串[])项;
List listOfStringArrays=Arrays.asList(expectedStringMatrix);
对于(字符串[]stringArray:ListofStringArray){
//Arrays.equals比较两个数组的内容!
if(Arrays.equals(stringArray,actualStringArray)){
返回true;
}
}
返回false;
}
问题在于Object.equals()
在对象为数组时没有达到预期效果。您可能已经知道,必须使用array.equals()
——但是HamcrestisIn()
不允许这样做
可能最简单的解决方案是转换为List
,即使只是为了测试——因为List.equals()
的工作方式与Hamcrest预期的一样:
...
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.collection.IsIn.in;
...
String[][] expected = new String[][] { { "A", "B", "C" }, { "A", "B", "C" } };
Object[] expectedLists = Arrays.stream(expected).map(Arrays::asList).toArray();
String[] actual = new String[] { "A", "B", "C" };
assertThat(Arrays.asList(actual), is(in(expectedLists)));
问题是当对象是数组时,Object.equals()
并没有达到您所期望的效果。您可能已经知道,必须使用array.equals()
——但是HamcrestisIn()
不允许这样做
可能最简单的解决方案是转换为List
,即使只是为了测试——因为List.equals()
的工作方式与Hamcrest预期的一样:
...
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.collection.IsIn.in;
...
String[][] expected = new String[][] { { "A", "B", "C" }, { "A", "B", "C" } };
Object[] expectedLists = Arrays.stream(expected).map(Arrays::asList).toArray();
String[] actual = new String[] { "A", "B", "C" };
assertThat(Arrays.asList(actual), is(in(expectedLists)));
我通过替换较短的字符串使问题更容易阅读。它不会影响问题或答案。我通过替换较短的字符串使问题更容易阅读。它不会影响问题或答案。感谢您为hamcrest提供基于列表的解决方案。感谢您为hamcrest提供基于列表的解决方案。