2015-06-03 50 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); 
相關問題