I'm trying to create a function that compares all elements of an array with all elements of a second array and will return all possible matches, and message if no matches are found. When I try to implement the code, I get an index out of bound error. The inner for loop probably maxed out before the outer for loop can finish running. How do I modify it to prevent such a scenario from occurring?
Stocks[] stockList3 = new Stocks[3];
stockList3[0] = new Stocks("a", 2, 1, "Buy");
stockList3[1] = new Stocks("a", 3, 1, "Buy");
stockList3[2] = new Stocks("a", 4, 1, "Buy");
Stocks[] stockList4 = new Stocks[3];
stockList4[0] = new Stocks("a", 2, 1, "Buy");
stockList4[1] = new Stocks("a", 5, 1, "Buy");
stockList4[2] = new Stocks("a", 4, 1, "Buy");
public void matching(Stocks[] array1, Stocks[] array2) {
for (int i = 0; i < array1.length; i++) {
for (int j = 0; i < array2.length; j++) {
if (array1[i].stockPrice == array2[j].stockPrice) {
System.out.println("It's a match at $" + array1[i].stockPrice);
}
System.out.println("still searching...");
}
System.out.println("first loop test...");
}
}