import java.util.Collections;
import java.util.Vector;
public class SwapElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Before swap Vector = "+vector);
/*
* Swaps the elements at the specified positions
* in the specified list. (If the specified
* positions are equal, invoking this method
* leaves the list unchanged.)
*/
Collections.swap(vector, 1, 3);
System.out.println("After swap vector = "+vector);
}
}
Output:
Before swap Vector = [cricket, hockey, football, tennish]
After swap vector = [cricket, tennish, football, hockey]
import java.util.Vector;
public class SwapElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Before swap Vector = "+vector);
/*
* Swaps the elements at the specified positions
* in the specified list. (If the specified
* positions are equal, invoking this method
* leaves the list unchanged.)
*/
Collections.swap(vector, 1, 3);
System.out.println("After swap vector = "+vector);
}
}
Output:
Before swap Vector = [cricket, hockey, football, tennish]
After swap vector = [cricket, tennish, football, hockey]
No comments:
Post a Comment