I have two Java Lists (as in the actual class), each of which has the elements of a numbered list and levels, expressed as String objects. Each line below is a separate string in the List. For example, the first list could be:

```
==level 1==
1.1 2

==level 2==
1.R4 0
2.0 2

==level 3==
3.R3 1
4.R5 1

```

and the second could be:

```
==level 3==
1.null
2.null

```

The lists can each be of any length and the values after the numbers don't matter here. The goal is to merge the lists of the levels that match. For example, a merge of the two lists above would be:

```
==level 1==
1.1 2

==level 2==
1.R4 0
2.0 2

==level 3==
1.null
2.null
3.R3 1
4.R5 1

```

The numbers in the matching levels of the two lists will never be the same, so that does not need to checked for. I also believe that the numbers will always be consecutive. This is what I have so far, but it does not cover all cases, specifically when the merge takes place only at the beginning of a level, or only at the end. It is also quite unpleasant to look at. tempLines and lines are the two lists.

```
for(int x=0; x < lines.size();x++){
            for(int y=0; y < tempLines.size();y++){
                if(tempLines.get(y).equals(lines.get(x)) && !(tempLines.get(y).equals("\n"))&& !(lines.get(x).equals("\n"))){
                    int a=y+1;
                    int b=x+1;
                    while(!(tempLines.get(a).equals("\n"))){
                        while(!(lines.get(b).equals("\n"))){
                            if(Integer.valueOf(tempLines.get(a).charAt(0))==(Integer.valueOf(tempLines.get(a).charAt(0))-1))
                                lines.add(b,tempLines.get(a));
                        }
                    }
                }
            }
        }

```

Could someone please help?