深度复制并返回Java实例

深度复制并返回Java实例,java,class,clone,deep-copy,Java,Class,Clone,Deep Copy,所以我正在做一个大学一年级的作业问题,我会诚实地回答。我只是想在你们投票表决我的问题之前把事情弄清楚。我不需要完整的代码,我只需要一些帮助 问题分为两部分。第一部分是编写一个核苷酸类,其构造函数有两个属性。称为base的单个字符必须是'A'或'c'或'g'或't',否则它应该是'n'和一个称为degenerate的布尔值 我的这部分代码如下: class Nucleotide { private char base; private boolean degenerate;

所以我正在做一个大学一年级的作业问题,我会诚实地回答。我只是想在你们投票表决我的问题之前把事情弄清楚。我不需要完整的代码,我只需要一些帮助

问题分为两部分。第一部分是编写一个核苷酸类,其构造函数有两个属性。称为base的单个字符必须是'A'或'c'或'g'或't',否则它应该是'n'和一个称为degenerate的布尔值

我的这部分代码如下:

class Nucleotide {
    private char base;
    private boolean degenerate;

    public nucleotide(char base, boolean degenerate){
      if(base != ‘a’ || base != ‘c’ || base != ‘g’ || base != ’t’){
        this.base = ’n’;
      } else {
          this.base = base;
      }
      this.degenerate = degenerate;
    }
}
class Bacteria {
  //private ArrayList<Nucleotide> genome;
  private String species;

  public Bacteria(String species, ArrayList<Nucleotide> genome) {
    genome = new ArrayList<Nucleotide>();
    this.species = species;
  }
问题的下一部分是使用核苷酸对象并创建一个新的细菌类。细菌的实例由一个基因组(核苷酸的集合)和一个物种(字符串)组成

您必须创建一个接受字符串和集合的构造函数,并使用它们来 初始化物种和核苷酸集合。 我的这部分代码如下:

class Nucleotide {
    private char base;
    private boolean degenerate;

    public nucleotide(char base, boolean degenerate){
      if(base != ‘a’ || base != ‘c’ || base != ‘g’ || base != ’t’){
        this.base = ’n’;
      } else {
          this.base = base;
      }
      this.degenerate = degenerate;
    }
}
class Bacteria {
  //private ArrayList<Nucleotide> genome;
  private String species;

  public Bacteria(String species, ArrayList<Nucleotide> genome) {
    genome = new ArrayList<Nucleotide>();
    this.species = species;
  }
类细菌{
//私有ArrayList基因组;
私有串种;
公共细菌(字符串物种,ArrayList基因组){
基因组=新的ArrayList();
这个物种=物种;
}
我的问题从下一步开始,这一步要求我们编写一个执行深度复制并返回细菌实例的实例方法

公共细菌二元裂变(){

我如何在没有序列化和反射的情况下执行深度复制。我几乎不知道这些事情


我还需要关于如何完成
二进制裂变()的指针或基本想法
方法。我已经讨论了几个正在进行的深度复制问题,但没有一个与我的问题相关,因此我不相信我问的是重复的问题。我很乐意提供更多细节。

由于
核苷酸
没有设置符,并且它的字段是原始的,因此它可以有效地免疫不可更改(不能更改,因此可以安全地“重用”)。最好将字段设置为
final
,以使其形式上不可更改

制作深度拷贝所需的只是制作
核苷酸列表的浅拷贝,并将其用于新的
细菌中。您可以这样制作拷贝:

List<Nucleotide> copy = new ArrayList<>(genome);

由于
nucleotice
没有设置符,并且它的字段是原始的,因此它实际上是不可变的(不能更改,因此可以安全地“重用”)。最好将字段
final
正式设置为不可变的

制作深度拷贝所需的只是制作
核苷酸列表的浅拷贝,并将其用于新的
细菌中。您可以这样制作拷贝:

List<Nucleotide> copy = new ArrayList<>(genome);

这是手动操作的方法

public Bacteria binaryFission() {
    String speciesClone = this.species;
    ArrayList<Nucleotide> genomeClone = new ArrayList<Nucleotide>();
    //now iterate over the existing arraylist and clone each Nucleotide
    for(int index = 0; index < this.genome.size(); index++)
    {
        genomeClone.add(new Nucleotide(
                            genome.get(index).getBase(), //needs to be added to the Nucleotide class to retrieve the base variable
                            genome.get(index).getDegenerate() //needs to be added to be allowed to get its degenerate
                ));
    }

    return new Bacteria(speciesClone, genomeClone);
}
公共细菌二元裂变(){ 字符串speciesClone=this.species; ArrayList genomeClone=新的ArrayList(); //现在迭代现有的arraylist并克隆每个核苷酸 for(int index=0;index

仅供参考-您需要为您的核苷酸类私有变量添加getter才能使其工作,因为它们是私有的,没有它们细菌将无法访问它们的值。

这是手动执行的方法

public Bacteria binaryFission() {
    String speciesClone = this.species;
    ArrayList<Nucleotide> genomeClone = new ArrayList<Nucleotide>();
    //now iterate over the existing arraylist and clone each Nucleotide
    for(int index = 0; index < this.genome.size(); index++)
    {
        genomeClone.add(new Nucleotide(
                            genome.get(index).getBase(), //needs to be added to the Nucleotide class to retrieve the base variable
                            genome.get(index).getDegenerate() //needs to be added to be allowed to get its degenerate
                ));
    }

    return new Bacteria(speciesClone, genomeClone);
}
公共细菌二元裂变(){ 字符串speciesClone=this.species; ArrayList genomeClone=新的ArrayList(); //现在迭代现有的arraylist并克隆每个核苷酸 for(int index=0;index

仅供参考-您需要为您的核苷酸类私有变量添加getter,以使其工作,因为它们是私有的,没有它们细菌将无法访问它们的值。

为什么要注释掉
//私有ArrayList genome;
?创建一个新列表。遍历核酸,并创建每个核酸的副本。添加他复制到新列表,然后通过调用具有该新列表的构造函数来创建一个新的细菌实例。您不需要复制字符串,因为它是不可变的,因此可以在实例之间安全地共享。因为这将复制代码,我相信。我在构造函数中也有该代码。通过获取构造函数r的ArrayList,我可以从那个里初始化列表。我的逻辑对那个部分错了吗?@Bohemiany你们需要学习基础知识,了解字段、局部变量和参数之间的区别。你们的构造函数将列表作为参数,但完全忽略它,甚至用一个新的空列表覆盖它。你们需要用rgument,或将其存储在字段中供以后使用,否则该参数将无效。我只是在构造函数中执行这一部分,您必须创建一个接受字符串和集合的构造函数,并使用它们初始化物种和核苷酸集合。这也是我注释掉geno实例变量的原因之一我。如果我取消注释基因组并在构造器中获得一个列表,那么我将如何初始化它们?@jbnizetwo你为什么注释掉了
//private ArrayList genome;
?创建一个新列表。遍历核酸,并创建每个核酸的副本。将副本添加到新列表中,然后通过