📜  java 数组交换 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:05.711000             🧑  作者: Mango

代码示例2
public static final  void swap (T[] a, int i, int j) {
  T t = a[i];
  a[i] = a[j];
  a[j] = t;
}

public static final  void swap (List l, int i, int j) {
  Collections.swap(l, i, j);
}

private void test() {
  String [] a = {"Hello", "Goodbye"};
  swap(a, 0, 1);
  System.out.println("a:"+Arrays.toString(a));
  List l = new ArrayList(Arrays.asList(a));
  swap(l, 0, 1);
  System.out.println("l:"+l);
}