我有一个对象的问题。实际上我必须将一个对象转换为另一个对象。在我的 Category 对象中,这里包含一个类别列表(子类别)。此代码中的主要问题是我们必须将所有子类别(类别对象)转换为 CatgoryUI。
import java.util.ArrayList;
import java.util.List;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("In main");
TestMain tm = new TestMain();
List<Category> categories= tm.prepareList();
tm.displayCategories(categories);
List<CategoryUI> categoryList = tm.convertCategories(categories);
System.out.println("------Converted------");
tm.displayConvertedCategories(categoryList);
}
private List<Category> prepareList(){
//Category category = new Category();
List<Category> level3List1 = new ArrayList<Category>();
level3List1.add(new Category(4L, "Sentence Equalence", new ArrayList<Category>()));
level3List1.add(new Category(5L, "Antonyms", new ArrayList<Category>()));
level3List1.add(new Category(6L, "Text Completion", new ArrayList<Category>()));
List<Category> level3List2 = new ArrayList<Category>();
level3List2.add(new Category(7L, "Problem Solving", new ArrayList<Category>()));
level3List2.add(new Category(8L, "Logical Reasoning", new ArrayList<Category>()));
List<Category> level2List = new ArrayList<Category>();
level2List.add(new Category(2L, "Verbal", level3List1));
level2List.add(new Category(3L, "Quantative", level3List2));
List<Category> level1List = new ArrayList<Category>();
level1List.add(new Category(1L, "GRE", level2List));
return level1List;
}
private void displayCategories(List<Category> categories){
System.out.println("<ul>");
for(Category category : categories){
System.out.println("<li>");
System.out.println(category.getName());
System.out.println("</li>");
if(category.getSubCategory().size()>0){
displayCategories(category.getSubCategory());
}
}
System.out.println("</ul>");
}
}
package net.sankhya.debug;
import java.util.List;
public class Category {
private Long id;
private String name;
private List<Category> subCategory;
public Category(Long id, String name, List<Category> subCategory) {
super();
this.id = id;
this.name = name;
this.subCategory = subCategory;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Category> getSubCategory() {
return subCategory;
}
public void setSubCategory(List<Category> subCategory) {
this.subCategory = subCategory;
}
}
package net.sankhya.debug;
import java.util.List;
public class CategoryUI {
private Long id;
private String name;
private List<CategoryUI> subCategory;
public CategoryUI(){
}
public CategoryUI(Long id, String name, List<CategoryUI> subCategory) {
super();
this.id = id;
this.name = name;
this.subCategory = subCategory;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<CategoryUI> getSubCategory() {
return subCategory;
}
public void setSubCategory(List<CategoryUI> subCategory) {
this.subCategory = subCategory;
}
}
当您在 html 视图中使用 ul 和 li 显示类别列表时,它将如下所示。并且在转换为 CategoryUI 对象后,如果打印列表,它也应该以相同的方式播放。那么,任何人都可以就如何将Category转换为CategoryUI提出任何建议。
<ul>
<li>GRE</li>
<ul>
<li>Verbal</li>
<ul>
<li>Sentence Equalence</li>
<li>Antonyms</li>
<li>Text Completion</li>
</ul>
<li>Quantative</li>
<ul>
<li>Problem Solving</li>
<li>Logical Reasoning</li>
</ul>
</ul>
谢谢