这段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++)