我想結合一些指標與他們對應的字符串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]
我需要一些幫助來弄清楚我在這裏犯了什麼錯誤?或者我只是沒有把它們正確地打印出來?任何幫助將不勝感激。
謝謝
丹尼爾
謝謝你做到了。謝謝大家的幫助,我很高興我能問這些簡單的問題,而且人們花時間回答。 – DVM 2012-01-03 14:14:58
@DanielV:雖然這確實解決了問題,但我仍然建議您使用番石榴。你不喜歡你的代碼是簡單的:'sign.put(a,index);'?這就是Multimap會給你的。 – 2012-01-03 14:32:53
哦,我會,但我被要求至少現在不要使用任何「外部工具」(爲什麼我不知道)。 – DVM 2012-01-03 14:43:47