Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add values in arraylist of arraylist?

I am retrieving longitude and latitude values of each user from firebase but problem is that i want to add that values of each user in one list so there are many list that’s why I’m using arraylist of arraylist.

So for first user

longitude = 12345 , latitude = 23456

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and for 2nd user

longitude = 45678 , latitude = 67891.

Now my question is i want to add longi and lat of first user in list [ 12345 , 23456 ] and for 2nd user [ 45678 , 67891]
By using arraylist of arraylist is equal to [[
12345,23456 ],[ 45678 , 67891 ] ].

My code:-

Initialization:-

    ArrayList<String> longitudes = new ArrayList<>();//for storing latitude and longitude values
    ArrayList<ArrayList<String> > aList =
            new ArrayList<ArrayList<String> >();
//for storing combine values of longitude and latitude

                databaseReference.child("hospital").addValueEventListener(new ValueEventListener() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {

                        longitudes.clear();
                      
                         for (DataSnapshot snapshot1: snapshot.getChildren()) {
                             longitudes.add(snapshot1.child("longitude").getValue().toString());
                             longitudes.add(snapshot1.child("latitude").getValue().toString());
                             aList.add(count,longitudes);                           
                             count++;
                         }

                        
                        count=0;
                        }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });

But it is giving output as

[[12345,23456,45678 , 67891],[ 12345,23456,45678 , 67891]]
It is repeating the values

But I am expecting for each user like

[[12345,23456],[45678 , 67891]]

>Solution :

Your longitudes is always the same list. Create a new ArrayList and assign it to longitudes on every loop iteration instead of clearing and reusing the existing one.

Remove your existing longitudes variable and make your loop look like this:

ArrayList<String> longitudes;
for (DataSnapshot snapshot1: snapshot.getChildren()) {
    longitudes = new ArrayList<>()
    longitudes.add(snapshot1.child("longitude").getValue().toString());
    longitudes.add(snapshot1.child("latitude").getValue().toString());
    aList.add(count,longitudes);                           
    count++;
}

I would further like to suggest not to represent lat+long as a List with 2 elements, just create your own class with 2 fields orif your are on java14+, use a record:

public record LatLong (String lat, String long) {}

and then use that in your list:

ArrayList<LatLong > aList = new ArrayList<>();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading