Java 在特定索引处添加到列表中

Java 在特定索引处添加到列表中,java,Java,我对一些列表操作有问题。我获取用户的输入并进行搜索:如果我找到一个“=”符号,我假设前面的字符串是一个变量的名称,因此在该变量正上方的行中,我想向用户的输入添加一个新字符串(在本例中,它被称为“tempVAR”,其实并不重要)。我一直在尝试使用StringBuilder来实现这一点,但没有任何成功,因此我目前正在尝试使用ArrayList来实现这一点,但我一直无法将新元素添加到列表中。由于list.add(index,string)的工作方式,我要添加的内容右侧的元素将始终向其索引中添加+1。即

我对一些列表操作有问题。我获取用户的输入并进行搜索:如果我找到一个“=”符号,我假设前面的字符串是一个变量的名称,因此在该变量正上方的行中,我想向用户的输入添加一个新字符串(在本例中,它被称为“tempVAR”,其实并不重要)。我一直在尝试使用StringBuilder来实现这一点,但没有任何成功,因此我目前正在尝试使用ArrayList来实现这一点,但我一直无法将新元素添加到列表中。由于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;
    }
}
import java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map.Entry;
公共阶级的东西{
静态列表引用列表=新的ArrayList();
公共静态最终字符串半列=“;”;
公共静态最终字符串BLANK=“”;
公共静态最终字符串为空=”;
公共静态最终字符串LEFT_CURLY=“{”;
公共静态最终字符回车='\r';
公共静态最终字符串CR\u String=“回车和换行”;
公共静态最终字符新行='\n';
公共静态void main(字符串[]args){
列表测试=新建ArrayList();
String x=“AGE_x”;
字符串y=“AGE_y”;
字符串z=“年龄\年”;
字符串t=“P_期间”;
字符串w=“T\u有效”;
添加(x);
添加(y);
添加(z);
添加(t);
添加(w);
String text2=“如果(年龄>35){\r\n”
+“varX=P\u周期;\r\n”
+“}\r\n”
+“如果(年龄<35岁){\r\n”
+“varY=T\u有效;\r\n”
+“varZ=AGE_Y;\r\n”
+“varA=AGE\u X;\r\n”
+ " }";
检测质量(text2);
}
公共静态字符串检测相等(字符串文本){
字符串a=null;
//text=text.trim();
//text=testspliting.addDelimiters(text);
String[]newString=text.split(“”);
List test=Arrays.asList(新闻字符串);
StringBuilder strBuilder=新StringBuilder();
HashMap signs=新的HashMap();
HashMap references=新的HashMap();
HashMap indexesOfStringAndList=新HashMap();
List testList=new ArrayList();
List lastList=new ArrayList();
列表索引列表=新的ArrayList();
List refList=new ArrayList();
List keysList=new ArrayList();
List minList=new ArrayList();
字符串previous=null;
int指数=0;
Object obj=新对象();
List referenceValueList=新建ArrayList();
List indexPosition=new ArrayList();
字符串b=null;
int indexOfa=0;
//System.out.println(“a-->”+测试);
List anotherList=新阵列列表(测试);
for(int i=0;i”+previous+”位于“+anotherList.indexOf(previous));
anotherList.add(anotherList.indexOf(previous)-1,“\r\ntempVAR=”);
}
}
strBuilder.setLength(0);
对于(int j=0;j