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        

    }

これは良いように思えますが、ループが 1 回実行され、キーに関連付けられた値の長い文字列が作成されます。私はそのようにしたくありません。各値を 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

OK、完了しました...実際には、マップを反復処理する方法はたくさんありますが、これは私が望むものには最適ではありません。そのため、別のメソッドの反復の実装が変更され、スムーズに機能するようになりました。

于 2012-05-14T06:29:58.820 に答える