使用List.InsertRange()或List.Insert()似乎正在编辑其从中插入的列表

时间:2019-05-02 15:25:07

标签: c# arrays list

我正尝试从x,y指定的点开始将2D列表插入另一个2D列表。

例如:

List A = {{1,1,1,1},
          {1,1,1,1},
          {1,1,1,1},
          {1,1,1,1}}
List B = {{2,2,2,2},
          {2,2,2,2},
          {2,2,2,2},
          {2,2,2,2}}

MergeLists(A, B, 0, 0) would give:
{{2,2,2,2,1,1,1,1},
 {2,2,2,2,1,1,1,1},
 {2,2,2,2,1,1,1,1},
 {2,2,2,2,1,1,1,1}}

MergeLists(A, B, 2, 0) would give:
{{1,1,2,2,2,2,1,1},
 {1,1,2,2,2,2,1,1},
 {1,1,2,2,2,2,1,1},
 {1,1,2,2,2,2,1,1}}

*My code adds an additional 0 at the end of each line but I'm not worried about that.
MergeLists(A, B, 5, 0) would give:
{{1,1,1,0,2,2,2,2},
 {1,1,1,0,2,2,2,2},
 {1,1,1,0,2,2,2,2},
 {1,1,1,0,2,2,2,2}}

MergeLists(A, B, 0, 1) would give:
{{1,1,1,1},
 {2,2,2,2,1,1,1,1},
 {2,2,2,2,1,1,1,1},
 {2,2,2,2,1,1,1,1},
 {2,2,2,2}}

MergeLists(A, B, 0, 5) would give:
{{1,1,1,1},
 {1,1,1,1},
 {1,1,1,1},
 {1,1,1,1},
 {0},
 {2,2,2,2},
 {2,2,2,2},
 {2,2,2,2},
 {2,2,2,2}}

我的代码在x方向上效果很好,但是在y> 0时中断。相反,它似乎将它合并的最后一个列表添加到将要合并的下一个列表的开头。

MergeLists(A, B, 0, 1) erroneously gives:
{{1,1,1,1},
 {2,2,2,2,1,1,1,1},
 {2,2,2,2,1,1,1,1,1,1,1,1},
 {2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1},
 {2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1}}

这是我的代码:

public class StrTester{
    private List<List<int>> grid;

    // Start is called before the first frame update
    void Start(){
        grid = new List<List<int>>();
        List<List<int>> newGrid = new List<List<int>>();
        List<int> ints = new List<int>();
        for(int i=1; i<21; i++){
            ints.Add(i);
            if(i%4 == 0){
                newGrid.Add(ints);
                ints = new List<int>();
            }
        }
        MergeLists(newGrid, 0, 0);
        Print(newGrid);
        MergeLists(newGrid, 0, 1);
        Print(grid);
    }

    private void MergeLists(List<List<int>> newInts, int x, int y){
        if(grid.Count == 0){
            grid = new List<List<int>>(newInts);
        } else {
            while(y + newInts.Count >= grid.Count){
                    grid.Add(new List<int>());
                }
            foreach(List<int> ints in grid){
                while(ints.Count < x){
                    ints.Add(0);
                }
            }
            foreach(List<int> newIntRow in newInts){
                Print(newIntRow);
                grid[y].InsertRange(x, new List<int>(newIntRow));
                Print(newIntRow);
                y++;
                Print(newIntRow);
            }
        }
    }

    private void Print(List<List<int>> printGrid){
        string str = "";
        foreach(List<int> lInt in printGrid){
            foreach(int i in lInt){
                str += i + ", ";
            }
            Debug.Log(str);
            str = "";
        }
    }

    private void Print(List<int> printList){
        string str = "";
        foreach(int i in printList){
            str += i + ", ";
        }
        Debug.Log("this line is: " + str);
    }
}

请注意,这是为在统一引擎中使用而编写的,因此,如果要在控制台上进行测试,则需要在打印中将“开始”方法更改为“主”,并将“调试。日志”更改为方法。

0 个答案:

没有答案