Использование List.InsertRange () или List.Insert (), по-видимому, редактирует список, из которого он вставляет - PullRequest
0 голосов
/ 02 мая 2019

Я пытаюсь вставить 2D-список в другой 2D-список, начиная с точки, указанной x, y.

ex:

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направление, но ломается, когда у> 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);
    }
}

Обратите внимание, что это было записано виспользовать в движке Unity, поэтому, если вы тестируете это на консоли, вам нужно изменить метод «Start» на «Main» и «Debug.Log» в методах Print.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...