List element swap
In this chapter you will learn:
Swap element in a list
static void swap(List<?> list, int i, int j)
from Collections
class
swaps the elements at the specified positions in the specified list.
import java.util.Collections;
import java.util.Vector;
// j a v a 2 s .co m
public class Main {
public static void main(String[] args) {
Vector<String> v = new Vector<String>();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("java2s.com");
System.out.println(v);
Collections.swap(v, 0, 4);
System.out.println(v);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » List