我正在尝试创建一个函数,将数组的所有元素与第二个数组的所有元素进行比较,并将返回所有可能的匹配项,如果未找到匹配项则返回消息。当我尝试实现代码时,我得到一个索引超出范围的错误。在外部 for 循环完成运行之前,内部 for 循环可能已达到极限。如何修改它以防止发生这种情况?
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...");
}
}
侃侃无极
函数式编程
相关分类