2013-06-28 158 views
-2

我遇到了对象问题。其实我必须将一个对象转换为另一个对象。这里在我的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对象,它也应该以相同的方式播放。所以,可以请任何建议,如何将类别转换为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> 

感谢

+0

我的问题与您的问题无关,但为什么不让CategoryUI和Category实现相同的接口? –

+0

我同意@ C.Champagne,那将是正确的做法。我的回答是基于这是不可能做到的假设。 – wattostudios

回答

1

写自己的转换方法,这样的事情...

public List<CategoryUI> convertArray(List<Category> categoryArray){ 
    List<CategoryUI> convertedArray = new ArrayList<CategoryUI>(); 

    for (int i=0;i<categoryArray.size();i++){ 
     convertedArray.add(convertObject(categoryArray.get(i))); 
    } 

    return convertedArray; 
} 


public CategoryUI convertObject(Category category){ 
    return new CategoryUI(category.getId(),category.getName(),convertArray(category.getSubCategory())); 
} 

只需拨打convertObject(category)为要转换的每个对象。你有意识地创建一个正确类型的新对象,并用旧对象中的所有数据填充它。它还转换subCategories的数组。如果你传入树的顶层节点,那么这个调用将会遍历树的所有子类并转换它们,所以它很好用,很简单。

1

如果您需要在应用程序中更频繁地使用它,您也可以考虑Dozer 它会自动执行映射。