HOWTO · Java

Java 中的交换方法

本教程解释了 java 中的 swap 方法。

swap() 方法用于交换 Java 中两个元素、字符或对象的位置。这个方法可以应用于一个列表、一个字符串或一个对象。在本文中,我们将讨论 swap() 方法在的使用。

  1. 交换列表中的两个元素
  2. 交换字符串中的两个字符
  3. 交换两个对象

使用 swap 方法交换 Java 列表中的两个元素

该方法用于在不影响列表中其他元素的情况下,交换定义位置上的两个特定元素。如果指定的索引之一高于列表的大小,那么该方法将返回一个 out of bound 异常。swap() 将给出一个包含交换的索引中元素的列表输出。

语法

public static void swap(List mylist, int m, int n)

该方法使用三个参数作为参数-将应用 swap() 方法的列表和要交换的两个索引。

代码示例:

import java.util.ArrayList;
import java.util.Collections;

public class PerformSwap {
  public static void main(String[] args) {
    ArrayList<String> lettersList = new ArrayList<String>();
    lettersList.add("a");
    lettersList.add("b");
    lettersList.add("c");
    lettersList.add("d");
    lettersList.add("e");
    lettersList.add("f");
    lettersList.add("g");
    System.out.println("Original List: " + lettersList);
    Collections.swap(lettersList, 0, 6);
    System.out.println("\nSwapped List: " + lettersList);
  }
}

输出:

Original List: [a, b, c, d, e, f, g]

Swapped List: [g, b, c, d, e, f, a]

在上面的例子中,我们将索引 6 上的字母 g 和索引 0 上的字母 a 进行了交换。swap 方法只交换了这两个字母,而没有干扰任何其他列表元素。

交换 Java 字符串中的两个字符

字符串值的一个主要属性是它是不可变的,也就是说它不能被改变。为了执行交换操作,我们首先要将 String 对象复制到 StringBuilder 或字符数组中。这两种数据类型允许我们对复制的对象进行交换操作。下面,我们将使用 char 数组和 StringBuilder 执行交换操作,以创建一个新的字符串和交换字符。

在 Java 中使用 char 数组执行字符串互换操作

swap 方法有三个参数-我们要执行交换的字符串和我们要交换的字符的两个索引。为了执行字符交换,我们首先创建一个临时字符存储对象-tempo。这个临时对象在我们将第一个字符替换为第二个字符时,将其存储起来,然后将这个字符传递给第二个字符,完成交换过程。

其中涉及三个步骤:

  • 将 String 转换为 char 数组对象; 2.
  • 获取对象的长度
  • 交换 char 数组的索引

代码示例。

public class SwapString {
  static char[] swap(String mystring, int i, int j) {
    char ch[] = mystring.toCharArray();
    char tempo = ch[i];
    ch[i] = ch[j];
    ch[j] = tempo;
    return ch;
  }

  public static void main(String args[]) {
    String theS = "Favourite";

    System.out.println(swap(theS, 5, 2));
    System.out.println(swap(theS, 0, theS.length() - 1));
    System.out.println(theS);
  }
}

输出:

Farouvite
eavouritF
Favourite

在 Java 中使用 StringBuilder 执行字符串交换

代码示例。

public class SwapString {
  static String swap(String mystring, int i, int j) {
    StringBuilder mysb = new StringBuilder(mystring);
    mysb.setCharAt(i, mystring.charAt(j));
    mysb.setCharAt(j, mystring.charAt(i));
    return mysb.toString();
  }

  public static void main(String args[]) {
    String theS = "Favorite";

    System.out.println(swap(theS, 5, 2));
    System.out.println(swap(theS, 0, theS.length() - 1));

    // Original String
    System.out.println(theS);
  }
}

输出:

Faiorvte
eavoritF
Favorite

交换 Java 中的两个对象

swap 方法也可用于交换两个对象的属性。对象交换可以在一个属性的对象上进行,也可以在多个属性的对象上进行。

交换具有一个属性的对象

假设我们有一个名为 House 的类,它有一些属性,如卧室的数量和浴室的数量。让我们创建两个 House 的对象-house1house2House 只有一个属性-value。我们的目标是交换 house1house2 的数据。

代码示例:

public class SwapObjects {
  public static void swap(House house1, House house2) {
    House temp = house1;
    house1 = house2;
    house2 = temp;
  }

  public static void main(String[] args) {
    House house1 = new House();
    House house2 = new House();

    house1.value = 5;
    house2.value = 2;

    // swap using objects
    swap(house1, house2);
    System.out.println(house1.value + ", " + house2.value);
  }
}
class House {
  public int value;
}

输出:

5, 2

在 Java 中交换对象有一个以上的属性

我们使用一个 Wrapper 类来交换两个有多个属性的对象的属性。如果我们在不使用封装类的情况下进行交换,函数 swap 只会创建一个对象引用的副本。

public class SwapObjects {
  public static void main(String[] args) {
    House house1 = new House(5, 3);
    House house2 = new House(2, 1);

    Wrapper whs1 = new Wrapper(house1);
    Wrapper whs2 = new Wrapper(house2);

    // swap using wrapper of objects
    swap(whs1, whs2);
    whs1.house.print();
    whs2.house.print();
  }

  public static void swap(Wrapper whs1, Wrapper whs2) {
    House temp = whs1.house;
    whs1.house = whs2.house;
    whs2.house = temp;
  }
}
class House {
  int bedroom, bathroom;

  // Constructor
  House(int bedroom, int bathroom) {
    this.bedroom = bedroom;
    this.bathroom = bathroom;
  }

  // Utility method to print object details
  void print() {
    System.out.println("bedrooms = " + bedroom + ", bathrooms = " + bathroom);
  }
}
class Wrapper {
  House house;
  Wrapper(House house) {
    this.house = house;
  }
}

输出:

bedrooms = 2, bathrooms = 1
bedrooms = 5, bathrooms = 3

即使成员的类不给用户类访问权,包装类也会交换对象。在对对象应用 swap() 方法时,可以根据对象中的属性数量来选择使用的方法。