질문은 매우 명확하지 않다,하지만 경우 : 모든 의견
덕분에, 가장 좋은 방법은 다음과 같이 내림차순 루프를하고 있었다 목록의 임의의 인덱스에서 지정된 수의 요소를 제거하려면 여기에서 수행 할 방법입니다. (이 예는 String 객체 목록과 함께 사용되지만 물론 객체를 사용하도록 변경할 수도 있습니다.) B, C, F, H, I, J, K, M, N, O, P, Q, R, U, V, X, Y : 그것을 실행하면
private static List<String> strList = new List<String>();
static void Main(string[] args)
{
strList.AddRange("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".Split(','));
removeRandomItemsFromList(strList, 8);
Console.WriteLine(String.Join(",",strList.ToArray()));
Console.ReadLine();
}
private static void removeRandomItemsFromList(List<String> list, int numToRemove){
Random randGenerator = new Random();
if(numToRemove >= list.Count()){
Console.WriteLine("Num to remove is as big or larger than list size, clearing list.");
list.Clear();
return;
}
for (int i = 0; i < numToRemove; i++)
{
int randIndex = randGenerator.Next(list.Count - 1); //get random index within bounds of list
list.RemoveAt(randIndex);
}
}
는 I 출력있어 , Z
물론 임의의 항목을 제거하기 때문에 일반적으로 다릅니다.
제거되는 한, 어떻게 중요합니까? – John
왜 그게 당신이 원하는 것이 아니겠습니까? 내 생각 엔 가장 낮은 인덱스를 먼저 제거하고 인덱스 순서를 변경 한 다음이를 망쳐 버리는 것입니다. 제거하고 제외 할 항목의 두 번째 목록을 만들려고 했습니까? – Dassina
당신은 그들을 모두 동시에 제거해야하는 이유가 무엇입니까? –