我有一个类的复制构造函数,但 Android Studio 代码检查抛出一个我不明白的警告:
“java.util.Arrays.copyOf(other.value, other.value.length)”的复制构造函数中对字段值的可疑赋值
public class CpuVariable extends BaseIdentifier {
private int memoryType;
private byte[] value;
public CpuVariable(@NonNull CpuVariable other) {
super(other);
this.memoryType = other.memoryType;
if (other.value != null) {
this.value = java.util.Arrays.copyOf(other.value, other.value.length);
}
}
}
Run Code Online (Sandbox Code Playgroud)
将代码更改为
this.value = other.value
Run Code Online (Sandbox Code Playgroud)
会删除警告,但这不是一个选项,因为我需要为该字段创建深层副本或克隆。
我是否编码错误,或者忽略或抑制警告是否安全?