patrick1980 patrick1980 - 1 month ago 21
Android Question

One Loop with 2x ArrayList

I have 2 ArrayList:

ArrayList<LatLng> list1
ArrayList<String> list2


I try to use 2 ArrayList in one loop. Example.

for(LatLng a : list1 [i dont know what put here] String b : list2)
{

Log.d("Response", String.valueOf(a, b));

}


what i should do ?

Answer

You can't use an enhanced for loop over 2 lists at once, the only option is to use a normal for loop:

//Assuming list1 and list2 are identical sizes:
for (int i = 0; i < list1.size(); i++){
    LatLng latLng = list1.get(i);
    String s = list2.get(i);
    //Do something with them
}

If you REALLY want to do it in 1 loop, you might consider making a combination object:

public class CombinationObject{

    private LatLng latLng;
    private String string;

    //GETTERS & SETTERS
}

Then you can add your elements to a list of CombinationObject : List<CombinationObject> list; and loop just over that singular list.

for (CombinationObject co : list){
    LatLng latLng = co.getLatLng();
    String s = co.getString();
    //Do something...
}