-
I want to loop trough an arraylist of String arrays.
-
These string arrays have only two elements. inside the loop i would like to compare these two elements of each array.
-
i would like to do this in a clean and simple way (avoiding classic for, iterator class, etc., if possible -i know how to do this those ways, but I’m looking for a cleaner way-.
-
i would prefer a for-each loop (or for + “:”)
-
I’m attaching a sample code to show you what i want to do.
what is the best way for doing this?
ArrayList<String[]> myLst = new ArrayList<String[]>();
// the array of strings has two elements
public String getSecondField(String query)
{
for (String[] binArray : myLst)
{
if (binArray[0].equals(query))
{
return binArray[1];
}
}
return "";
}
Instead of arrays of two elements, and since all the first elements of each array are unique, use a
Map<String, String>. This way, your code will become: