Below is my code where I want to compare two array elements and add corresponding elements to a new array(foundArray
) and not found elements to other array(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]);
}
}
}
}
This isn't working.It's giving me ArrayIndexOutOfBoundsException
and also executing only else statement.I have googled for it but no correct answer.
Any help is appreciated.Thank you!