如何连续迭代两个数组?

时间:2012-12-13 08:42:40

标签: java android google-maps for-loop

我有两个双数组:

double[] LatArr = {28.5700,28.4700,28.29};
double[] LonArr = {77.3200,77.0300,77.62};

我的循环结构如下所示:

for(int i=0;i<count;i++)
            {
                for(int j=0;j<count;j++)
                {
                    Double lat1;
                    Double lon1;
                    Double lat2;
                    Double lon2;
                    lat1 = LatArr[i];
                    lon1 = LonArr[j];
                    //System.out.println(LatLon[i][j]);
                    /*System.out.println("<< BREAK >>");
                    System.out.println(lat1);
                    System.out.println(lon1);
                    */
                    lat2=lat1;
                    //lat1 = null;
                    lon2=lon1;
                    //lon1 = null;
                    i++;

                    if(lat1!=null){
                    GeoPoint gp1 = new GeoPoint((int)(lat1 * 1E6), (int)(lon1 * 1E6)); //
                    GeoPoint gp2 = new GeoPoint((int)(lat2 * 1E6), (int)(lon2 * 1E6)); //
                    System.out.println("<< LAT1 >>");
                    System.out.println(lat1);
                    System.out.println(lon1);
                    System.out.println("<< LAT2 >>");
                    System.out.println(lat2);
                    System.out.println(lon2);

                    Point p11 = new Point();
                    Point p22 = new Point();

                    Path mypath = new Path();
                    projection.toPixels(gp1, p11);
                    projection.toPixels(gp2, p22);
                    mypath.moveTo(p22.x, p22.y);// *
                    mypath.lineTo(p11.x, p11.y);// *

                    canvas.drawPath(mypath, mPaint);
                    }
                }
            }

我要做的是连续获取项目LatArr [0]和LonArr [0]。

这样我就可以在lat1中使用LatArr [0],在lon1中使用LonArr [0]。 LatArr [1]也应该在lat2中,LonArr [1]在lon2中。

但是这段代码取了LatArr [0] .. [1] ..和LonArr [0] .. [1] ..两次。每个元素都打印两次?

任何人都请你纠正我,我希望能够按照这种方法在地图上绘制多个点。在所有这些代码中,将一个接一个地映射两个地理点(每个地理点具有从两个阵列馈送的纬度和经度)

任何建议都非常感谢

3 个答案:

答案 0 :(得分:1)

如果两个数组的长度相同,那么只使用一个循环,而不是两个循环,所以更改代码如下:

for(int j=0;j<count;j++)
                    {
                        Double lat1;
                        Double lon1;
                        Double lat2;
                        Double lon2;
                        lat1 = LatArr[j];
                        lon1 = LonArr[j];
                        lat2=lat1;
                        //lat1 = null;
                        lon2=lon1;
                        //lon1 = null;
                       if(lat1!=null){
                        GeoPoint gp1 = new GeoPoint((int)(lat1 * 1E6), (int)(lon1 * 1E6)); //
                        GeoPoint gp2 = new GeoPoint((int)(lat2 * 1E6), (int)(lon2 * 1E6)); //
                        System.out.println("<< LAT1 >>");
                        System.out.println(lat1);
                        System.out.println(lon1);
                        System.out.println("<< LAT2 >>");
                        System.out.println(lat2);
                        System.out.println(lon2);

                        Point p11 = new Point();
                        Point p22 = new Point();

                        Path mypath = new Path();
                        projection.toPixels(gp1, p11);
                        projection.toPixels(gp2, p22);
                        mypath.moveTo(p22.x, p22.y);// *
                        mypath.lineTo(p11.x, p11.y);// *

                        canvas.drawPath(mypath, mPaint);
                        }
                    }
                }

答案 1 :(得分:0)

每次使用相同的索引,以便从两个数组中获得相同的索引元素。

for(int i=0;i<count;i++){
  Double lat1;
  Double lon1;
  Double lat2;
  Double lon2;
  lat1 = LatArr[i];//  i th element from lat arr
  lon1 = LonArr[i];// i th element from lon arr
  ...
}

答案 2 :(得分:0)

我不明白,为什么你有两个循环,试试这个:

for(int i=0;i<count-1;i++)
        {

                Double lat1;
                Double lon1;
                Double lat2;
                Double lon2;
                lat1 = LatArr[i];
                lon1 = LonArr[i];

                /*System.out.println("<< BREAK >>");
                System.out.println(lat1);
                System.out.println(lon1);
                */
                lat2=latArr[i+1];
                //lat1 = null;
                lon2=lonArr[i+1];
                //lon1 = null;


                if(lat1!=null){
                GeoPoint gp1 = new GeoPoint((int)(lat1 * 1E6), (int)(lon1 * 1E6)); //
                GeoPoint gp2 = new GeoPoint((int)(lat2 * 1E6), (int)(lon2 * 1E6)); //
                System.out.println("<< LAT1 >>");
                System.out.println(lat1);
                System.out.println(lon1);
                System.out.println("<< LAT2 >>");
                System.out.println(lat2);
                System.out.println(lon2);

                Point p11 = new Point();
                Point p22 = new Point();

                Path mypath = new Path();
                projection.toPixels(gp1, p11);
                projection.toPixels(gp2, p22);
                mypath.moveTo(p22.x, p22.y);// *
                mypath.lineTo(p11.x, p11.y);// *

                canvas.drawPath(mypath, mPaint);
                }

        }