我正在尝试创建一个函数,该函数将数组的所有元素与第二个数组的所有元素进行比较,并将返回所有可能的匹配项,如果未找到匹配项,则返回消息。当我尝试实现代码时,我得到一个索引越界错误。内部 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...");
}
}
元芳怎么了
三国纷争
人到中年有点甜
随时随地看视频慕课网APP
相关分类