我在 Netbeans 中对 Integer 进行了不必要的拳击

3 java autoboxing integer netbeans

我的 IDE 向 Integer 发出了不必要的装箱警告。

 // Custom
 double[] Cvalues = {18,1,0,0,17};
 methodParams.put(Integer.valueOf(this.getCustom()), Cvalues);
Run Code Online (Sandbox Code Playgroud)

ΦXo*_*a ツ 5

如果您有一个Map<Integer, Object> myMap并且您执行以下操作:

Map<Integer, Object> myMap = new HashMap<>();
myMap.put(Integer.valueOf(1), "A");
myMap.put(Integer.valueOf(10), "B");
myMap.put(Integer.valueOf(13), "C");
Run Code Online (Sandbox Code Playgroud)

那么编译器将生成警告,因为不需要使用 Integer 类进行 Wrapping ...

这样做就足够了:

myMap.put(1, "A");
myMap.put(10, "B");
myMap.put(13, "C");
Run Code Online (Sandbox Code Playgroud)

在你的情况下

methodParams.put(Integer.valueOf(this.getCustom()), Cvalues);
Run Code Online (Sandbox Code Playgroud)

看起来getCustom()方法正在返回一个整数原语,使得原语的装箱/包装变得不必要。

做就是了:

methodParams.put(getCustom(), Cvalues);
Run Code Online (Sandbox Code Playgroud)