amaro tati
amaro tati

Reputation: 55

How can I create and access list of list of list.. in Java?

I have an object from JSON and I would like to access the elements within multiple arrays. I generate classes with getters and setters in Dto from JSON and it gave me one of the public type List <List <List <List <Double> >>> getCoordinates ()

How can I get the coordinates elements?

And I try this way, but it´s not working. GeoReferencias is my object json.

GeoReferencias geoReferencias = new GeoReferencias();
for(Geometry geometry : geoReferencias.getFeatures().get().getGeometry()) {
    geometry.getCoordinates();
}

JSON data:

{
    "type": "Feature",
    "id": 426,
    "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
        [
            [
                [
                    20.90620040893566,
                    -11.984783172607422
                ],
                [
                    20.901933670044002,
                    -11.982872009277344
                ]
            ]
        ]
    }
}

Upvotes: 0

Views: 509

Answers (1)

SSP
SSP

Reputation: 2670

List <List <List <List <Double> >>> getCoordinates = new ArrayList<>();

for(int i = 0 ; i <getCoordinates.size()  ; i++){
     Double cordinate[] = new Double[2];// if you have only 2 cordinates
     List <List <List <Double> >> a = getCoordinates.get(i)

     for(int j = 0 ; j <a.size()  ; j++){
         List <List <Double> > b = a.get(j);

         for(int k = 0 ; k <b.size()  ; k++){
            List <Double>  c = b.get(k);

            for(int l = 0 ; l <c.size()  ; l++){
                Double[l] =  c.get(l);
            }
        }
    }
    //perform your action related to coordinates over here because it will re-initialize as its inside loop.
}

This is how you traverse nested list.

If you know that you always need to traverse 4rth list and you have fixed length of previous 3 nested list you can use below syntax :-

Double arr[] = new int[2];
arr[0] = getCoordinates.get(fixed_index).get(fixed_index).get(fixed_index).get(0);
arr[1] = getCoordinates.get(fixed_index).get(fixed_index).get(fixed_index).get(1);

Upvotes: 1

Related Questions