我有一段代码,我们尝试将本地数据中心放在第一位,然后移动所有其他数据中心,但此代码抛出了异常:
在我下面的代码中,如果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是什么,我想把它放在列表的第一位,其余的可以是随机的。
发布于 2019-05-09 14:33:46
执行以下操作:
result.remove(CURRENT_LOCATION);
result.add(0, CURRENT_LOCATION);或使用swap
Collections.swap(result, 0, result.indexOf(CURRENT_LOCATION.get()));https://stackoverflow.com/questions/56053446
复制相似问题