有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java计算到arraylist的路由距离,但不断添加第三个元素

因此,我试图通过计算long和lat来计算每条路线的距离。我将其添加到arrayList中,然后移动到下一条路线。它似乎在计算前3个元素的路径,但似乎只是继续添加第3个元素

谁能看出我做错了什么? 这是一个相当大的功能

ArrayList<Integer> xCoords = new ArrayList<Integer>();
        ArrayList<Integer> yCoords = new ArrayList<Integer>();
        int fitness2 = 0;

        for (List<Integer> eachChromeNew : populationShuffle){



            for (int n =0; n < eachChromeNew.size();n++){


                xCoords.add(geoPoints.get(n).getLongitudeE6());
                yCoords.add(geoPoints.get(n).getLatitudeE6());

            }
            for (int c = 0; c < xCoords.size();c++){

                if(c != xCoords.size()-1)
                {

                int x1 = xCoords.get(c);
                int y1 = xCoords.get(c + 1);

                int x2 = yCoords.get(c);
                int y2 = yCoords.get(c + 1);

                fitness2 += Math.sqrt((Math.pow(x2 - x1,2) + Math.pow(y2 - y1, 2)));
                }
                fitnessArrayTest.add(fitness2);
            }
            System.out.println("Fitness Test is: = " +fitness2);

        }

这就是输出

10-08 21:43:10.715: I/System.out(26785): Fitness Test is: = 669448211
10-08 21:43:10.715: I/System.out(26785): Fitness Test is: = 2092025460
10-08 21:43:10.720: I/System.out(26785): Fitness Test is: = 2147483647
10-08 21:43:10.720: I/System.out(26785): Fitness Test is: = 2147483647
10-08 21:43:10.720: I/System.out(26785): Fitness Test is: = 2147483647
10-08 21:43:10.720: I/System.out(26785): Fitness Test is: = 2147483647

编辑

所以,在把它改成双倍后,它似乎稍微起作用,但几分钟后,它似乎稳定地增加了。我的意思是:

一,

0-08 21:56:07.275: I/System.out(28729): Fitness Test is: = 6.694724656380861E8
10-08 21:56:07.275: I/System.out(28729): Fitness Test is: = 2.0921019557100217E9
10-08 21:56:07.275: I/System.out(28729): Fitness Test is: = 4.267888470215804E9
10-08 21:56:07.275: I/System.out(28729): Fitness Test is: = 7.196832009155435E9
10-08 21:56:07.280: I/System.out(28729): Fitness Test is: = 1.0878932572528923E10
10-08 21:56:07.280: I/System.out(28729): Fitness Test is: = 1.5314190160336267E10
10-08 21:56:07.280: I/System.out(28729): Fitness Test is: = 2.050260477257744E10
10-08 21:56:07.285: I/System.out(28729): Fitness Test is: = 2.644417640925244E10
10-08 21:56:07.285: I/System.out(28729): Fitness Test is: = 3.313890507036129E10
10-08 21:56:07.285: I/System.out(28729): Fitness Test is: = 4.0586790755903984E10
10-08 21:56:07.285: I/System.out(28729): Fitness Test is: = 4.878783346588052E10
10-08 21:56:07.285: I/System.out(28729): Fitness Test is: = 5.774203320029091E10
10-08 21:56:07.285: I/System.out(28729): Fitness Test is: = 6.744938995913514E10
10-08 21:56:07.285: I/System.out(28729): Fitness Test is: = 7.790990374241312E10
10-08 21:56:07.290: I/System.out(28729): Fitness Test is: = 8.912357455012492E10

数字末尾显示的E8、E9和E10是什么

编辑

在Martin建议除以1000000后,我得到了一个更好的答案,但我知道我遇到了另一个问题,希望是最后一个问题。这就是每次健身都会增加,我期望的是一个随机顺序。这是新的输出

10-08 22:18:49.990: I/System.out(2576): Fitness Test is: = 669.4690141960474
10-08 22:18:49.995: I/System.out(2576): Fitness Test is: = 2092.0939129186613
10-08 22:18:49.995: I/System.out(2576): Fitness Test is: = 4267.874696167842
10-08 22:18:49.995: I/System.out(2576): Fitness Test is: = 7196.8113639435915
10-08 22:18:49.995: I/System.out(2576): Fitness Test is: = 10878.903916245905
10-08 22:18:50.000: I/System.out(2576): Fitness Test is: = 15314.152353074784
10-08 22:18:50.005: I/System.out(2576): Fitness Test is: = 20502.556674430216
10-08 22:18:50.005: I/System.out(2576): Fitness Test is: = 26444.116880312213
10-08 22:18:50.005: I/System.out(2576): Fitness Test is: = 33138.83297072078
10-08 22:18:50.005: I/System.out(2576): Fitness Test is: = 40586.70494565593
10-08 22:18:50.010: I/System.out(2576): Fitness Test is: = 48787.73280511766
10-08 22:18:50.010: I/System.out(2576): Fitness Test is: = 57741.916549105954
10-08 22:18:50.010: I/System.out(2576): Fitness Test is: = 67449.25617762083
10-08 22:18:50.010: I/System.out(2576): Fitness Test is: = 77909.75169066226
10-08 22:18:50.010: I/System.out(2576): Fitness Test is: = 89123.40308823026
10-08 22:18:50.010: I/System.out(2576): Fitness Test is: = 101090.21037032483
10-08 22:18:50.010: I/System.out(2576): Fitness Test is: = 113810.17353694596
10-08 22:18:50.015: I/System.out(2576): Fitness Test is: = 127283.29258809367
10-08 22:18:50.015: I/System.out(2576): Fitness Test is: = 141509.56752376774

现在有人看到什么不对劲了吗


共 (2) 个答案

  1. # 1 楼答案

    问题是,您正在使用int作为fitness2变量,并且达到了它可以处理的上限。既然您处理的是本质上是浮点的大数,为什么不使用double来代替呢

    你看到的效果是一个奇怪的效果组合,基本上是因为你在int操作符上使用了+=,但是右边是double。使用double执行算术,然后使用JLS section 5.1.3规则将其转换回int,当结果太大或太小时,包括此步骤:

    The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.

  2. # 2 楼答案

    这里的提示是2147483647是MAX_INT。您正在达到上限。您可以尝试将fitness2更改为long以确认这是问题所在,但我不建议将其作为解决方案

    我建议使用double,因为这就是数学。sqrt正在处理

    编辑:这与您的问题无关,但请注意,当您离开赤道时,您的计算将非常不准确。经线聚集在一起,所以你会夸大东西方向的距离。一个简单的解决方法是用y坐标平均值的余弦来缩放x坐标。[注:这假设地球是一个完美的球体,而事实并非如此,但它会让你离得非常近]

    编辑2:E8、E9等表示* 10 ^ 8* 10 ^ 9等。因此1.0E9比1.0E8大10倍

    您的坐标单位为微度(每度1000000)。您可能希望将x1 x2 etc也更改为双倍,并将值除以1000000

    编辑3:如果您不想累积结果,则需要在每次迭代之间重置distance2。尝试将double fitness2移动到外部循环中。就是

    ArrayList<Integer> yCoords = new ArrayList<Integer>();
    
    for (List<Integer> eachChromeNew : populationShuffle){
        double fitness2 = 0;