2017-05-08 269 views
2

我的IDE給Integer提供了一個不必要的裝箱警告。我在Netbeans中得到了不必要的裝箱整數

// Custom 
double[] Cvalues = {18,1,0,0,17}; 
methodParams.put(Integer.valueOf(this.getCustom()), Cvalues); 
+3

爲什麼你需要這樣的:'Integer.valueOf(this.getCustom())'?只需使用'getCustom()'。 –

+1

問題是什麼?你只是告訴我們你的IDE告訴你,你正在做不必要的拳擊。你認爲你的IDE在騙你嗎?實際上你在做不必要的拳擊。 – Michael

+0

我的IDE抱怨缺少分號。咄! –

回答

3

如果你有一個Map<Integer, Object> myMap和你做這樣的事情:因爲與Integer類的包裝

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

那麼編譯器會生成警告是沒有必要的......

它將足以做到:

myMap.put(1, "A"); 
myMap.put(10, "B"); 
myMap.put(13, "C"); 

你的情況

methodParams.put(Integer.valueOf(this.getCustom()), Cvalues); 

看起來像getCustom()方法正在返回的整數原始使原始不必要的拳擊/包裝。

只是做:

methodParams.put(getCustom(), Cvalues); 
相關問題