In Java, sometimes, you need to exchange the positions of objects, elements, and characters. For this purpose, you can use the swap() method. The swap means exchange. This method is used to exchange the positions of the characters or elements in the string or lists. Java supports the swap functionality by providing a “Collections.swap()” static method.
This tutorial will demonstrate the use of the swap() method in Java.
How to Use swap() Method in Java?
The “swap()” method is used to swap the characters in a string and the elements in a list. You can use either a predefined swap() method of the Collections class or create a user-defined method.
Let’s see some examples related to predefined and user-defined swap() methods in Java.
Syntax
The syntax for the swap() method to swap the elements is given below:
The method takes three parameters, where “a” represents the String or list where the swapping is performed, and “i” and “j” are the indexes of the elements that need to be swapped.
First, we will understand the functionality of the swapping by creating a user-defined swap() method.
Example 1: Utilize User-defined swap() Method to Swap Characters
In this example, we will swap the characters of a string using the user-defined swap() method. First, we will create a method named “swap()” and pass three arguments, the string “str” whose characters will be swapped, and the other two are the integer type variables referring to the indexes of the variables.
The method first creates a char type array that stores the passed string as an array by calling the “toCharArray()” method. Then, store the character from index “a” to the char type variable “temp” and place the character at the “b” index at the “a” index. Next, place the value of “temp” at the index “b” and finally return it to the method:
{
char ch[] = str.toCharArray();
char temp = ch[a];
ch[a] = ch[b];
ch[b] = temp;
return ch;
}
In the main() method, we have a string “s”. Next, print the original string and call the swap() method by passing the created string and the indexes of characters as arguments. For instance, we want to swap the sixth index character “n” with “i” that is present at the second index:
System.out.println("The String is: " + s);
System.out.print("After swapping:");
System.out.println(swap(s, 6, 2));
The given output signifies that we have successfully swapped characters of the specified characters:
Do you need to swap elements of the list? If yes! Then follow the given section.
Example 2: Using Predefined swap() Method to Swap ArrayList Elements
For swapping ArrayList elements, utilize the predefined “swap()” method of the Collections class. To do so, first, we will create an ArrayList of “fruits”:
Then, add the elements in the created ArrayList using the “add()” method:
fruits.add("Banana");
fruits.add("Apricot");
fruits.add("Peach");
Print the original order of elements using the “System.out.println()” method:
Then, call the “Collections.swap()” method by passing a list of “fruits” and indexes of elements that need to be swapped. Here, we will swap the first and the last elements of the ArrayList:
Finally, we will print out all elements after swapping on the console:
As you can see, elements of the ArrayList are successfully swapped:
Now, let’s see what happens if we pass the index that does not exist in the array.
Example 3: Swapping a Non-existing Element
Here, we will swap the element at the index of “1” with the element present at the index “4”. As the previously created ArrayList is of size three, the specified operation will throw an error:
The output shows an index out of bounds exception because the fourth index does not exist in our ArrayList:
We have provided all the essential information related to the use of the swap() method in Java.
Conclusion
The swap() method is used to swap the characters or elements of the string and a list. It takes a string or list and the indexes of the elements that need to be swapped. In Java, a predefined swap() method is used to swap the elements of the lists, ArrayList, and so on. It belongs to the Collections class. You can also utilize the predefined swap() method by adding the same functionality to it. This tutorial demonstrated the use of the swap() method in Java with detailed examples.