I have the following code:
private List<String> listOfStrings = new ArrayList<>();
listOfStrings.add("a");
listOfStrings.add("b");
listOfStrings.add("c");
listOfStrings.add("d");
for (String temp : listOfStrings) {
if (temp.equals("c")) {
Collections.swap(listOfStrings, 0, listOfStrings.indexOf(temp));
}
}
The list may not just be list of String but could a list of objects defined by the class that I wrote. I'm not sure about the swap here, I see it compiled and running fine but I don't know if it's safe here.
Does anyone have any suggestions on this? If I need to do the swap.
I planned to use for (int i = 0; i < size; i++)
to iterate and use list.get(i)
to get item, but i think it's not good idea to use list.get(i)
on an arraylist?
Any help will be appreciated!! Thanks in advance!!