0

作为一名开发人员,我处于早期阶段,需要帮助来解决 Sonar 错误,该错误表示以下代码的字段双重分配。

//Inside constructor
list1 = new ArrayList();
....
...
// in some method
  if(description.equalsIgnoreCase(MAPPED_CATIA_MODELS)) {
        tempList = new ArrayList();
        tempList = list1 ; // gives error here as "Correctness - Double assignment of field."
        addKey = true;
      }

请建议需要更改的内容。谢谢。

4

1 回答 1

2

声纳建议您删除其中之一

tempList = new ArrayList();

或者

tempList = list1;

关键是在您创建了一个 ArrayList 之后,您用对 list1 的引用覆盖了对它的引用。的结果new ArrayList()立即被丢弃并永远消失。

于 2012-05-04T06:45:14.707 回答