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...

这是输出的图像: 在此处输入图像描述

4

2 回答 2

1

我试着理解代码。我怀疑问题在于地图对象以及您如何为其填充数据。从您的声明中,您的地图可能只有 1 个带有长字符串的条目。

所以...至少,您可以发布地图数据填充的代码。希望解决这个问题不会那么难。

于 2012-05-12T07:25:28.473 回答
0

好的,我完成了...实际上有很多方法可以迭代地图,这不是最适合我想要的。所以改变了另一种方法的迭代实现,现在它的工作顺利。

于 2012-05-14T06:29:58.820 回答