2015-06-03 56 views
0

我有以下POJO它由以下成员,使下面的是一些成员在它转换对象的列表到字符串数组中

public class TnvoicetNotify { 
    private List<TvNotifyContact> toMap = new ArrayList<TvNotifyContact>(); 
    private List<TvNotifyContact> ccMap = new ArrayList<TvNotifyContact>(); 

} 

现在在一些其他类我得到的对象的POJO在作为参数的方法的签名类TnvoicetNotify以上如下所示..所以想要写从列表中提取的代码,现在该方法本身

public void InvPostPayNotification(TnvoicetNotify TnvoicetNotify) 
    { 

     String[] mailTo = it should contain all the contents of list named toMap 
     String[] mailCC = it should contain all the contents of list named ccMap 
    } 

内字符串数组将它们存储在上面的类我需要提取上述po中的类型为list的toMap裘命名TnvoicetNotify,我想存储每个项目,如果如以下的方式

在一个字符串数组数组列表用于在列表中例如第一项是A1和第二是A2和第三是A3 所以应该被存储在字符串数组作为

String[] mailTo = {"A1","A2","A3"}; 

同样地,我想实现CC部分同样也如上面POJO它在名单我想在下面的方式来存储

String[] mailCc = {"C1","C2","C3"}; 

所以请告诉我如何内实现这一目标InvPostPayNotification方法

+3

你应该张贴的代码对于'TvNotifyContact' –

+0

@erertgghg请阅读:[当某人回答我的问题时怎么办] – CKing

回答

2

伪代码,因为我不知道细节TnvoicetNotify

public void invPostPayNotification(final TnvoicetNotify tnvoicetNotify) 
{ 
    final List<String> mailToList = new ArrayList<>(); 
    for (final TvNotifyContact tv : tnvoicetNotify.getToMap()) { // To replace: getToMap() 
     mailToList.add(tv.getEmail()); // To replace: getEmail() 
    } 
    final String[] mailTo = mailToList.toArray(new String[mailToList.size()]) 
    // same for mailCc then use both arrays 
} 
+0

只是为了好奇,为什么'最终'?而且每次迭代如何改变? – Mordechai

+0

我错过了2;)因为我习惯了。另请参阅http://stackoverflow.com/questions/18019582/what-is-the-purpose-of-using-final-for-the-loop-variable-in-enhanced-for-loop –

+0

不错,你提醒我的乔什布洛赫。但是这不会干扰增强环路吗? – Mordechai

1

如果您使用的是Java 8,你可以简单地用一个班轮:

String[] mailCC = ccMap.stream().map(TvNotifyContact::getEmail).toArray(String[]::new); 
相关问题