📜  Java中的集合swap()方法与示例

📅  最后修改于: 2022-05-13 01:55:48.017000             🧑  作者: Mango

Java中的集合swap()方法与示例

Java.util.Collections类的swap()方法用于交换指定列表中指定位置的元素。如果指定的位置相等,则调用此方法会使列表保持不变。

句法:

public static void swap(List list, int i, int j)

参数:此方法将以下参数作为参数

  • list --交换元素的列表。
  • i -要交换的一个元素的索引。
  • j -要交换的另一个元素的索引。

异常如果 i 或 j 超出范围 (i = list.size() || j = list.size()),此方法将引发IndexOutOfBoundsException

以下是说明swap()方法的示例

示例 1:

Java
// Java program to demonstrate
// swap() method for String value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of List
            List vector = new ArrayList();
 
            // populate the vector
            vector.add("A");
            vector.add("B");
            vector.add("C");
            vector.add("D");
            vector.add("E");
 
            // printing the vector before swap
            System.out.println("Before swap: " + vector);
 
            // swap the elements
            System.out.println("\nSwapping 0th and 4th element.");
            Collections.swap(vector, 0, 4);
 
            // printing the vector after swap
            System.out.println("\nAfter swap: " + vector);
        }
 
        catch (IndexOutOfBoundsException e) {
            System.out.println("\nException thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// swap() method for IndexOutOfBoundsException
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of List
            List vector = new ArrayList();
 
            // populate the vector
            vector.add("A");
            vector.add("B");
            vector.add("C");
            vector.add("D");
            vector.add("E");
 
            // printing the vector before swap
            System.out.println("Before swap: " + vector);
 
            // swap the elements
            System.out.println("\nTrying to swap elements"
                               + " more than upper bound index ");
            Collections.swap(vector, 0, 5);
 
            // printing the vector after swap
            System.out.println("After swap: " + vector);
        }
 
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
Before swap: [A, B, C, D, E]

Swapping 0th and 4th element.

After swap: [E, B, C, D, A]

示例 2:对于IndexOutOfBoundsException

Java

// Java program to demonstrate
// swap() method for IndexOutOfBoundsException
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of List
            List vector = new ArrayList();
 
            // populate the vector
            vector.add("A");
            vector.add("B");
            vector.add("C");
            vector.add("D");
            vector.add("E");
 
            // printing the vector before swap
            System.out.println("Before swap: " + vector);
 
            // swap the elements
            System.out.println("\nTrying to swap elements"
                               + " more than upper bound index ");
            Collections.swap(vector, 0, 5);
 
            // printing the vector after swap
            System.out.println("After swap: " + vector);
        }
 
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Before swap: [A, B, C, D, E]

Trying to swap elements more than upper bound index 
Exception thrown : java.lang.IndexOutOfBoundsException: Index: 5, Size: 5