import java.util.*;
import java.lang.*;
class Main {
{
//create an ArrayList object
//Add elements to Arraylist
words.add("A");
words.add("B");
words.add("C");
words.add("D");
words.add("E");
System.
out.
println("Before swaping, ArrayList contains : " + words
);
/*
To swap elements of Java ArrayList use,
static void swap(List list, int firstElement, int secondElement)
method of Collections class. Where firstElement is the index of first
element to be swapped and secondElement is the index of the second element
to be swapped.
If the specified positions are equal, list remains unchanged.
Please note that, this method can throw IndexOutOfBoundsException if
any of the index values is not in range.
*/
System.
out.
println("After swaping, ArrayList contains : " + words
);
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CgpjbGFzcyBNYWluIHsKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHRocm93cyBqYXZhLmxhbmcuRXhjZXB0aW9uCgoKICAgIHsKCiAgICAgICAgLy9jcmVhdGUgYW4gQXJyYXlMaXN0IG9iamVjdAogICAgICAgIEFycmF5TGlzdCB3b3JkcyA9IG5ldyBBcnJheUxpc3QoKTsKCiAgICAgICAgLy9BZGQgZWxlbWVudHMgdG8gQXJyYXlsaXN0CiAgICAgICAgd29yZHMuYWRkKCJBIik7CiAgICAgICAgd29yZHMuYWRkKCJCIik7CiAgICAgICAgd29yZHMuYWRkKCJDIik7CiAgICAgICAgd29yZHMuYWRkKCJEIik7CiAgICAgICAgd29yZHMuYWRkKCJFIik7CgogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiQmVmb3JlIHN3YXBpbmcsIEFycmF5TGlzdCBjb250YWlucyA6ICIgKyB3b3Jkcyk7CgogICAgICAgIC8qCiAgICAgIFRvIHN3YXAgZWxlbWVudHMgb2YgSmF2YSBBcnJheUxpc3QgdXNlLAogICAgICBzdGF0aWMgdm9pZCBzd2FwKExpc3QgbGlzdCwgaW50IGZpcnN0RWxlbWVudCwgaW50IHNlY29uZEVsZW1lbnQpCiAgICAgIG1ldGhvZCBvZiBDb2xsZWN0aW9ucyBjbGFzcy4gV2hlcmUgZmlyc3RFbGVtZW50IGlzIHRoZSBpbmRleCBvZiBmaXJzdAogICAgICBlbGVtZW50IHRvIGJlIHN3YXBwZWQgYW5kIHNlY29uZEVsZW1lbnQgaXMgdGhlIGluZGV4IG9mIHRoZSBzZWNvbmQgZWxlbWVudAogICAgICB0byBiZSBzd2FwcGVkLgogICAgIAogICAgICBJZiB0aGUgc3BlY2lmaWVkIHBvc2l0aW9ucyBhcmUgZXF1YWwsIGxpc3QgcmVtYWlucyB1bmNoYW5nZWQuCiAgICAgCiAgICAgIFBsZWFzZSBub3RlIHRoYXQsIHRoaXMgbWV0aG9kIGNhbiB0aHJvdyBJbmRleE91dE9mQm91bmRzRXhjZXB0aW9uIGlmCiAgICAgIGFueSBvZiB0aGUgaW5kZXggdmFsdWVzIGlzIG5vdCBpbiByYW5nZS4KICAgICovCgogICAgICAgIENvbGxlY3Rpb25zLnN3YXAod29yZHMsIDAsIHdvcmRzLnNpemUoKSAtIDEpOwoKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkFmdGVyIHN3YXBpbmcsIEFycmF5TGlzdCBjb250YWlucyA6ICIgKyB3b3Jkcyk7CgoKICAgIH0KfQ==
Before swaping, ArrayList contains : [A, B, C, D, E]
After swaping, ArrayList contains : [E, B, C, D, A]