Java Program to Swap the Elements of Vector Last Updated : 21 Dec, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The swap() method of java.util.Collections class is used to swap the elements at the specified positions in the specified list. If the specified positions are equal, invoking this method leaves the list unchanged. Syntax: public static void swap(List list, int i, int j) Parameters: This method takes the following argument as a Parameter list – The list in which to swap elements.i – the index of one element to be swapped.j – the index of the other element to be swapped. Exception This method throws IndexOutOfBoundsException, if either i or j is out of range (i = list.size() || j = list.size()). Example 1: Java // Java program to Swap Elements of Java Vector import java.util.Collections; import java.util.Vector; public class GFG { public static void main(String[] args) { // create vector Vector<String> vector = new Vector<String>(); // insert elements in vector vector.add("A"); vector.add("B"); vector.add("C"); vector.add("D"); vector.add("E"); // print original vector System.out.println("Before Swapping = "+vector); // call Collection.swap() method Collections.swap(vector, 0, 4); // print vector after swap two elements System.out.println("After Swapping = "+vector); } } Output[A, B, C, D, E] After swapping [E, B, C, D, A] Example 2: For IndexOutOfBoundsException Java // Java program to demonstrate // swap() method for IndexOutOfBoundsException import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of List<String> List<String> vector = new ArrayList<String>(); // populate the vector vector.add("rohan"); vector.add("manish"); vector.add("simran"); vector.add("ananya"); vector.add("ishika"); // printing the vector before swap System.out.println("Before swap: " + vector); // swap the elements System.out.println("\nTrying to swap elements" + " more than upper bound index "); Collections.swap(vector, 0, 5); // printing the vector after swap System.out.println("After swap: " + vector); } catch (IndexOutOfBoundsException e) { System.out.println("Exception thrown : " + e); } } } OutputBefore swap: [rohan, manish, simran, ananya, ishika] Trying to swap elements more than upper bound index Exception thrown : java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 5 Comment More infoAdvertise with us Next Article Vector firstElement() Method in Java M mukulsomukesh Follow Improve Article Tags : Java Technical Scripter Technical Scripter 2020 Java-Vector Practice Tags : Java Similar Reads Java Vector elementAt() Method In Java, the elementAt() method is used to fetch or retrieve an element at a specific index from a Vector. It allows to access elements in the vector by providing the index, which is zero-based.Example 1: Here, we use the elementAt() method to retrieve an element at a specified index with an Integer 2 min read Java Vector elementAt() Method In Java, the elementAt() method is used to fetch or retrieve an element at a specific index from a Vector. It allows to access elements in the vector by providing the index, which is zero-based.Example 1: Here, we use the elementAt() method to retrieve an element at a specified index with an Integer 2 min read Java Program to Swap two Variables Given two numbers x and y, we need to swap their values. In this article, we will learn the Swapping of two numbers in Java.Examples of SwappingInput : x = 10, y = 20;Output : x = 20, y = 10Input : x = 213, y = 109Output : x = 109, y = 213Steps to Swap Two Numbers in JavaBelow are the simple steps w 4 min read Java Program to Swap two Variables Given two numbers x and y, we need to swap their values. In this article, we will learn the Swapping of two numbers in Java.Examples of SwappingInput : x = 10, y = 20;Output : x = 20, y = 10Input : x = 213, y = 109Output : x = 109, y = 213Steps to Swap Two Numbers in JavaBelow are the simple steps w 4 min read Vector firstElement() Method in Java The java.util.vector.firstElement() method in Java is used to retrieve or fetch the first element of the Vector. It returns the element present at the 0th index of the vector Syntax: Vector.firstElement() Parameters: The method does not take any parameter. Return Value: The method returns the first 2 min read Vector firstElement() Method in Java The java.util.vector.firstElement() method in Java is used to retrieve or fetch the first element of the Vector. It returns the element present at the 0th index of the vector Syntax: Vector.firstElement() Parameters: The method does not take any parameter. Return Value: The method returns the first 2 min read Like