[英]How to compare array elements and add corresponding elements to a third array?
下面是我的代碼,在這里我想比較兩個數組元素並將相應的元素添加到新數組( foundArray
)中,而未找到的元素添加到其他數組( notFoundArray
)中。
public static void main(String[] args) {
Integer[] originalArray = { 12, 54, 19, 20, 44, 32, 14, 63, 57, 28 };
Integer[] keyArray = { 20, 44, 50, 62, 23, 28, 19, 57, 60, 99 };
List<Integer> foundArray = new ArrayList<Integer>();
List<Integer> notFoundArray = new ArrayList<Integer>();
for (int i = 0; i <= originalArray.length; i++) {
for (int j = 0; j <= keyArray.length; j++) {
if (originalArray[i] == keyArray[j]) {
System.out.println("Found");
foundArray.add(originalArray[i]);
} else if (originalArray[i] != keyArray[j]) {
System.out.println("Not Found");
notFoundArray.add(originalArray[i]);
}
}
}
}
這不起作用。它給了我ArrayIndexOutOfBoundsException
並且也只執行else語句。我已經用谷歌搜索了它,但沒有正確的答案。
感謝您的幫助。謝謝!
ann數組的最后一個索引為length-1
因為第一個索引為零,因此您的代碼必須為
for (int i = 0; i < originalArray.length; i++) {
for (int j = 0; j < keyArray.length; j++) {
我想您想將輸入數組與鍵數組進行比較,並且順序並不重要。
public static void main(String[] args) {
Set<Integer> originalArray = Arrays.asList(12, 54, 19, 20, 44, 32, 14, 63, 57, 28).stream().collect(Collectors.toSet());
Set<Integer> keyArray = Arrays.asList(20, 44, 50, 62, 23, 28, 19, 57, 60, 99).stream().collect(Collectors.toSet());
List<Integer> foundArray = new ArrayList<>();
List<Integer> notFoundArray = new ArrayList<>();
for (Integer i : originalArray) {
if (keyArray.contains(i)) {
foundArray.add(i);
} else {
notFoundArray.add(i);
}
}
System.out.println("Found");
foundArray.forEach(System.out::println);
System.out.println("Not found");
notFoundArray.forEach(System.out::println);
}
我發現您的代碼有兩個問題。 首先,您的循環邊界不正確,因為大小為N
的數組最多只能尋址N-1
。 其次,也許更重要的是,您應該在內部掃描循環遍歷查找值之后而不是在內部對每個原始編號進行簿記。 考慮到這兩個因素:
for (int i=0; i < originalArray.length; i++) {
boolean found = false;
for (int j=0; j < keyArray.length; j++) {
if (originalArray[i] == keyArray[j]) {
System.out.println("Found");
found = true;
break;
}
}
if (found) {
foundArray.add(originalArray[i]);
}
else {
notFoundArray.add(originalArray[i]);
}
}
在當前方法中,您將向兩個集合多次添加相同的初始編號。 最有可能的是,您不希望出現這種情況。
存在多個問題(用<=代替<和邏輯)。 這應該工作:
public static void main(String[] args) {
Integer[] originalArray = { 12, 54, 19, 20, 44, 32, 14, 63, 57, 28 };
Integer[] keyArray = { 20, 44, 50, 62, 23, 28, 19, 57, 60, 99 };
List<Integer> foundArray = new ArrayList<Integer>();
List<Integer> notFoundArray = new ArrayList<Integer>();
for (int i = 0; i < originalArray.length; i++) {
boolean found = false;
for (int j = 0; j < keyArray.length; j++) {
if (originalArray[i] == keyArray[j]) {
System.out.println("Found");
foundArray.add(originalArray[i]);
found = true;
break;
}
}
if(found == false) {
System.out.println("Not Found");
notFoundArray.add(originalArray[i]);
}
}
}
public static void main(String[] args) {
Integer[] originalArray = { 12, 54, 19, 20, 44, 32, 14, 63, 57, 28 };
Integer[] keyArray = { 20, 44, 50, 62, 23, 28, 19, 57, 60, 99 };
List<Integer> foundArray = new ArrayList<Integer>();
List<Integer> notFoundArray = new ArrayList<Integer>();
for (int i = 0; i < originalArray.length; i++) {
for (int j = 0; j < keyArray.length; j++) {
if (originalArray[i] == keyArray[j]) {
System.out.println("Found");
foundArray.add(originalArray[i]);
}
}
}
}
這不會引發ArrayOfOfBoundsException,並且會為您提供所有找到的元素。 要獲取未找到的元素,只需獲取一個數組並將其與“ foundArray”進行比較。
如果您對僅生成兩個最終整數列表的Stream
版本感興趣,則此代碼可以做到:
Set<Integer> keys = new HashSet<>(Arrays.asList(keyArray));
Map<Boolean, List<Integer>> partionedIntegers = Arrays.stream(originalArray).collect(Collectors.partitioningBy(keys::contains));
List<Integer> foundArray = partionedIntegers.get(true);
List<Integer> notFoundArray = partionedIntegers.get(false);
請注意,這將返回兩個具有不同Integer
List<Integer>
,而問題中的代碼將導致兩個包含重復項的列表。
此代碼的更多內容將適用於不同的數組長度。
並參考我對這個問題的評論:
HashSet.contains
將使用hashCode
和equals
,而不是對象標識( ==
)確定相等性。
這也是使用Streams的另一個版本。
Integer[] originalArray = { 12, 54, 19, 20, 44, 32, 14, 63, 57, 28 };
Integer[] keyArray = { 20, 44, 50, 62, 23, 28, 19, 57, 60, 99 };
List<Integer> foundArray = new ArrayList<>();
List<Integer> notFoundArray = new ArrayList<>();
Stream.of(keyArray).forEach(p -> {
boolean result = Stream.of(originalArray).anyMatch(s -> s.intValue()==p.intValue());
if(result) {
foundArray.add(p);
}else {
notFoundArray.add(p);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.