C# quickest way to shift array

前端 未结 20 1310
礼貌的吻别
礼貌的吻别 2020-12-01 01:35

How can I quickly shift all the items in an array one to the left, padding the end with null?

For example, [0,1,2,3,4,5,6] would become [1,2,3,4,5,6,null]

Ed

20条回答
  •  悲&欢浪女
    2020-12-01 02:16

    Try this! using Linq. No need of second Array.

            var i_array = new int?[] {0, 1, 2, 3, 4, 5, 6 };
    
            i_array = i_array.Select((v, k) => new { v = v, k = k }).
                Where(i => i.k > 0).Select(i => i.v).ToArray();
    
            Array.Resize(ref i_array, i_array.Length + 1);
    

    Output: [0,1,2,3,4,5,6] would become [1,2,3,4,5,6,null]

提交回复
热议问题