Java 集合 swap(List<?>, int, int) 方法



描述

Java 集合 swap(List<?>, int, int) 方法用於交換指定列表中指定位置處的元素。

宣告

以下是 java.util.Collections.swap() 方法的宣告。

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

引數

  • list − 要交換元素的列表。

  • i − 要交換的一個元素的索引。

  • j − 要交換的另一個元素的索引。

返回值

異常

IndexOutOfBoundsException − 如果 i 或 j 超出範圍 (i < 0 || i >= list.size() || j < 0 || j >= list.size()),則丟擲此異常。

整數列表元素交換示例

以下示例演示了 Java 集合 swap(List,int, int) 方法的使用。我們建立了一個包含一些整數的 List 物件,並列印了原始列表。使用 swap(List, int, int) 方法,我們交換了列表中的元素,然後列印了更新後的列表。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5));

      System.out.println("Initial collection value: " + list);
      // swap values of this collection
      Collections.swap(list, 0, 4);
      System.out.println("Final collection value: "+list);
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Initial collection value: [1, 2, 3, 4, 5]
Final collection value: [5, 2, 3, 4, 1]

字串列表元素交換示例

以下示例演示了 Java 集合 swap(List,int, int) 方法的使用。我們建立了一個包含一些字串的 List 物件,並列印了原始列表。使用 swap(List, int, int) 方法,我們交換了列表中的元素,然後列印了更新後的列表。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<String> list = new ArrayList<>(Arrays.asList("A","B","C","D","E"));

      System.out.println("Initial collection value: " + list);
      // swap values of this collection
      Collections.swap(list, 0, 4);
      System.out.println("Final collection value: "+list);
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Initial collection value: [A, B, C, D, E]
Final collection value: [E, B, C, D, A]

物件列表元素交換示例

以下示例演示了 Java 集合 swap(List,int, int) 方法的使用。我們建立了一個包含一些 Student 物件的 List 物件,並列印了原始列表。使用 swap(List, int, int) 方法,我們交換了列表中的元素,然後列印了更新後的列表。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam")));

      System.out.println("Initial collection value: " + list);
      // swap values of this collection
      Collections.swap(list, 0, 2);
      System.out.println("Final collection value: "+list);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Initial collection value: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Final collection value: [[ 3, Adam ], [ 2, Robert ], [ 1, Julie ]]
java_util_collections.htm
廣告