思路一
先找到最长的数组,然后循环短的数组并判断元素是否在长数组中
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
String[] strArr1={"ee","aa","bb","cc"};
String[] strArr2={"aa","bb","cc","dd","44"};
System.out.println(Main.subContain(strArr1,strArr2));
}
public static boolean subContain(String[] strArr1, String[] strArr2) {
if (null == strArr1 || null == strArr2) {
return false;
}
boolean flag = true;
if (strArr2.length > strArr1.length) {
String[] temp;
temp = strArr1;
strArr1 = strArr2;
strArr2 = temp;
}
for (String str : strArr2) {
if (!Arrays.asList(strArr1).contains(str)) {
flag = false;
break;
}
}
return flag;
}
}
思路二
判断两个数组的交集长度是否等于最小的数组长度,用到了第三方的jar包
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
String[] strArr1 = {"aa", "bb", "cc"};
String[] strArr2 = {"aa", "bb", "cc", "dd", "44"};
System.out.println(Main.subContain(strArr1, strArr2));
}
public static boolean subContain(String[] strArr1, String[] strArr2) {
if (null == strArr1 || null == strArr2) {
return false;
}
Collection collection = org.apache.commons.collections.CollectionUtils.intersection(Arrays.asList(strArr1), Arrays.asList(strArr2));
return collection.size() == Math.min(strArr1.length, strArr2.length);
}
}