Java 创建一个<;鱼>;arrayList深拷贝
无论出于何种原因,当我尝试为plant数组列表创建深度副本时,我得到了一个空指针异常,我不知道为什么Java 创建一个<;鱼>;arrayList深拷贝,java,deep-copy,copyonwritearraylist,Java,Deep Copy,Copyonwritearraylist,无论出于何种原因,当我尝试为plant数组列表创建深度副本时,我得到了一个空指针异常,我不知道为什么 /** * Copy Constructor. Since landscape is immutable in the scope of our * project, you could do a simple reference copy for it. However, Fish and * Plants are mutable, so those lists must be copi
/**
* Copy Constructor. Since landscape is immutable in the scope of our
* project, you could do a simple reference copy for it. However, Fish and
* Plants are mutable, so those lists must be copied with a DEEP copy! (In
* other words, each fish and each plant must be copied.)
*/
private ArrayList<Fish> fish;
private ArrayList<Plant> plants;
private int[][] landscape;
public Model(Model other) {
this.landscape = other.landscape;
for(Fish fishy: other.fish){
this.fish.add(new Fish(fishy));
}
for(Plant planty: other.plants){
this.plants.add(new Plant(planty));
}
}
/**
*复制构造函数。因为景观在我们的范围内是不变的
*项目,您可以为它做一个简单的参考副本。然而,鱼和鱼
*植物是可变的,所以这些列表必须用深度副本复制!(在
*换句话说,每一条鱼和每一株植物都必须复制。)
*/
私人鱼;
私人植物;
私人int[][]景观;
公共模型(模型其他){
这个景观=其他景观;
对于(鱼腥味:其他。鱼){
this.fish.add(新鱼(鱼));
}
用于(植物植物:其他植物){
this.plants.add(新植物(planty));
}
}
您尚未初始化鱼和植物
public Model(Model other) {
fish = new ArrayList<Fish>();
plants = new ArrayList<Plant>();
this.landscape = other.landscape;
for(Fish fishy: other.fish){
this.fish.add(new Fish(fishy));
}
for(Plant planty: other.plants){
this.plants.add(new Plant(planty));
}
}
公共模型(其他模型){
fish=新的ArrayList();
plants=新的ArrayList();
这个景观=其他景观;
对于(鱼腥味:其他。鱼){
this.fish.add(新鱼(鱼));
}
用于(植物植物:其他植物){
this.plants.add(新植物(planty));
}
}
您尚未初始化鱼和植物
public Model(Model other) {
fish = new ArrayList<Fish>();
plants = new ArrayList<Plant>();
this.landscape = other.landscape;
for(Fish fishy: other.fish){
this.fish.add(new Fish(fishy));
}
for(Plant planty: other.plants){
this.plants.add(new Plant(planty));
}
}
公共模型(其他模型){
fish=新的ArrayList();
plants=新的ArrayList();
这个景观=其他景观;
对于(鱼腥味:其他。鱼){
this.fish.add(新鱼(鱼));
}
用于(植物植物:其他植物){
this.plants.add(新植物(planty));
}
}
您应该初始化阵列:
public Model(Model other) {
this.landscape = other.landscape;
this.fish = new ArrayList<Fish>();
this.plants = new ArrayList<Plants>();
if (other.fish != null) {
for (Fish myFish : other.fish) {
this.fish.add(new Fish(myFish));
}
}
if (other.plants != null) {
for (Plant myPlant : other.plants) {
this.plants.add(new Plant(myPlant));
}
}
}
公共模型(其他模型){
这个景观=其他景观;
this.fish=新的ArrayList();
this.plants=新的ArrayList();
if(other.fish!=null){
对于(鱼myFish:其他。鱼){
this.fish.add(newfish(myFish));
}
}
if(other.plants!=null){
对于(植物myPlant:其他植物){
this.plants.add(newplant(myPlant));
}
}
}
另外,重要的是要确定other.fish是否为null。在您的情况下,您可能会尝试对空列表进行迭代。您应该初始化数组:
public Model(Model other) {
this.landscape = other.landscape;
this.fish = new ArrayList<Fish>();
this.plants = new ArrayList<Plants>();
if (other.fish != null) {
for (Fish myFish : other.fish) {
this.fish.add(new Fish(myFish));
}
}
if (other.plants != null) {
for (Plant myPlant : other.plants) {
this.plants.add(new Plant(myPlant));
}
}
}
公共模型(其他模型){
这个景观=其他景观;
this.fish=新的ArrayList();
this.plants=新的ArrayList();
if(other.fish!=null){
对于(鱼myFish:其他。鱼){
this.fish.add(newfish(myFish));
}
}
if(other.plants!=null){
对于(植物myPlant:其他植物){
this.plants.add(newplant(myPlant));
}
}
}
另外,重要的是要确定other.fish是否为null。在您的情况下,您可能会尝试在一个空列表上进行迭代。如果没有看到stacktrace,我不确定,但是您是否在创建模型对象时将其初始化为ArrayList e、 g:
public Model() {
fish = new ArrayList<Fish>();
plants = new ArrayList<Plant>();
}
公共模型(){
fish=新的ArrayList();
plants=新的ArrayList();
}
如果没有看到stacktrace,我不确定,但是在创建模型对象时,您是否已初始化为ArrayList
e、 g:
public Model() {
fish = new ArrayList<Fish>();
plants = new ArrayList<Plant>();
}
公共模型(){
fish=新的ArrayList();
plants=新的ArrayList();
}
您还可以粘贴构造函数吗?您还可以粘贴构造函数吗?