1. 使用双括号初始化(Double Brace Initialization)
public static final List<Integer> FIELD_KILL_CHAIN_LIST = new ArrayList<Integer>() {{
add(1);
add(6);
}};
注意: 这种方式会创建匿名子类,在某些情况下可能不是最佳选择。
2. 使用 Arrays.asList()(推荐)
public static final List<Integer> FIELD_KILL_CHAIN_LIST = Arrays.asList(1, 6);
3. 使用 List.of()(Java 9+ 推荐)
public static final List<Integer> FIELD_KILL_CHAIN_LIST = List.of(1, 6);
4. 使用 Collections.unmodifiableList()
public static final List<Integer> FIELD_KILL_CHAIN_LIST =
Collections.unmodifiableList(Arrays.asList(1, 6));
5. 分步初始化
public static final List<Integer> FIELD_KILL_CHAIN_LIST;
static {
List<Integer> tempList = new ArrayList<>();
tempList.add(1);
tempList.add(6);
FIELD_KILL_CHAIN_LIST = Collections.unmodifiableList(tempList);
}