2012-01-15 94 views
0

我遇到了一些列表操作问题。我接受用户的输入并搜索它:如果我找到一个“=”符号,我假设它前面的字符串是一个变量的名称,所以在该变量上面的那一行上,我想添加一个新的字符串用户的输入(在这种情况下,它被称为“tempVAR”,虽然并不重要)。我一直试图用StringBuilder来做到这一点,但没有任何成功,所以我目前正在尝试使用ArrayLists来做到这一点,但我在向列表添加新元素时遇到困难。由于list.add(index,string)的工作方式,我所添加的元素右侧的元素将始终为其索引添加+1。有没有办法总是知道我正在寻找什么索引,即使在添加了随机数的字符串之后?这里是我的代码到目前为止,如果你运行它,你会明白我的意思,而不是“tempVAR”或“tempVar1”被添加到变量的名称上方,他们将被添加一个或错误的方式位置。添加到特定索引

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collections; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map.Entry; 


public class ToTestStuff { 

    static List<String> referenceList = new ArrayList<String>(); 
    public static final String SEMICOLUMN = ";"; 
    public static final String BLANK = " "; 
    public static final String EMPTY = ""; 
    public static final String LEFT_CURLY = "{"; 
    public static final char CARRIAGE_RETURN = '\r'; 
    public static final String CR_STRING = "CARRIAGE_RETURN_AND_NEW_LINE"; 
    public static final char NEW_LINE = '\n'; 

    public static void main(String[] args) { 

     List<String> test = new ArrayList<String>(); 
     String x = "AGE_X"; 
     String y = "AGE_Y"; 
     String z = "AGE_YEARS"; 
     String t = "P_PERIOD"; 
     String w = "T_VALID"; 
     referenceList.add(x); 
     referenceList.add(y); 
     referenceList.add(z); 
     referenceList.add(t); 
     referenceList.add(w); 

     String text2 = " if (AGE_YEARS > 35) {\r\n" 
       + " varX = P_PERIOD ;\r\n" 
       + " }\r\n" 
       + " if (AGE_YEARS < 35) {\r\n" 
       + " varY = T_VALID ;\r\n" 
       + " varZ = AGE_Y ;\r\n" 
       + " varA = AGE_X ;\r\n" 
       + " }"; 

     detectEquals(text2); 
    } 

    public static String detectEquals(String text) { 
     String a = null; 
     // text = text.trim(); 
     // 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>>(); 
     HashMap<String, List<Integer>> references = new HashMap<String, List<Integer>>(); 
     HashMap<Integer, Integer> indexesOfStringAndList = new HashMap<Integer, Integer>(); 
     List<String> testList = new ArrayList<String>(); 
     List<Integer> lastList = new ArrayList<Integer>(); 
     List<Integer> indexList = new ArrayList<Integer>(); 
     List<Integer> refList = new ArrayList<Integer>(); 
     List<String> keysList = new ArrayList<String>(); 
     List<List> minList = new ArrayList<List>(); 
     String previous = null; 
     int index = 0; 
     Object obj = new Object(); 
     List<Integer> referenceValueList = new ArrayList<Integer>(); 
     List<Integer> indexPosition = new ArrayList<Integer>(); 
     String b = null; 
     int indexOfa = 0; 
     // System.out.println("a----> " + test); 
     List<String> anotherList = new ArrayList(test); 
     for (int i = 0; i < anotherList.size(); i++) { 
      a = anotherList.get(i).trim(); 
      index = strBuilder.length();// - a.length(); 
      // index = i; 
      strBuilder.append(a); // "=", 3 - if, 14 - while, 36 , "=", 15 
      testList.add(a); 


      if (a.equals("if") || a.equals("=")) { 
       lastList.add(i); 
       indexOfa = i; 
       indexesOfStringAndList.put(index, indexOfa); 

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

      if (referenceList.contains(a)) { 
       indexList.add(index); 
       if (references.containsKey(a)) { 
        references.get(a).add(index); 
       } else { 
        references.put(a, indexList); 
       } 
       indexList = new ArrayList<Integer>(); 
      } 
     } 
     for (String k : references.keySet()) { 
      keysList.add(k); 
      referenceValueList = references.get(k); 
      obj = Collections.min(referenceValueList); 
      int is = (Integer) obj; 
      ArrayList xx = new ArrayList(); 
      xx.add(new Integer(is)); 
      xx.add(k); 
      minList.add(xx); 

     } 

     for (List q : minList) { 
      Integer v = (Integer) q.get(0); 
      String ref = (String) q.get(1); 
      int x = closest(v, indexPosition); 
      int lSize = anotherList.size(); 
      int sizeVar = lSize - test.size(); 
      int indexOfPx = 0; 
      int px = 0; 
      if (x != 0) { 
       px = indexesOfStringAndList.get(x) - 1; 
      } else { 
       px = indexesOfStringAndList.get(x); 
      } 

      if (px == 0) { 
       System.out.println("previous when x=0 " +anotherList.get(px+sizeVar)); 
       anotherList.add(px, "tempVar1=\r\n"); 

      } else { 
       previous = anotherList.get(px + sizeVar); 
       System.out.println("previous is---> " + previous + " at position " + anotherList.indexOf(previous)); 
       anotherList.add(anotherList.indexOf(previous) - 1, "\r\ntempVAR="); 

      } 
     } 
     strBuilder.setLength(0); 
     for (int j = 0; j < anotherList.size(); j++) { 
      b = anotherList.get(j); 
      strBuilder.append(b); 
     } 
     String stream = strBuilder.toString(); 
    // stream = stream.replaceAll(CR_STRING, CARRIAGE_RETURN + EMPTY + NEW_LINE); 
     System.out.println("after ----> " + stream); 
     return stream; 

    } 

    public static int closest(int of, List<Integer> in) { 
     int min = Integer.MAX_VALUE; 
     int closest = of; 
     for (int v : in) { 
      final int diff = Math.abs(v - of); 

      if (diff < min) { 
       min = diff; 
       closest = v; 
      } 
     } 

     return closest; 
    } 
} 

我绘制了“=”和位置“如果”他们在StringBuilder的位置,但这些都是从当我试图使用StringBuilder做我上面说的残余。

我一直在为此奋斗了几天,仍然没有设法做我需要的东西,我不知道我错了哪里。目前,我非常想让这项工作(无论是列表还是字符串生成器)完成后,如果有更好的方法,我会研究并相应地进行调整。

addDelimiters()方法是我创建的一种方法,用于避免在“String text2”中看到字符串,但我为此解决了这一问题,因为它只会混乱我已经混乱的代码,甚至更多:)不要认为它与我为什么试图做不起作用有任何关联。

TLDR:在每个varX或varY或其他“var”前面的行我想能够添加一个字符串到列表,但我认为我的逻辑获取变量名称或添加到列表是错的。

+0

我很难搞清楚这个程序可能是什么? – Jivings 2012-01-15 15:53:45

+0

相信我我正试图理解这个问题。我想我应该知道答案。就像许多其他人一样。问题是你没有把不必要的信息抽象出来,这让我不仅相信我。如果你试图简化如下问题:我已经输入了带有字符串的列表,我想操纵它,在每个值Y之前在列表中添加值X(这是我理解的问题),有人可能会帮助你。 – 2012-01-15 16:03:48

+0

谢谢你,我会努力将不必要的信息抽象出来。 – DVM 2012-01-15 16:12:53

回答

0

我想我们都知道你的代码搞砸了,你需要更多的抽象才能使它更好。但是你可以通过维护一个偏移量来使它工作,比如说“int offset”。每次在初始传递之后插入一个字符串时,都会增加它,并且当您访问使用它的列表时,“list.get(index + offset);”。请阅读Abstract syntax trees.,这是解析和操作语言的好方法。

+0

谢谢,是的,我知道我相当混乱的代码。我会稍微处理一下,并尝试使用偏移量变量的建议 – DVM 2012-01-15 16:13:35

相关问题