I have an array list of String element I want to duplicate last element of that list and that duplicate element set in the 0th position of same list. how to duplicate array list.
below is my input list:
List ["Apple","Tomato","Patato","Brinjal","Cabbage"]
I want below type of list as output:
List ["Cabbage","Apple","Tomato","Patato","Brinjal","Cabbage"]
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Apple");
cars.add("Tomato");
cars.add("Patato");
cars.add("Cabbage");
System.out.println(cars);
}
}
cars? Just curious.cars.add(0, cars.get(cars.size() - 1))will work, but that makes the first and last element the same instance; this may be okay for an immutable object like instances ofString, but for mutable objects this could lead to unintended side effects.