2012-01-03 74 views
0

我想結合一些指標與他們對應的字符串HashMap的關鍵,所以我試圖用一個HashMap像這樣:具有多個值

public class NewClass { 
static List<String> referenceList = new ArrayList<String>(); 

public static void main(String[] args) { 

    String x = "AGE_X"; 
    String y = "AGE_Y"; 
    //String w = "AGE_Z"; 
    referenceList.add(x); 
    referenceList.add(y); 

    String text1 = "if (AGE_X = = 10){\r\n" 
      + " j = 1;" 
      + " AGE_X = 5 ;" 
      + " if (true) {" 
      + " m = 4/AGE_Y ;" 
      + " }" 
      + " }"; 
    detectEquals(text1); 
} 

public static String detectEquals(String text) { 
    String a = null; 
    // text = TestSplitting.addDelimiters(text); 
    String[] newString = text.split(" "); 
    List<String> test = Arrays.asList(newString); 
    StringBuilder strBuilder = new StringBuilder(); 
    HashMap<String, List<Integer>> signs = new HashMap<String, List<Integer>>(); 
    List<Integer> refList = new ArrayList<Integer>(); 
    int index = 0; 

    for (int i = 0; i < test.size(); i++) { 
     a = test.get(i).trim(); 
     //System.out.println("a=" + a); 
     strBuilder.append(a); 
     index = strBuilder.length() - a.length(); 

     if (a.equals("if") || a.equals("=")) {   
      refList.add(index); 
      // System.out.println(refList); 
      signs.put(a, refList); 
      refList = new ArrayList<Integer>(); 

      System.out.println(signs); 
     } 
    } 
    return a; 
} 
} 

但我沒有任何與它的運氣。我想,「如果」讓每一個和該字符串(文本)「=」,並在一起映射,但似乎我做錯了什麼,因爲我的輸出是這樣的:

{如果= [ 0]}

{如果= [0],== [8]}

{如果= [0],== [9]}

{如果= [0],== [012]

{if = [0],== [23]}

{如果= [26],== [23]}

{如果= [26],== [36]}

我尋找類似:

如果= [0,26]和= = [8,9,15,23,23,36]

我需要一些幫助來弄清楚我在這裏犯了什麼錯誤?或者我只是沒有把它們正確地打印出來?任何幫助將不勝感激。

謝謝

丹尼爾

回答

0

試試這個

if (a.equals("if") || a.equals("=")) {   

      // System.out.println(refList); 
      if(signs.containsKey(a)){ 
       signs.get(a).add(index); 
      } 
      else{ 
       refList.add(index); 
       signs.put(a, refList); 
      } 
      refList = new ArrayList<Integer>(); 

      System.out.println(signs); 
     } 
+0

謝謝你做到了。謝謝大家的幫助,我很高興我能問這些簡單的問題,而且人們花時間回答。 – DVM 2012-01-03 14:14:58

+1

@DanielV:雖然這確實解決了問題,但我仍然建議您使用番石榴。你不喜歡你的代碼是簡單的:'sign.put(a,index);'?這就是Multimap會給你的。 – 2012-01-03 14:32:53

+0

哦,我會,但我被要求至少現在不要使用任何「外部工具」(爲什麼我不知道)。 – DVM 2012-01-03 14:43:47

3

聽起來像是你想有一個multimap - 我建議你使用Guava及其Multimap接口的實現,所以你不必重新發明輪子:)

我並不清楚你想要做什麼,但是你現有的代碼總是在之後創建一個新列表在地圖中添加對舊列表的引用...我不知道你爲什麼要做那。我期望更多的東西一樣:

  • 檢查是否a已經存在在地圖上
    • 如果是這樣的一個關鍵,獲取價值(現有列表),並添加index
    • 如果沒有,創建一個新的列表,把在地圖上,並添加index
+0

我不認爲他想多地圖,他想用一個鍵和多個值的HashMap中。問題是他沒有重複使用列表並在循環中重新創建它。 – Manoj 2012-01-03 14:00:25

+0

謝謝,我目前正在尋找谷歌MultiMap。但有沒有辦法做到這一點,而不使用這些? – DVM 2012-01-03 14:00:48

+0

@Manoj:一個具有多個值的鍵聽起來像是一個multimap對我來說...... – 2012-01-03 14:02:47

0

改變這樣

代碼
public static String detectEquals(String text) { 
      String a = null; 
      // text = TestSplitting.addDelimiters(text); 
      String[] newString = text.split(" "); 
      List<String> test = Arrays.asList(newString); 
      StringBuilder strBuilder = new StringBuilder(); 
      HashMap<String, List<Integer>> signs = new HashMap<String, List<Integer>>(); 
      List<Integer> refList = new ArrayList<Integer>(); 
      int index = 0; 

      for (int i = 0; i < test.size(); i++) { 
       a = test.get(i).trim(); 
       //System.out.println("a=" + a); 
       strBuilder.append(a); 
       index = strBuilder.length() - a.length(); 

       if (a.equals("if") || a.equals("=")) { 
        // System.out.println(refList); 
        refList = signs.get(a); 
        if(refList == null) { 
         refList = new ArrayList<Integer>(); 
        } 
        refList.add(index); 

        signs.put(a, refList); 

       } 
      } 
      System.out.println(signs); 
      return a; 
     } 
+0

請注意,當您更新現有列表時,不需要調用'signs.put';你可以把它放在'if'語句中。 – 2012-01-03 14:22:41

0
public static void main(String[] args) 
{ 
    String x = "AGE_X"; 
    String y = "AGE_Y"; 
    referenceList.add(x); 
    referenceList.add(y); 
    String text1 = "if (AGE_X = = 10){\r\n" + " j = 1;" + " AGE_X = 5 ;" + " if (true) {" + " m = 4/AGE_Y ;" 
      + " }" + " }"; 
    detectEquals(text1); 
} 

public static String detectEquals(String text) 
{ 
    String a = null; 
    String[] newString = text.split(" "); 
    List<Integer> tests = new ArrayList<Integer>(); 
    List<Integer> equals = new ArrayList<Integer>(); 
    int index = 0; 
    for(int i = 0; i < newString.length; i++) 
    { 
     a = newString[i].trim(); 
     //System.out.println("a=" + a); 
     if(a.equals("if")) 
     { 
      tests.add(index); 
     } 
     else 
     { 
      if(a.equals("=")) 
      { 
       equals.add(index); 
      } 
     } 
     index += newString[i].length(); 
    } 
    print("if", tests); 
    print("=", equals); 
    return a; 
} 

private static void print(String s, List l) 
{ 
    System.out.print(s+"=["); 
    Iterator i = l.iterator(); 
    if(i.hasNext()) System.out.print(i.next()); 
    while(i.hasNext()) 
     System.out.print(","+i.next()); 
    System.out.println("]"); 
}