如果可以在array2 中找到array1 的元素,我一直在嘗試解決通過array1 搜索以回傳布林值的問題。但是,只有當 array2 包含相同 order 中的array1 元素時,它才能成立。
例如,array1 = {3,4,5,6}并且array2 = {3, 4, 1, 2, 7, 5, 6}將是true因為所有元素都以相同的順序找到。
到目前為止,這是我的代碼:
int size = Queue1.size();
for(int i = 0; i < size; i ) {
arr3[i] = Queue1.dequeue();
}
boolean test = Arrays.asList(arr2).containsAll(Arrays.asList(arr3));
System.out.println(test);
變數 test 回傳false為
arr3 = {3,4,5,6}arr2 = {3,4,2,1,5,10,9,8,6,7}
uj5u.com熱心網友回復:
您使用的是正確的方法,在使用Arrays.asList時有一點需要注意,請看:
如果將int[]陣列傳遞給asList(),它將回傳List<int[]>而不是Integer 串列。
但是,如果您將Integer[]陣列傳遞給asList()它將回傳一個Integer 串列,您將能夠對其進行比較。
看看代碼是否更清楚:
public static void main(String[] args) {
Integer[] arr1 = {1, 2, 3, 4};
Integer[] arr2 = {1, 2, 3, 4, 5};
boolean wrappers = checkWrappers(arr2, arr1);
System.out.println(wrappers); // true
int[] err1 = {1, 2, 3, 4};
int[] err2 = {1, 2, 3, 4, 5};
boolean primitives = checkPrimitives(err2, err1);
System.out.println(primitives); // false
}
public static boolean checkWrappers(Integer[] outer, Integer[] inner) {
List<Integer> integers2 = Arrays.asList(outer);
List<Integer> integers1 = Arrays.asList(inner);
return integers2.containsAll(integers1);
}
public static boolean checkPrimitives(int[] outer, int[] inner) {
List<int[]> ints2 = Arrays.asList(outer);
List<int[]> ints1 = Arrays.asList(inner);
return ints2.containsAll(ints1); // compare object references not content
}
傳遞原語時,它會比較 int[]物件 參考,而不是像您想要的那樣比較每個元素。
uj5u.com熱心網友回復:
這是一個簡單的演算法來解決您的問題,基于List<Integer> contains和indexOf功能:
代碼(with comments):
public static void main(String[] args) {
Integer[] array1 = new Integer[]{3,4,5,6};
Integer[] array2 = new Integer[]{3, 4, 1, 2, 7, 5, 6};
List<Integer> list1 = Arrays.asList(array1);
List<Integer> list2 = Arrays.asList(array2);
boolean check = false;
int i = 0;
for (Integer n : list1) {
if (i == 0) {
if (list2.contains(n)) {// first element check if it's on the second array
check = true;
}else check = false;
}else {
// check if the element in the array2 and its index is > of the previous element
if (list2.contains(n) && list2.indexOf(n) > list2.indexOf(list1.get(i-1))) {
check = true;
}else {
check = false;
break;
}
}
i ;
}
System.out.println(check);
}
結果:
input 1:
Integer[] array1 = new Integer[]{3,4,5,6};
Integer[] array2 = new Integer[]{3, 4, 1, 2, 7, 5, 6};
output 1: true
input 2:
Integer[] array1 = new Integer[]{3,7,4,6};
Integer[] array2 = new Integer[]{3, 4, 1, 2, 7, 5, 6};
output 2: false
uj5u.com熱心網友回復:
兩個簡單的解決方案。
兩者的想法是查看第一個串列中的每個元素 x 是否在第二個串列中。如果是,我們關注串列的剩余部分(subList)。
第二種方法有效,因為如果未找到該元素, indexOf 回傳 -1。
private static boolean test(List<Integer> list1, List<Integer> list2) {
int i = 0;
for (int x : list1) {
List<Integer> tempList = list2.subList(i, list2.size());
if (tempList.contains(x)) {
i = list2.indexOf(x);
}
else return false;
}
return true;
}
private static boolean test2(List<Integer> list1, List<Integer> list2) {
int i = 0;
int j = 0;
int len = list2.size();
for (int x : list1) {
j = list2.subList(i, len).indexOf(x);
if (j < 0) return false;
i = j;
}
return true;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/444860.html
