Remove element of a regular array

前端 未结 15 2590
野性不改
野性不改 2020-11-22 10:06

I have an array of Foo objects. How do I remove the second element of the array?

I need something similar to RemoveAt() but for a regular array.

15条回答
  •  梦谈多话
    2020-11-22 10:46

    I use this method for removing an element from an object array. In my situation, my arrays are small in length. So if you have large arrays you may need another solution.

    private int[] RemoveIndices(int[] IndicesArray, int RemoveAt)
    {
        int[] newIndicesArray = new int[IndicesArray.Length - 1];
    
        int i = 0;
        int j = 0;
        while (i < IndicesArray.Length)
        {
            if (i != RemoveAt)
            {
                newIndicesArray[j] = IndicesArray[i];
                j++;
            }
    
            i++;
        }
    
        return newIndicesArray;
    }
    

提交回复
热议问题