Fustigador
Fustigador

Reputation: 6459

ArrayList to two dimensional array

I have an ArrayList that has objects that are a latitude, and a longitude. I need to translate them to a two dimensional array.

public void DrawPolygon(ArrayList<PointModel> pModel){
    int size=pModel.size();
    double [][] latlon= new double[size][size];
    for(int i=0;i<pModel.size();i++){
        //What to do here, so I can get double[0][0][latitude of first index of arraylist][longitude of first index of array], and so on 
    }
}

Thank you!

Upvotes: 0

Views: 24

Answers (1)

flyingfox
flyingfox

Reputation: 13506

You have set the wrong size for the array,it need to be double[size][2] instead of double[size][size]

public void DrawPolygon(ArrayList<PointModel> pList){
    int size=pList.size();
    double [][] latlon= new double[size][2];
    for(int i=0;i<pList.size();i++){
         latlon[i][0] = pList.get(i).getLatitude();
         latlon[i][1] = pList.get(i).getlongitude();
    }
}

Also, make an ArrayList called pModel is a very bad idea.

Upvotes: 1

Related Questions