We are going to share Java program to swap two elements in a vector with the output. If you are a Java beginner and want to start learning the Java programming, then keep your close attention in this tutorial as I am going to share how to write a Java program to swap two elements in a vector.
Copy the below Java program and execute it with the help of Javac compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Collections; import java.util.Vector; public class Main { public static void main(String[] args) { Vector<String> v = new Vector(); v.add("10"); v.add("20"); v.add("30"); v.add("40"); v.add("50"); System.out.println(v); Collections.swap(v, 0, 4); System.out.println("After swapping"); System.out.println(v); } } |
10 20 30 40 50
After swapping
50 40 30 20 10
Liked this program? Do Like & share with your friends.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: java example of collection, Java programs, java programs examples with output, java programs to practice, Java programs with examples, stack in java example, swapping program using array in java