为什么将数组从数组转换为数组再到数组

问题描述 投票:4回答:1

我一直在使用SpringBatch并查看类的源代码

org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor<T>

我发现了这个:

public void setNames(String[] names) {
    Assert.notNull(names, "Names must be non-null");
    this.names = Arrays.asList(names).toArray(new String[names.length]);
}
  • 将数组转换为列表再转换为数组的目的是什么?
  • 为什么不使用这样的东西:
public void setNames(String[] names) {
    Assert.notNull(names, "Names must be non-null");
    this.names = names; // Simpler and without conversions
}
  • 或以此创建一个孤立的新实例:
public void setNames(String[] names) {
    Assert.notNull(names, "Names must be non-null");
    this.names = names.clone(); //Simpler and create a new instance
}

所有答案都欢迎。

java performance optimization spring-batch code-cleanup
1个回答
2
投票
需要复制的数组以使接收对象不可变。考虑以下示例,该示例未完成复制:

System.arraycopy()

© www.soinside.com 2019 - 2024. All rights reserved.