这段JAVA代码有什么问题? - 爱问答

(爱问答)

这段JAVA代码有什么问题?

代码内容:

package solution;

 

import edu.princeton.cs.algs4.*;

 

public class C1L2Solution {

 

public static void intersectedInterval1D(Interval1D[] param)

{

for (int i = 0; i < param.length; i++)

for (int j = i + 1; j < param.length; i++)

if (isIntersected(param[i], param[j]))

System.out.println(param[i] + ", " +  param[j]);

}

 

public static boolean isIntersected(Interval1D a, Interval1D b)

{

return

(a.min() <= b.min() && a.max() >= b.max()) ||

(a.min() >= b.min() && a.min() <= b.max()) ||

(a.min() >= b.min() && a.max() >= b.max()) ||

(a.min() >= b.min() && a.min() <= b.max()) ;

}

 

public static void main(String[] args)

{

Interval1D[] test = new Interval1D[4];

test[0] = new Interval1D(1, 4);

test[1] = new Interval1D(2, 4);

test[2] = new Interval1D(2, 3);

test[3] = new Interval1D(3, 4);

intersectedInterval1D(test);

}

}

我做的是《算法》第四版的1.2.2,测试报错

[1.0, 4.0], [2.0, 4.0]

[2.0, 4.0], [2.0, 4.0]

[2.0, 3.0], [2.0, 4.0]

[3.0, 4.0], [2.0, 4.0]

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

at solution.C1L2Solution.intersectedInterval1D(C1L2Solution.java:11)

at solution.C1L2Solution.main(C1L2Solution.java:31)


请问出了什么问题?

for (int j = i + 1; j < param.length; i++)

改成

for (int j = i + 1; j < param.length;j++)

下一篇:for循环的判断条件是如何发挥作用的

上一篇:m3-7y30和i7是不是一样的大小

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?