Add to list at certain index
<p>I'm having a problem with some list manipulation. I take the user's input and search through it: if i find an "=" sign i assume that the string in front of it is the name of a variable , so on the line right above that variable i want to add a new string to the user's input (in this case it is called "tempVAR", doesn't really matter though). I've been trying to do this with StringBuilder but without any success , so i currently am trying to do it with ArrayLists but I am getting stuck at adding new elements to the list. Because of the way list.add(index,string) works , the elements to the right of what i am adding will always add +1 to their index. Is there a way to always know exactly what index i am looking for even after a random number of string has been added? Here is my code so far, if you run it you will see what i mean, instead of "tempVAR" or "tempVar1" being added above the name of the variable they will be added one or to positions in the wrong way.</p>

<pre><code>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&lt;String&gt; referenceList = new ArrayList&lt;String&gt;();
    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&lt;String&gt; test = new ArrayList&lt;String&gt;();
        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 &gt; 35 ) {\r\n"
                + " varX = P_PERIOD ;\r\n"
                + " }\r\n"
                + " if ( AGE_YEARS &lt; 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&lt;String&gt; test = Arrays.asList(newString);
        StringBuilder strBuilder = new StringBuilder();
        HashMap&lt;String, List&lt;Integer&gt;&gt; signs = new HashMap&lt;String, List&lt;Integer&gt;&gt;();
        HashMap&lt;String, List&lt;Integer&gt;&gt; references = new HashMap&lt;String, List&lt;Integer&gt;&gt;();
        HashMap&lt;Integer, Integer&gt; indexesOfStringAndList = new HashMap&lt;Integer, Integer&gt;();
        List&lt;String&gt; testList = new ArrayList&lt;String&gt;();
        List&lt;Integer&gt; lastList = new ArrayList&lt;Integer&gt;();
        List&lt;Integer&gt; indexList = new ArrayList&lt;Integer&gt;();
        List&lt;Integer&gt; refList = new ArrayList&lt;Integer&gt;();
        List&lt;String&gt; keysList = new ArrayList&lt;String&gt;();
        List&lt;List&gt; minList = new ArrayList&lt;List&gt;();
        String previous = null;
        int index = 0;
        Object obj = new Object();
        List&lt;Integer&gt; referenceValueList = new ArrayList&lt;Integer&gt;();
        List&lt;Integer&gt; indexPosition = new ArrayList&lt;Integer&gt;();
        String b = null;
        int indexOfa = 0;
        // System.out.println("a----&gt; " + test);
        List&lt;String&gt; anotherList = new ArrayList(test);
        for (int i = 0; i &lt; 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&lt;Integer&gt;();
            }

            if (referenceList.contains(a)) {
                indexList.add(index);
                if (references.containsKey(a)) {
                    references.get(a).add(index);
                } else {
                    references.put(a, indexList);
                }
                indexList = new ArrayList&lt;Integer&gt;();
            }
        }
        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---&gt; " + previous + " at position " + anotherList.indexOf(previous));
                anotherList.add(anotherList.indexOf(previous) - 1, "\r\ntempVAR=");

            }
        }
        strBuilder.setLength(0);
        for (int j = 0; j &lt; 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 ----&gt; " + stream);
        return stream;

    }

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

            if (diff &lt; min) {
                min = diff;
                closest = v;
            }
        }

        return closest;
    }
}
</code></pre>

<p>I've mapped the positions of the "=" and "if" to their positions in the StringBuilder, but these are remnants from when i was trying to use a stringBuilder to do what i said above. </p>

<p>I have been struggling with this for a few days now and still haven't managed to do what i need, i am not sure where i am going wrong. At the moment i am hellbent on making this work as it is (with either lists or string builder) after which , if there is a better way i will look into that and adapt this accordingly.</p>

<p>The addDelimiters() method is a method i created to avoid writing the string as you see it in "String text2" but i took that out for this because it would only clutter my already chaotic code even more :), i don't think it has any relevance to why what i am trying to do is not working. </p>

<p>TLDR: at the line above front of every varX or varY or other "var" i would like to be able to add a string to the list but i think my logic in getting the variable names or in adding to the list is wrong. </p>
