gpt4 book ai didi

java - 在 Java 中比较二维数组和一维数组

转载 作者:行者123 更新时间:2023-12-03 18:41:48 24 4
gpt4 key购买 nike

伙计们,我对使用 java 有点陌生,我正在尝试编写一个程序来检查二维数组是否包含一维数组的值。第二个数组就像一个数字列表,它将检查如果它们匹配,则为第一个数组。

array1[6]= {"a","b","c","d","e","f"}
array2[1][4]={{"a","b","c","d"}{"d","e","f","g"}}
array2[0]= rowcomplete ; // because it contain all the value a,b,c,d
array2[1]= incomplete; // because it only match d,e,f but not g

这是我的代码:

String array1[] = {"a","b","c","d","e","f"};
String array2[][] = {{"a","b","c","d"}, {"d","e","f","g"}};

for (int 2row = 0; 2row < array2.length; 2row++) {
for (int 2column = 0;2column< array2[2row].length;2column++) {
for(int 1row=0; 1row < array1[1row].length();1row++) {
if (array2[2row][2column].equals(array1[1row])) {
System.out.println("complete");
}
else{

}
}
}
}

最佳答案

一个简单的方法是使用 Arrays 类将数组转换为 List 并使用 containsAll 方法,如下所示:

  String array1 []={"a","b","c","d","e","f"};
String array2 [][] ={{"a","b","c","d"},{"d","e","f","g"}};

List<String> array1AsList = Arrays.asList(array1);

for (int i = 0; i < array2.length; i++) {
List<String> array2rowAsList = Arrays.asList(array2[i]);

if(array1AsList.containsAll(array2rowAsList)){
System.out.println("row " + i + " is complete");
}
}

关于java - 在 Java 中比较二维数组和一维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8015953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com