如何通过交换列表中的其他数据中心将本地数据中心放在列表的第一位?

How to put local datacenter first in the list by swapping other datacenter in the list?

我有一段代码,我们尝试将本地数据中心放在第一位,然后转移所有其他数据中心,但这段代码抛出异常:

在我下面的代码中,如果 CURRENT_LOCATION 是 GHI 那么它会抛出异常 java.lang.ArrayIndexOutOfBoundsException

  public enum Colocation {
    ABC("ABC", 2), PQR("PQR", 3), DEF("DEF", 4), GHI("GHI", 5), ;

    ...
  }


public static List<Colocation> get() {
  List<Colocation> result = Arrays.asList(Colocation.ABC, Colocation.PQR, Colocation.DEF, Colocation.GHI);
  // first element in the list will always be the local datacenter
  Collections.swap(result, 0, CURRENT_LOCATION.get().ordinal());
  Collections.shuffle(result.subList(1, result.size()));
  return result;
}

我想做的是 - 无论 CURRENT_LOCATION 是什么,我想把它放在列表的第一位,其余的可以是随机的。

做:

result.remove(CURRENT_LOCATION);
result.add(0, CURRENT_LOCATION);

swap:

Collections.swap(result, 0, result.indexOf(CURRENT_LOCATION.get()));