【问题标题】:Segment Length from an arrayList of Points --Java来自点数组列表的段长度--Java
【发布时间】:2013-05-22 19:02:27
【问题描述】:

我正在尝试从我创建的 arrayList 中计算线段(长度)。 这是在 JAVA 中。一切都按我的意愿进行,但是我收到了以下消息: “线程“主”java.lang.IndexOutOfBoundsException 中的异常:索引:2,大小:2 在 java.util.ArrayList.rangeCheck(未知来源) 在 java.util.ArrayList.get(未知来源) "

数组列表的索引为“+1”会造成麻烦。这是导致错误消息的原因,但是,它确实计算了线段。它打印它们,然后打印可怕的错误语句。

myPoints.get((i+1)).x ;

public static void showStats(ArrayList<Point> myPoints)
{
    double distance = 0.0;
    double length;
    double tempX;
    double tempY;
    double tempX2;
    double tempY2;
    int tempFirst;
    int tempSecond;
    int tempYFirst;
    int tempYSecond;
    int xValue;
    int yValue;


    // Line segments are calculated by the distance formula of:
    // Sqrt ( (x2-x1)^2 + (y2-y2)^2)
    for (int i = 0; i < myPoints.size(); i++) {

        tempFirst = myPoints.get(i).x;
        tempSecond = myPoints.get((i+1)).x ;
        tempYFirst = myPoints.get(i).y;
        tempYSecond = myPoints.get((i+1)).y;

        xValue = tempFirst - tempSecond;
        yValue = tempYFirst - tempYSecond;

        tempX2 = Math.pow(xValue, 2);
        tempY2 = Math.pow(yValue, 2);

        distance += Math.sqrt((tempX2 + tempY2));

        System.out.println(tempSecond);
     }// /
}

【问题讨论】:

  • 这并不奇怪:当i 指向列表中的最后一个点时,您预计会发生什么? (你想让它环绕吗?你想让它停在列表的最后两点之间吗?)另外,你使用的是哪个Point 类?至少,您可以使用distance += Math.hypot(tempFirst - tempSecond, tempYFirst - tempYSecond) 简化此操作。

标签: java point indexoutofboundsexception


【解决方案1】:

您正试图在索引 2 处获取 Point,但只有 2 个点,并且在 Java 中索引仅从 0size - 1,或在这种情况下为 1。问题是这一行

tempSecond = myPoints.get((i+1)).x ;

i 为1 时,i + 12 并且超出范围。在您最后一个Point 上时,最好与第一个Point 进行比较以完成循环:

tempSecond = myPoints.get((i+1) % myPoints.size()).x ;

这里使用%取模运算符来得到除以myPoints.size()时的余数,这样当i + 1变得太大时,0就是结果,即size除以@987654337时的余数@ 是0

【讨论】:

    【解决方案2】:

    尝试从 1 开始循环。这样就不会出现索引越界的问题...

    for (int i = 1; i < myPoints.size(); i++) {
    
        tempFirst = myPoints.get(i-1).x;
        tempSecond = myPoints.get((i)).x ;
        tempYFirst = myPoints.get(i-1).y;
        tempYSecond = myPoints.get((i)).y;
    
        xValue = tempFirst - tempSecond;
        yValue = tempYFirst - tempYSecond;
    
        tempX2 = Math.pow(xValue, 2);
        tempY2 = Math.pow(yValue, 2);
    
        distance += Math.sqrt((tempX2 + tempY2));
    
        System.out.println(tempSecond);
     }
    

    【讨论】:

      【解决方案3】:

      除了@rgettman 的回答,尝试使用内置的Point2D.distance 方法,像这样:

      public static void showStats(ArrayList<Point> myPoints) {
          double distance = 0.0;
          for (int i = 0; i < myPoints.size(); i++) {
              Point first = myPoints.get(i);
              Point second = myPoints.get((i+1) % myPoints.size());
              // use distance method
              distance += first.distance(second);
              System.out.println(second.x);
           }
      }
      

      【讨论】:

      • 也感谢您的帮助。这将在未来派上用场,但我能够让我的代码完美运行!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 2020-05-15
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多