如何将值从一个 Java 类复制到具有相同属性的另一个类

这是我的类结构:(省略了 getter 和 setter)


public class A { 

    public List<QuestionTemplate> qTemplateList;

}


public class QuestionTemplate {

    public List<QuestionList> qList;

}


public class QuestionList {

    public String questionText;

    public String questionChoice;

}


----------------------------------------------------


public class B { 

    public List<QuestionTemplate> qTemplateList;

}


public class QuestionTemplate {

    public List<QuestionList> qList;

}


public class QuestionList {

    public String questionText;

    public String questionChoice;

我想手动将数据从 B 类复制到 A 类,手动复制字段而不是使用任何类型的映射器。


我尝试遍历列表并尝试从一个列表复制到另一个列表(从最内部的列表开始但遇到了很多问题。请原谅我是新手。我试过了。请帮忙。


拉风的咖菲猫
浏览 148回答 1
1回答

三国纷争

此代码需要 Java 7 或更高版本。选项 A 通过迭代手动复制数据:&nbsp; &nbsp; A a = new A();&nbsp; &nbsp; ..&nbsp; &nbsp; List<QuestionTemplate> templateListCopy = new LinkedList<>();&nbsp; &nbsp; for (QuestionTemplate template : a.qTemplateList) {&nbsp; &nbsp; &nbsp; &nbsp; List<QuestionList> questionListCopy = new LinkedList<>();&nbsp; &nbsp; &nbsp; &nbsp; for (QuestionList question : template.qList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QuestionList questionCopy = new QuestionList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; questionCopy.questionText = question.questionText;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; questionCopy.questionChoice = question.questionChoice;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; questionListCopy.add(questionCopy);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; QuestionTemplate questionTemplateCopy = new QuestionTemplate();&nbsp; &nbsp; &nbsp; &nbsp; questionTemplateCopy.qList = questionListCopy;&nbsp; &nbsp; &nbsp; &nbsp; templateListCopy.add(questionTemplateCopy);&nbsp; &nbsp; }&nbsp; &nbsp; B b = new B();&nbsp; &nbsp; b.qTemplateList = templateListCopy;选项 B 修改类并添加复制方法,使实现代码不那么混乱:class A {&nbsp; &nbsp; public List<QuestionTemplate> qTemplateList;&nbsp; &nbsp; public A copy() {&nbsp; &nbsp; &nbsp; &nbsp; A copy = new A();&nbsp; &nbsp; &nbsp; &nbsp; List<QuestionTemplate> questionTemplateListCopy = new ArrayList<>(qTemplateList.size());&nbsp; &nbsp; &nbsp; &nbsp; for (QuestionTemplate questionTemplate : qTemplateList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; questionTemplateListCopy.add(questionTemplate.copy());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; copy.qTemplateList = questionTemplateListCopy;&nbsp; &nbsp; &nbsp; &nbsp; return copy;&nbsp; &nbsp; }}class QuestionTemplate {&nbsp; &nbsp; public List<QuestionList> qList;&nbsp; &nbsp; public QuestionTemplate copy() {&nbsp; &nbsp; &nbsp; &nbsp; QuestionTemplate copy = new QuestionTemplate();&nbsp; &nbsp; &nbsp; &nbsp; List<QuestionList> qListCopy = new ArrayList<>(qList.size());&nbsp; &nbsp; &nbsp; &nbsp; for (QuestionList questionList : qList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qListCopy.add(questionList.copy());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; copy.qList = qListCopy;&nbsp; &nbsp; &nbsp; &nbsp; return copy;&nbsp; &nbsp; }}class QuestionList {&nbsp; &nbsp; public String questionText;&nbsp; &nbsp; public String questionChoice;&nbsp; &nbsp; public QuestionList copy() {&nbsp; &nbsp; &nbsp; &nbsp; QuestionList copy = new QuestionList();&nbsp; &nbsp; &nbsp; &nbsp; copy.questionText = questionText;&nbsp; &nbsp; &nbsp; &nbsp; copy.questionChoice = questionChoice;&nbsp; &nbsp; &nbsp; &nbsp; return copy;&nbsp; &nbsp; }}执行:A a = new A();..B b = new B();b.qTemplateList = a.copy().qTemplateList;

函数式编程

请参考此&nbsp;通过反射将一个类中的字段的所有值复制到另一个类那里已经回答了类似的问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java