0

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?

2
  • 1
    What is showing "the following output"? Which code renders this output? Commented Jan 2, 2017 at 8:17
  • are you creating a new nodes arraylist for each pair of co-ordinates? Commented Jan 2, 2017 at 8:18

7 Answers 7

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What does singleton class mean here? I assume that you do not mean your program to have one single instance of LatLngSinglton?
LatLngSinglton class is just model class.
1

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]]

1 Comment

The add() method could be implemented simply as list.add(Arrays.asList(lat, lon)). The fact that Arrays.asList() returns a fixed-size list should not be an issue here.
0

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());

Comments

0

You are creating array list with wrong type.

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

1 Comment

I know. But it is possible to create a list with type node(longitude and latitude saved array), and we can store each node object in to the list. I think it is the easy way to fix the issue.
0

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;
}

Comments

0

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);
        }

Comments

0

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 )
));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.