如何在Java中交换或交换对象?
为了理解如何在Java中交换对象,让我们考虑下面的示例: 如下:
插图:
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?
方法:
- 使用 OOPS 的概念
- 使用Java的 Wrapper 类
方法一:使用OOPS的概念
在这里,我们将简单地交换成员,让我们直接为我们将玩的示例“汽车”插图。所以如果类'Car'只有一个整数属性说“no”(车号),我们可以通过简单地交换两辆车的成员来交换汽车。
示例 1-A
Java
// Java program to demonstrate that we can swap two
// objects be swapping members
// Class 1
// Number class Car
class Car {
// Attributes associated with car
int no;
Car(int no) { this.no = no; }
}
// Class 2
// Uses Car objects
class GFG {
// Method 1
// To swap
public static void swap(Car c1, Car c2)
{
int temp = c1.no;
c1.no = c2.no;
c2.no = temp;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating car class objects(creating cars)
Car c1 = new Car(1);
Car c2 = new Car(2);
// Calling method 1
swap(c1, c2);
// Print and display commands
System.out.println("c1.no = " + c1.no);
System.out.println("c2.no = " + c2.no);
}
}
Java
// Java program to demonstrate that we can swap two
// objects be swapping members
// Where it does not work
// Class 1
// A car with number and name
class Car {
// Attributes of Car class
int model, no;
// Constructor
Car(int model, int no)
{
// This keyword is used to refer
// current instance itself
this.model = model;
this.no = no;
}
// Method of this class
// To print Car
void print()
{
// Printing number and model of car
System.out.println("no = " + no +
", model = " + model);
}
}
// Class 2
// A class that uses Car
class Main
{
// swap() doesn't swap c1 and c2
public static void swap(Car c1, Car c2)
{
Car temp = c1;
c1 = c2;
c2 = temp;
}
// Driver method
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();
}
}
Java
// Java program to Demonstrate that Wrapper Classes
// Can be Used to Swap two Objects
// Class 1
// A car with model and no.
class Car {
// Attributes associated with car
int model, no;
// Constructor of class 1
Car(int model, int no)
{
// This refers to current instance itself
this.model = model;
this.no = no;
}
// Method
// To print object details
void print()
{
System.out.println("no = " + no
+ ", model = " + model);
}
}
// Class 2
// Wrapper over class that is used for swapping
class CarWrapper {
Car c;
// Constructor
CarWrapper(Car c) { this.c = c; }
}
// Class 3
// Uses Car class and swaps objects of Car
// using CarWrapper
class GFG {
// This method swaps car objects in wrappers
// cw1 and cw2
public static void swap(CarWrapper cw1, CarWrapper cw2)
{
Car temp = cw1.c;
cw1.c = cw2.c;
cw2.c = temp;
}
// Main driver method
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();
}
}
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?
示例 1-B
Java
// Java program to demonstrate that we can swap two
// objects be swapping members
// Where it does not work
// Class 1
// A car with number and name
class Car {
// Attributes of Car class
int model, no;
// Constructor
Car(int model, int no)
{
// This keyword is used to refer
// current instance itself
this.model = model;
this.no = no;
}
// Method of this class
// To print Car
void print()
{
// Printing number and model of car
System.out.println("no = " + no +
", model = " + model);
}
}
// Class 2
// A class that uses Car
class Main
{
// swap() doesn't swap c1 and c2
public static void swap(Car c1, Car c2)
{
Car temp = c1;
c1 = c2;
c2 = temp;
}
// Driver method
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();
}
}
no = 1, model = 101
no = 2, model = 202
输出说明:从上面的输出中我们可以看出,对象没有交换。我们在上一篇文章中讨论过Java中的参数是按值传递的。因此,当我们将 c1 和 c2 传递给 swap() 时,函数swap() 会创建这些引用的副本。
方法二:包装类
如果我们创建一个包含 Car 引用的包装类,我们可以通过交换包装类的引用来交换汽车。
例子
Java
// Java program to Demonstrate that Wrapper Classes
// Can be Used to Swap two Objects
// Class 1
// A car with model and no.
class Car {
// Attributes associated with car
int model, no;
// Constructor of class 1
Car(int model, int no)
{
// This refers to current instance itself
this.model = model;
this.no = no;
}
// Method
// To print object details
void print()
{
System.out.println("no = " + no
+ ", model = " + model);
}
}
// Class 2
// Wrapper over class that is used for swapping
class CarWrapper {
Car c;
// Constructor
CarWrapper(Car c) { this.c = c; }
}
// Class 3
// Uses Car class and swaps objects of Car
// using CarWrapper
class GFG {
// This method swaps car objects in wrappers
// cw1 and cw2
public static void swap(CarWrapper cw1, CarWrapper cw2)
{
Car temp = cw1.c;
cw1.c = cw2.c;
cw2.c = temp;
}
// Main driver method
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();
}
}
输出:
no = 2, model = 202
no = 1, model = 101