How to swap or exchange objects in Java?
In order to understand how to swap objects in Java, let us consider an illustration below as follows:
Illustration:
Let’s say we have a class called “Car” with some attributes. And we create two objects of Car, say car1 and car2, how to exchange the data of car1 and car2?
Methods:
- Using concepts of OOPS
- Using Wrapper classes of java
Method 1: Using concepts of OOPS
Here we will be simply swapping members for which let us directly take a sample ‘Car’ illustration with which we will play. So if the class ‘Car’ has only one integer attribute say “no” (car number), we can swap cars by simply swapping the members of two cars.
Example 1-A
Java
class Car {
int no;
Car( int no) { this .no = no; }
}
class GFG {
public static void swap(Car c1, Car c2)
{
int temp = c1.no;
c1.no = c2.no;
c2.no = temp;
}
public static void main(String[] args)
{
Car c1 = new Car( 1 );
Car c2 = new Car( 2 );
swap(c1, c2);
System.out.println( "c1.no = " + c1.no);
System.out.println( "c2.no = " + c2.no);
}
}
|
Output
c1.no = 2
c2.no = 1
Note: Geek, what if we don’t know members of Car?
The above solution worked as we knew that there is one member “no” in Car. What if we don’t know members of Car or the member list is too big. This is a very common situation as a class that uses some other class may not access members of other class. Does below solution work?
Example 1-B
Java
class Car {
int model, no;
Car( int model, int no)
{
this .model = model;
this .no = no;
}
void print()
{
System.out.println( "no = " + no +
", model = " + model);
}
}
class Main
{
public static void swap(Car c1, Car c2)
{
Car temp = c1;
c1 = c2;
c2 = temp;
}
public static void main(String[] args)
{
Car c1 = new Car( 101 , 1 );
Car c2 = new Car( 202 , 2 );
swap(c1, c2);
c1.print();
c2.print();
}
}
|
Output
no = 1, model = 101
no = 2, model = 202
Output explanation: As we can see from the above output, the objects are not swapped. We have discussed in a previous post that parameters are passed by value in Java. So when we pass c1 and c2 to swap(), the function swap() creates a copy of these references.
Method 2: Wrapper Class
If we create a wrapper class that contains references of Car, we can swap cars by swapping references of the wrapper class.
Example
Java
class Car {
int model, no;
Car( int model, int no)
{
this .model = model;
this .no = no;
}
void print()
{
System.out.println( "no = " + no
+ ", model = " + model);
}
}
class CarWrapper {
Car c;
CarWrapper(Car c) { this .c = c; }
}
class GFG {
public static void swap(CarWrapper cw1, CarWrapper cw2)
{
Car temp = cw1.c;
cw1.c = cw2.c;
cw2.c = temp;
}
public static void main(String[] args)
{
Car c1 = new Car( 101 , 1 );
Car c2 = new Car( 202 , 2 );
CarWrapper cw1 = new CarWrapper(c1);
CarWrapper cw2 = new CarWrapper(c2);
swap(cw1, cw2);
cw1.c.print();
cw2.c.print();
}
}
|
Output:
no = 2, model = 202
no = 1, model = 101
So a wrapper class solution works even if the user class doesn’t have access to members of the class whose objects are to be swapped.
Last Updated :
19 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...