[英]Comparing 2D arrays of strings
I have a method which generates a 2D array of strings, then places them in a ArrayList
. 我有一个方法,该方法生成一个2D字符串数组,然后将它们放置在
ArrayList
。 I am trying to write a method which take a new 2D array and check it against those already in the array for duplication. 我正在尝试编写一种方法,该方法采用新的2D数组,并对照数组中已有的2D数组进行检查以进行复制。 This is what I have so far, it should return
true
for a new solution but it returns false
every time. 这就是我到目前为止的内容,对于新的解决方案,它应该返回
true
,但是每次都返回false
。 The calling method prints the arrays out and I can see that they are not the same. 调用方法将数组打印出来,我可以看到它们不相同。
public static boolean tester(String[][] b, ArrayList<String[][]> s)
{
if (s.size() == 1) return true;
boolean n = true;
int count;
int size = b.length * b[0].length;
for (int i = 0; n && i < s.size() -1; i ++)
{
count = 0;
for (int j = 0; j < s.get(i).length; j ++)
for (int k = 0; k < s.get(i)[0].length; k ++)
if (s.get(i)[j][k].equals(b[j][k])) count ++;
if (count == size) n = false;
}
return n;
}
Your code can get way simpler by using Arrays.deepEquals : 您的代码可以通过获取方式简单Arrays.deepEquals :
public static boolean nonDuplicate(ArrayList<String[][]> s, String[][] b){
for(String[][] e : s){
if(Arrays.deepEquals(e, b)) return false; //e and b are duplicates
}
return true;
}
Your code may be incorrect because you didn't use braces ( {}
) around your for loops. 您的代码可能不正确,因为您没有在for循环中使用大括号(
{}
)。 So when you wrote: 所以当你写:
for (int j = 0; j < s.get(i).length; j ++)
for (int k = 0; k < s.get(i)[0].length; k ++)
if (s.get(i)[j][k].equals(b[j][k])) count ++;
if (count == size) n = false;
It was interpreted as: 它被解释为:
for (int j = 0; j < s.get(i).length; j ++){
for (int k = 0; k < s.get(i)[0].length; k ++){
if (s.get(i)[j][k].equals(b[j][k])) count ++;
}
}
if (count == size) n = false;
Which was likely (?) not your intention judging by your indentation, though notably white space (including indentation) doesn't mean very much in java aside from separating tokens. 用缩进来判断,这可能不是您的意图,尽管值得注意的是,空格(包括缩进)在Java中除了分隔标记外没有多大意义。
This is how your code gets interpreted by the compiler: 这是编译器解释代码的方式:
for (int i = 0; n && i < s.size() -1; i ++)
{
count = 0;
for (int j = 0; j < s.get(i).length; j ++) {
for (int k = 0; k < s.get(i)[0].length; k ++) {
if (s.get(i)[j][k].equals(b[j][k])) count ++;
}
}
if (count == size) n = false;
}
Imho its best to always use {
and }
to clarify your intentions to the compiler/reader of your code. 恕我直言,最好始终使用
{
和}
向代码的编译器/阅读器阐明您的意图。
I guess what you wanted is this: 我想你想要的是:
for (int i = 0; n && i < s.size() -1; i ++)
{
count = 0;
for (int j = 0; j < s.get(i).length; j ++) {
for (int k = 0; k < s.get(i)[0].length; k ++) {
if (s.get(i)[j][k].equals(b[j][k])) {
count ++;
}
if (count == size) {
n = false;
}
}
}
}
But you could also write: 但您也可以这样写:
public static boolean tester(String[][] b, ArrayList<String[][]> s)
{
// ..
for (int i = 0; n && i < s.size() -1; i ++) {
for (int j = 0; j < s.get(i).length; j ++) {
for (int k = 0; k < s.get(i)[0].length; k ++) {
if (s.get(i)[j][k].equals(b[j][k]) == false) {
return true;
}
}
}
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.