- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Swap the Elements of Vector
The realm of computer programming necessitates proficient organization and preservation of data for streamlined access and manipulation. The Vector, a versatile array that can expand or contract as required, serves as a pivotal data structure in this pursuit. Java, in particular, benefits greatly from the utilization of Vectors as it offers a means to store objects of varying natures.
In this discourse, we shall delve into the intricacies of exchanging the components of a Vector in Java. Interchanging elements is a frequent procedure, particularly during the sorting or rearranging of data. We shall furnish two divergent methodologies to achieve this feat, accompanied by code snippets and comprehensive explanations.
Algorithm
To swap the elements of a Vector, follow the steps below −
Step 1 − Identify the indices of the elements to be swapped.
Determine the index of the first element to be swapped.
Determine the index of the second element to be swapped.
Step 2 − Temporarily store one of the elements in a variable.
Create a temporary variable to store the value of one of the elements.
Assign the value of the first element to the temporary variable.
Step 3 − Replace the first element with the second element.
Assign the value of the second element to the index of the first element.
Step 4 − Replace the second element with the value stored in the temporary variable.
Assign the value of the temporary variable to the index of the second element.
Syntax
Vector<T> vector = new Vector(); // Create a Vector with the desired data type T Collections.swap(vector, index1, index2); // Swap the elements at index1 and index2 using the swap method from the Collections class
Approach 1: Using a temporary variable
This approach involves using a temporary variable to hold one of the elements, while the other element is moved to its new position. Here's the code snippet −
import java.util.Vector; public class VectorSwap { public static void swapElements(Vector<?>>vec, int index1, int index2) { Object temp = vec.get(index1); vec.set(index1, vec.get(index2)); vec.set(index2, temp); } }
The above code defines a generic function named swapElements, which takes in a Vector and the specified positions of the elements to be swapped. A temporary placeholder, temp, is utilized to retain the value located at index1. Subsequently, the element at index1 is substituted with the element at index2. Finally, the element at index2 is replaced with the value stored in temp.
Approach 2: Using Collections.swap()
The Java Collections framework offers a convenient tool, Collections.swap(), that streamlines the process of exchanging elements in either a List or a Vector. This function takes in a List or Vector and the designated positions of the elements to be swapped. The following is a code example −
import java.util.Collections; import java.util.Vector; public class VectorSwap { public static void swapElements(Vector<?> vec, int index1, int index2) { Collections.swap(vec, index1, index2); } }
In this approach, we simply call the Collections.swap() method with the given Vector and indices. This method handles the swapping process internally, making our code more concise.
Working Example 1
In this example, we will demonstrate Approach 1 by swapping the elements of a Vector of integers
import java.util.Vector; public class Main { public static void main(String[] args) { Vector<Integer> vec = new Vector<Integer>(); vec.add(10); vec.add(20); vec.add(30); vec.add(40); System.out.println("Before swap: " + vec); VectorSwap.swapElements(vec, 1, 3); System.out.println("After swap: " + vec); } } class VectorSwap { public static <T> void swapElements(Vector<T> vec, int index1, int index2) { T temp = vec.get(index1); vec.set(index1, vec.get(index2)); vec.set(index2, temp); } }
Output
Before swap: [10, 20, 30, 40] After swap: [10, 40, 30, 20]
In this example, we created a Vector of integers and added four elements to it. We then called the swapElements() method from Approach 1, passing the Vector and the indices to be swapped. The output shows the Vector before and after the swap.
Working Example 2
In this example, we will demonstrate Approach 2 by swapping the elements of a Vector of strings
import java.util.Vector; public class Main { public static void main(String[] args) { Vector<String> vec = new Vector<String>(); vec.add("Apple"); vec.add("Banana"); vec.add("Cherry"); vec.add("Date"); System.out.println("Before swap: " + vec); VectorSwap.swapElements(vec, 0, 2); System.out.println("After swap: " + vec); } } class VectorSwap { public static <T> void swapElements(Vector<T> vec, int index1, int index2) { T temp = vec.get(index1); vec.set(index1, vec.get(index2)); vec.set(index2, temp); } }
Output
Before swap: [Apple, Banana, Cherry, Date] After swap: [Cherry, Banana, Apple, Date]
In this example, we created a Vector of strings and added four elements to it. We then called the swapElements() method from Approach 2, passing the Vector and the indices to be swapped. The output shows the Vector before and after the swap.
Comparison of Approach 1 and Approach 2
Criteria |
Approach 1 |
Approach 2 |
---|---|---|
Type of Vector |
Integer |
String |
Method |
swapElements(Vector<T>, int, int) |
swapElements(Vector<T>, int, int) |
Method Logic |
Swaps the elements of the Vector |
Swaps the elements of the Vector |
Conclusion
Swapping elements in a Vector is a common operation that can be accomplished using various approaches. In this article, we discussed two different methods for swapping elements of a Vector in Java: using a temporary variable and using the Collections.swap() method. Both approaches have their own benefits; using a temporary variable offers more control over the swapping process, while Collections.swap() provides a more concise solution. We also demonstrated these approaches with two working examples, showcasing their functionality and versatility. By understanding these methods and their use cases, you can efficiently swap elements in Vectors to suit your programming needs.