我正在尝试控制我的应用程序的某些权限。 昨天我学习了如何创建 Double Brace Initialization,它帮助很大。但现在我试图嵌套使用它,但我得到了一个
')' expected
来自 IDE (Android Studio)
这是我的代码:
public static final Map<String, List> ALL_PERMISSIONS = new HashMap<String, List>() {{
put("Change-maps", new ArrayList<Integer>(){{add(R.id.button_change_view);}};);
put("Stores-info-view", new ArrayList<Integer>(){{add(R.id.details_fragment);}};);
put("Competitors-layer", new ArrayList<Integer>(){{add(R.id.switch_concorrentes);}};);
}};
我是不是遗漏了什么? 这是一个糟糕的方法吗?
PS:我正在尝试这种方法,因为将来我会使用一些具有多个 View (整数)的键,以及一些具有字符串列表的键。
最佳答案
您应该格式化/缩进您的代码(在 Eclipse 中默认为 Ctrl-Shift-F
)。
您会看到您的匿名 ArrayList
类声明(在大括号外)后面不能跟分号。
这是一个可以使用的格式化示例:
public static final Map<String, List> ALL_PERMISSIONS = new HashMap<String, List>() {
{
put("Change-maps", new ArrayList<Integer>() {
{
add(R.id.button_change_view);
}
});
put("Stores-info-view", new ArrayList<Integer>() {
{
add(R.id.details_fragment);
}
});
put("Competitors-layer", new ArrayList<Integer>() {
{
add(R.id.switch_concorrentes);
}
});
}
};
注意
同时注意原始类型或抑制警告。
关于java - 使用 HashMap 和 List 进行嵌套双括号初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29946258/