我想结合一些指标与他们对应的字符串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