anuradha
anuradha

Reputation: 692

Show List of Arrays in an ArrayList in Android

I am trying to create a list of arraylist in android. Here is my expected formate.

  [[90.35911560058594, 23.762723181676783],
  [90.36435127258301, 23.764608467290504],
  [90.36520957946776, 23.764922678903957],
  [90.36606788635254, 23.76327305946888],
  [90.36941528320312, 23.763901488386157],
  [90.3706169128418, 23.76445136119925],
  [90.36735534667969, 23.772070788117777],
  [90.3665828704834, 23.77324901017239],
  [90.3614330291748, 23.773327557929896],
  [90.35877227783203, 23.76979286188437],
  [90.35911560058594, 23.76979286188437]]

I used two lists List<Double> nodes = new ArrayList<>() and List<List<Double>> list = new ArrayList<List<Double>>(). Then I added the followings

                nodes.add(latLng.longitude);
                nodes.add(latLng.latitude);
                list.add(nodes);

But this is showing the following output,

Coordinate_List: [81.5364906191826, 56.07605883895308, 86.5351914614439, 61.56546071581482, 94.52162001281975, 59.151354552315375]

All elements are showing in one arraylist. But I need to show this one -

[[90.35911560058594, 23.762723181676783],
      [90.36435127258301, 23.764608467290504]]

Can anyone please suggest me possible ways to do this?

Upvotes: 0

Views: 1095

Answers (7)

Lalit Jadav
Lalit Jadav

Reputation: 1427

It is good if you create model class for latlng.

public class LatLngSinglton{
   private double mylat;
   private double mylng;
}

then assign these values in singlton class

ArrayList<LatLngSinglton> myLatlongs = new ArrayList<>();
myLatlongs.add(new LatLngSinglton(lat, lng));

This way you can get your expected format.

Upvotes: 2

Andreas
Andreas

Reputation: 159096

The easiest, if you are doing constants and don't need to re-size the lists, is to use Arrays.asList():

List<List<Double>> nodes = Arrays.asList(
        Arrays.asList(90.35911560058594, 23.762723181676783),
        Arrays.asList(90.36435127258301, 23.764608467290504),
        Arrays.asList(90.36520957946776, 23.764922678903957),
        Arrays.asList(90.36606788635254, 23.76327305946888 ),
        Arrays.asList(90.36941528320312, 23.763901488386157),
        Arrays.asList(90.3706169128418 , 23.76445136119925 ),
        Arrays.asList(90.36735534667969, 23.772070788117777),
        Arrays.asList(90.3665828704834 , 23.77324901017239 ),
        Arrays.asList(90.3614330291748 , 23.773327557929896),
        Arrays.asList(90.35877227783203, 23.76979286188437 ),
        Arrays.asList(90.35911560058594, 23.76979286188437 )
);
System.out.println(nodes);

Output

[[90.35911560058594, 23.762723181676783], [90.36435127258301, 23.764608467290504], [90.36520957946776, 23.764922678903957], [90.36606788635254, 23.76327305946888], [90.36941528320312, 23.763901488386157], [90.3706169128418, 23.76445136119925], [90.36735534667969, 23.772070788117777], [90.3665828704834, 23.77324901017239], [90.3614330291748, 23.773327557929896], [90.35877227783203, 23.76979286188437], [90.35911560058594, 23.76979286188437]]

If you need to re-size the nodes list, do the following. No need to do that for each node, since they are always size 2.

List<List<Double>> nodes = new ArrayList<>(Arrays.asList(
        Arrays.asList(90.35911560058594, 23.762723181676783),
         . . .
        Arrays.asList(90.35911560058594, 23.76979286188437 )
));

Upvotes: 0

guest
guest

Reputation: 11

Could you post more of your code? And are you creating a new list for every coordinate? It Looks a bit like you are adding all your coordinates to the same list.

Maybe this minimal example can help you:

public class Main {
    public static void main(String[] args) {
        List<List<Double>> list = new ArrayList<List<Double>>();
        add(list, 10.5, 12.87);
        add(list, 20.5, 22.87);
        add(list, 30.5, 32.87);
        System.out.println(list);
    }

    private static void add(List<List<Double>> list, double lat, double lon) {
        List<Double> nodes = new ArrayList<Double>();
        nodes.add(lat);
        nodes.add(lon);
        list.add(nodes);
    }
}

output:

[[10.5, 12.87], [20.5, 22.87], [30.5, 32.87]]

Upvotes: 1

Sadiq Md Asif
Sadiq Md Asif

Reputation: 902

You aren't initializing the nodes each time you add data. thus it's not being in that format. Implement in this way.

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

         //loop to add add data
        for(...){
        List<Double>nodes=new ArrayList<>();
        nodes.add(latLng.longitude);
        nodes.add(latLng.latitude);
        list.add(nodes);
        }

Upvotes: 0

noobEinstien
noobEinstien

Reputation: 3283

Why don't you create

List<LatLong> nodes=new ArrayList();

And add LatLong directly

nodes.add(latLong);

And take the data

for(...){
    LatLong latLong=nodes.get(int);
    double lat=laLong.lattitude;
    double longtude=latLong.longitude;
}

Upvotes: 0

EKN
EKN

Reputation: 1906

You are creating array list with wrong type.

You must use List<nodes> nodes = new ArrayList<>() instead of List<Double> nodes = new ArrayList<>()

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

Instead of using List<Double> nodes, use JSONArray to store longitude and latitude then added it to list :

JSONArray jsonArray=new JSONArray();
jsonArray.add(latLng.longitude);
jsonArray.add(latLng.latitude);
list.add(jsonArray.toString());

Upvotes: 0

Related Questions