2012-01-15 10 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内の位置への「if」が、これらは、私は上記の言ったことを行うためのStringBuilderを使用しようとしていたときからの名残ですしました。

私は数日間このことに苦しんでいますが、まだ私が必要なことをすることはできませんでした。どこが間違っているのか分かりません。現時点では、私はそれが(リストまたは文字列のどちらかのビルダーで)そのままの状態でこの仕事をするのが大好きです。もっと良い方法があれば、それを調べてこれに応じて調整します。

addDelimiters()メソッドは、 "String text2"で見たように文字列を書くのを避けるために作成したメソッドですが、これは既に混乱しているコードをさらに乱雑にするためです。それが、私がやろうとしていることがうまくいかない理由には関係がないとは思わない。

TLDR:すべてのvarXまたはvarYまたは他の "var"の前の行にある文字列をリストに追加したいのですが、変数名を取得する際、またはリストに追加する際の私の論理と思います間違っている。

+0

私はこのプログラムが何のために働くのが難しいですか? – Jivings

+0

私はその質問を理解しようとしていると信じています。私は答えを知るべきだと思う。他の多くの人がそうです。問題は、不要な情報を抽象化していないことです。私は私だけではないと信じています。私は文字列のリストを入力していますが、すべての値Y(これは私が質問を理解したもの)の前に値Xをリストに追加して操作したいと思っています。 –

+0

ありがとうございます、私は不要な情報を抽象化する作業をします。 – DVM

答えて

0

私はあなたのコードが台無しになっていることを知っていると思うし、もっとよくするためにはもっと多くの抽象化が必要だと思います。しかし、あなたはオフセット変数を維持することでそれを動作させることができ、 "int offset"と言うことができます。最初のパスの後に文字列を挿入するたびに、それをインクリメントし、リストにアクセスすると、 "list.get(index + offset);"を使用します。 Abstract syntax trees.を読んでください。これは、言語を解析し操作するための優れた方法です。

+0

ありがとう、はい、私はかなり混乱したコードを知っています。私はそれについて少しでも作業し、オフセット変数であなたの提案を試してみます – DVM

関連する問題