2017-12-26 5 views
-4

변경 :목록 내가이 기능에 문제가있을 C#을

static List<int> transposition(List<List<int>> main_list, int ammount) 
    { 
     List<int> return_list = new List<int>(); 
     List<int> b = new List<int>(); 
     List<List<int>> new_list = new List<List<int>>(); 
     new_list = main_list; 
     int i = 0; 
     int j = 0; 
     int x = 0; 
     int key = 0; 
     Random rnd = new Random(); 
     foreach (List<int> subList in new_list) 
     { 
      x = subList[2]; 
     } 
     while (i < ammount) 
     { 
      i++; 
      key = rnd.Next(0, x + 1); 
      while (j < new_list.Count) 
      { 
       if (key >= new_list[j][1] && key <= new_list[j][2]) 
       { 
        List<int> one_element = new List<int>(); 
        one_element.Add(new_list[j][0]); 
        one_element.Add(new_list[j][1]); 
        one_element.Add(new_list[j][2]); 
        one_element.Add(new_list[j][3]); 
        if (j != 0) 
        { 
         b = new_list[j - 1]; 
         new_list[j - 1] = one_element; 
         new_list[j] = b; 
        } 
       } 
       j++; 
      } 
     } 
     foreach (List<int> element in new_list) 
     { 
      return_list.Add(element[3]); 
     } 
     return return_list; 
    } 

당신은 내가이 함수에 'main_list'을 통과하고있어 문제가 볼 수 있듯이이이 목록의 변화 함수. 나는 파이썬에서 같은 문제를 겪었고 거기에 ': new_list = main_list [:]'와 같이 [:]을 추가하여 문제를 해결했다. 하지만 C#에서이 작업을 수행하는 방법을 찾지 못했습니다. 어떤 제안? 대신

List<List<int>> new_list = new List<List<int>>(); 
new_list = main_list; 

+4

"복사본"목록의 변경 내용은 원본 목록을 반영합니다 - C#] (https://stackoverflow.com/questions/39633104/changes-made-on-copy-list-are-reflecting-original- list-c-sharp) –

+1

왜 그 개체를 사용하고 싶지 않을 때 목록을 인스턴스화합니까? – Sunil

답변

-1

List<List<int>> new_list = new List<List<int>>(main_list); 

이 main_list에서 컨텐츠를 취 새 목록을 생성을한다. main_list의 독립 사본을 만들었습니다.

관련 문제