<分区>
我在其中一个类中有一个 getter 方法:
public byte[] getBytes() {
return dataBytes;
}
Sonar 给我异常(exception)。我该如何解决这个问题?
Returning 'dataBytes' may expose an internal array
<分区>
我在其中一个类中有一个 getter 方法:
public byte[] getBytes() {
return dataBytes;
}
Sonar 给我异常(exception)。我该如何解决这个问题?
Returning 'dataBytes' may expose an internal array
最佳答案
返回数组的副本,因此对其所做的任何更改都不会反射(reflect)在原始数组中。这当然会导致性能和内存损失,因此对于大型阵列,这可能是您需要考虑的事情。
return Arrays.copyOf(dataBytes, dataBytes.length);
关于java - 返回 'dataBytes' 可能会暴露一个内部数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49370336/