Deleting A Specified Element In An Array Using Random Class

后端 未结 4 1019
余生分开走
余生分开走 2021-01-29 12:37
class Check
{
     public static void Main()
     {         
              int[] arr1 = new int[] { 1, 2, 3 };
              Console.WriteLine(\"The Number That Left Us          


        
4条回答
  •  悲哀的现实
    2021-01-29 13:13

    Arrays cannot be resized, if you want to remove items use a List.

    However, you can create a new one. If you want to keep all items but one at the random index:

    arr1 = arr1.Where((i, index) => index != r).ToArray();
    

    With a list you can use RemoveAt which is more efficient than creating arrays:

    var list = new List { 1, 2, 3 };
    list.RemoveAt(r);
    

提交回复
热议问题