1
        HashMap<String, String> apos = new HashMap<String, String>();
        apos.put("i'm","I am");
        apos.put("can't","cannot");
        apos.put("couldn't","could not");
        String[] st = new String[]{"i'm","johny"};
        Iterator itr = apos.entrySet().iterator();
        for (int i = 0; i < st.length; i++) {
            while((itr).hasNext()) {
                if (itr.equals(st[i]))
                {
                    st[i].replace(st[i],??????(value of the matched key))
                }   
            }

            }

文字列をハッシュマップと比較し、キーと一致する場合は単語をハッシュマップ値に置き換えます。上記は私がやろうとしていることです。キーの代わりに何を書くべきか、誰か助けてください。

助けていただければ幸いです。ありがとう

4

2 に答える 2

2
  1. 配列値がマップのキーであるかどうかを調べるために、マップを反復処理する必要はありません。Map#containsKey()そのためにメソッドを使用します。したがって、そのイテレータを取り除きます。

    if (map.containsKey(s[i]))
    
  2. 差し替えは一切必要ありません。=演算子を使用して、配列インデックスに新しい値を割り当てるだけです。

    s[i] = newValue;
    
  3. 配列に設定する特定のキーの値をマップから取得するには、Map#get(Object)メソッドを使用します。

    map.get(s[i]);
    
于 2013-09-24T17:25:55.747 に答える
0

これを試してください:st[i].replace(st[i],??????(value of the matched key))

使用する

   if(st[i].equals((String)itr.getKey()))
     st[i] = (String)itr.getValue(); 

使用方法の詳細については、このチュートリアルを参照してください。

于 2013-09-24T17:30:41.750 に答える