Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
We are very familiar with the programming language java and the swap method we usually use to swap particular elements in any array. In this blog, we will discuss the swap function in java.
This requires some prerequisite knowledge of objects, methods, and classes. We will look at them one by one and proceed by learning about the swap function along with the coded examples. Before proceeding with exploring the swap function in java, we must first understand what methods are in java.
What is Swap Function in Java
The Swap function in java as the name suggests is use to swap two values of a particular array with each other.In programming languages like Java we have to frequently use this swap function in different scenarios like when doing sorting or other complex operations.
Syntax of Swap Function in Java
The syntax for the swap function in java is as follows:
Collections.swap(List<data_type> list, int i, int j)
Parameters and return types
List<data_type> list
This is a list where the data type is defined inside the template, this is where we want the swap operation to execute
i
Index of one of the elements that you want to swap in the list
j
Index of the other element you want to swap.
The swap function does not return anything, it has a void return type; it; this just swaps the indexes inside the list we pass it as parameters.
Some Examples To Illustrate Swap Function in Java
Example 1
Java
Java
import java.util.*; public class example1swap { public static void main (String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("cat"); list.add("dog"); list.add("rat"); list.add("mouse"); list.add("monkey"); System.out.println("List before swapping the elements is " +list); /*calling the swap function */ Collections.swap(list, 2, 3); System.out.println("List after swapping the elements is :" +list); } }
You can also try this code with Online Java Compiler
In this section, we will take an example in which the indexes will be out of range.
Java
Java
import java.util.*; public class example2 { public static void main(String[] argv) throws Exception { try { ArrayList<String> list = new ArrayList<String>(); list.add("rat"); list.add("monkey"); list.add("mouse"); list.add("rabbit"); list.add("horse"); System.out.println("Before swapping the elements of the list are: " + list); /*more than upper bound index */ Collections.swap(list, 0, 5); System.out.println("After swapping the elements are: " + list); } catch (IndexOutOfBoundsException e) { System.out.println("IndexOutOfBoundsException!!!!!!! : " + e); } } }
You can also try this code with Online Java Compiler
import java.util.*; public class Main { public static void main(String[] args) { // Create a Vector List<String> list = new Vector<>(); list.add("Tom"); list.add("Jerry"); list.add("Spike"); // Display the original list System.out.println("Original List: " + list); // Swap elements at positions 0 and 2 Collections.swap(list, 0, 2); // Display the list after swapping System.out.println("After Swapping: " + list); } }
You can also try this code with Online Java Compiler
Original List: [Tom, Jerry, Spike]
After Swapping: [Spike, Jerry, Tom]
Example 4
Java
Java
import java.util.*; public class Main { public static void main(String[] args) { // Create a Stack List<Integer> list = new Stack<>(); list.add(100); list.add(200); list.add(300); list.add(400); // Display the original list System.out.println("Original List: " + list); // Swap elements at positions 1 and 3 Collections.swap(list, 1, 3); // Display the list after swapping System.out.println("After Swapping: " + list); } }
You can also try this code with Online Java Compiler
Original List: [100, 200, 300, 400]
After Swapping: [100, 400, 300, 200]
We can see here that the index is out of bound thus, an exception is thrown. You can also find the output of this Java compiler code here.
We hope you are now well-versed in the swap function in Java.
Frequently Asked Questions
What does swap() do?
The swap() function exchanges the values of two variables. It is commonly used in sorting algorithms and data structure manipulations.
What is switch() in Java?
The switch() statement in Java is a control flow statement that allows multi-way branching based on a variable's value, replacing multiple if-else conditions.
What is swap used for?
swap() is used to exchange variable values efficiently, often in sorting algorithms, swapping elements in arrays, and optimizing memory operations in programming.
Conclusion
Our prime focus was to learn about the swap function in Java. We started with understanding what objects and classes in java are. We further discussed methods in java. Then we finally addressed the swap function in java along with the coded examples.