2012-05-12 53 views
0

我在Java中的Hashmap中有一個關鍵值對,我以下面的方式遍歷Map。在散列圖中擺脫逗號

ArrayList<String> mysection = new ArrayList<String>(); 
    ArrayList<String> temp = new ArrayList<String>(); 

    Iterator it = Map.entrySet().iterator(); 

    while (it.hasNext()){ 
     Map.Entry pairs = (Map.Entry)it.next();     
     System.out.println(" = " + pairs.getValue());     

     mysection.add(pairs.getKey().toString()); 
     temp.add(pairs.getValue().toString()); 

     it.remove(); // avoids a ConcurrentModificationException   

    } 

這看起來不錯,但循環運行一次並創建一個與密鑰關聯的長串值。我不想那樣。我希望每個值都存儲在ArrayList中,但不會以這種方式發生。所有值都存儲在第一個索引本身中。

其次,即使我嘗試分叉,我也找不到合適的方法。首先,我想通過「,」運算符分割字符串,但字符串本身包含多個「,」請建議我應該專門爲第一個問題做些什麼,我不希望所有值都是單個字符串。

感謝

登錄:

        05-12 12:14:01.387: I/System.out(433): EAMCET: caution on punctuality, 
      05-12 12:14:01.387: I/System.out(433): Delay by even a minute can cost one year for the aspirants, warn the officials making it clear that they need to report at least 45 minutes in advance for the EAMCET to be held on May 12. Th... 
      05-12 12:14:01.387: I/System.out(433): , 
      05-12 12:14:01.387: I/System.out(433): Shankar Rao takes on Kiran , 
      05-12 12:14:01.387: I/System.out(433): Former Minister P. Shankar Rao on Friday opened a new front in his fight against Chief Minister N. Kiran Kumar Reddy by submitting a notice of breach of privilege against the latter for preventing... 
      05-12 12:14:01.387: I/System.out(433): , 
      05-12 12:14:01.458: I/System.out(433): Police fear more Maoist attacks , 
      05-12 12:14:01.458: I/System.out(433): Uneasy calm prevails in villages tucked away along the shores of the Godavari adjoining neighbouring Chhattisgarh after the Maoists conducted a â??Praja Courtâ? in the interior Mukunur village of Maha... 
      05-12 12:14:01.458: I/System.out(433): , 
      05-12 12:14:01.458: I/System.out(433): 'Science in danger of regressing' , 
      05-12 12:14:01.458: I/System.out(433): Askok Ganguly delivers 'A.V. Rama Rao Tech Award Lecture' 
      05-12 12:14:01.458: I/System.out(433): , 
      05-12 12:14:01.458: I/System.out(433): Global firms pick up ISB students , 
      05-12 12:14:01.458: I/System.out(433): Average annual salary offered is Rs.18.83 lakh, 8 p.c. more than last year 
      05-12 12:14:01.458: I/System.out(433): , 
      05-12 12:14:01.458: I/System.out(433): Telugu varsity to make its courses job-oriented, 
      05-12 12:14:01.458: I/System.out(433): Potti Sreeramulu Telugu University is planning to attract more students to pursue higher education by offering employment oriented courses.The university was exploring chances to embed info... 
      05-12 12:14:01.458: I/System.out(433): , 
      05-12 12:14:01.458: I/System.out(433): Kiran sharpens attack on Jagan, 
      05-12 12:14:01.458: I/System.out(433): Ruling Congress has launched a three-cornered attack on its two arch rivals -- YSRCP president Y.S. Jaganmohan Reddy and TDP chief N. Chandrababu Naidu at Tirupati on Friday in its run up to the b... 
      05-12 12:14:01.458: I/System.out(433): , 
      05-12 12:14:01.458: I/System.out(433): RINL to get Central nod soon for mining in Rajasthan, 
      05-12 12:14:01.458: I/System.out(433): The Centre will give its nod soon for granting lease for iron ore mining to Rashtriya Ispat Nigam Limited (RINL), the corporate entity of Visakhapatnam Steel Plant. 
      05-12 12:14:01.458: I/System.out(433): â??After the Rajasthan g... 

下面是輸出的圖像: enter image description here

+2

您應該添加代碼以明確您的代碼中的temp和mysection是什麼 – Tom

+1

我們無法通過'mysection'或'temp'告訴您正在做什麼。請給出一個簡短的*完整的*程序(只是普通的Java)來說明問題。 –

+1

你應該解釋你是如何得出這個結論的。 –

回答

0

好的,我已經完成了...實際上有很多方法可以遍歷地圖,但這並不是最適合我想要的。所以改變了現在其工作流暢的另一種方法的迭代實現。

1

我試圖理解的代碼。 我懷疑問題出在地圖對象上,以及如何爲它填充數據。從你的陳述中,也許你的地圖只有一個長字符串的條目。

因此......至少,您可以發佈地圖數據填充代碼。希望解決這個問題不會那麼困難。