PMD在Sun Security规则集中有一个名为ArrayIsStoredDirectly的规则:
Constructors and methods receiving arrays should clone objects and store the copy. This prevents that future changes from the user affect the internal functionality.
这是他们的例子:
- public class Foo {
- private String [] x;
- public void foo (String [] param) {
- // Don't do this,make a copy of the array at least
- this.x=param;
- }
- }
我不认为我完全理解这个规则背后的原因.是因为传递的数组中的值可以在其他地方更改吗?传递Collection与传递数组有什么区别?