C# 删除数组中的第一个/最后一个值

C# 删除数组中的第一个/最后一个值,c#,arrays,C#,Arrays,问题: 我正在尝试使用我编写的以下方法删除数组中的第一个值: 先走 显示界面 问题是,当我使用DisplayUI方法显示当前在values[]数组中的值时,该值没有被删除,它被设置为值[0],而现在我不知道如何解决这个问题 假设我使用addFirst方法在数组中输入以下数字:6、76、65、13 然后,当我运行removeFirst方法几次以删除数组的第一个值时,我得到以下结果: '6, 76, 13, 13' '6, 13, 13, 13' '13, 13, 13, 13' 我希望它删除6,

问题:

我正在尝试使用我编写的以下方法删除数组中的第一个值:

先走

显示界面

问题是,当我使用DisplayUI方法显示当前在values[]数组中的值时,该值没有被删除,它被设置为值[0],而现在我不知道如何解决这个问题

假设我使用addFirst方法在数组中输入以下数字:6、76、65、13

然后,当我运行removeFirst方法几次以删除数组的第一个值时,我得到以下结果:

'6, 76, 13, 13'
'6, 13, 13, 13'
'13, 13, 13, 13'
我希望它删除6,而不是用65替换13作为最后一个数组值,我不知道它为什么这样做

我希望输出为:

'76, 65, 13, 0' 
对于这个例子。因为第一个位置是空的,所有其他位置可以向上移动1

我该怎么做

问题2:

还尝试对removeLast执行相反的操作


removeLast方法的问题是,当我运行displayUI方法时,没有任何变化,它仍然认为所有的项都在数组中,如果它返回它们,它们必须在数组中。

我认为您可能希望去掉count变量,除非有其他原因保留它。这很可能会导致难以捕捉的问题,我不认为这会增加价值

由于您拥有对数组的全局访问权限,这是一种糟糕的设计,但对于初学者来说很好,因此您可以只使用.Length属性,除非禁止使用该属性

例如,您可以尝试以下操作:

/// <summary>
/// Writes the contents of the array to the console
/// </summary>
private static void DisplayArray()
{
    ThrowIfArrayNullOrEmpty();

    Console.WriteLine("Values currently in array: {0}", string.Join(", ", values));
}

/// <summary>
/// Adds the integer to the start of the array and everything else to the next index
/// </summary>
/// <param name="firstValue">The integer to add</param>
private static void AddFirst(int firstValue)
{
    // First remove the last item (this will clear a space in the first position)
    RemoveLast();

    // Then update the first item
    values[0] = firstValue;
}

/// <summary>
/// Moves all items to the index before them, and initializes the last item to zero
/// </summary>
public static void RemoveFirst()
{
    ThrowIfArrayNullOrEmpty();

    // Move each item to the index before it
    for (int i = 0; i < values.Length - 1; i++)
    {
        values[i] = values[i + 1];
    }

    // Set the last item to zero
    values[values.Length - 1] = 0;
}

/// <summary>
/// Moves all other to the next index and initializes the first item to zero
/// </summary>
public static void RemoveLast()
{
    ThrowIfArrayNullOrEmpty();

    // Move each item to the previous index. Note that we have to start with the last
    // item and work our way forward, or else we overwrite a value before we move it 
    for (int i = values.Length - 1; i > 0; i--)
    {
        values[i] = values[i - 1];
    }

    // Set the first item to zero
    values[0] = 0;
}

/// <summary>
/// Throws an exception if the array is null or empty
/// </summary>
public static void ThrowIfArrayNullOrEmpty()
{
    if (values == null) throw new Exception("Array is Null");
    if (values.Length == 0) throw new Exception("Array is Empty");
}

这与您提出的其他数组列表实现问题有何不同?如果这是家庭作业,你应该咨询你的老师,以确保你确实在学习他们想要你学习的东西。否则,就用列表吧。我不能用列表。如果可以,我会的。这听起来比这简单多了。然后这是作业。是的,但我在到期前很早就做了,整天都在努力理解这些东西,这是为一个12月底到期的项目准备的,就像我在以前的帖子中说的,我要到周三才能和我的老师再次交谈,所以我在尽可能地寻求帮助。你的删除代码没有问题,除了返回值。也许您想返回已删除的值?如果是这种情况,int value=value[0]应该在for循环之前。否则,请在其他地方查找错误。
'6, 76, 13, 13'
'6, 13, 13, 13'
'13, 13, 13, 13'
'76, 65, 13, 0' 
/// <summary>
/// Writes the contents of the array to the console
/// </summary>
private static void DisplayArray()
{
    ThrowIfArrayNullOrEmpty();

    Console.WriteLine("Values currently in array: {0}", string.Join(", ", values));
}

/// <summary>
/// Adds the integer to the start of the array and everything else to the next index
/// </summary>
/// <param name="firstValue">The integer to add</param>
private static void AddFirst(int firstValue)
{
    // First remove the last item (this will clear a space in the first position)
    RemoveLast();

    // Then update the first item
    values[0] = firstValue;
}

/// <summary>
/// Moves all items to the index before them, and initializes the last item to zero
/// </summary>
public static void RemoveFirst()
{
    ThrowIfArrayNullOrEmpty();

    // Move each item to the index before it
    for (int i = 0; i < values.Length - 1; i++)
    {
        values[i] = values[i + 1];
    }

    // Set the last item to zero
    values[values.Length - 1] = 0;
}

/// <summary>
/// Moves all other to the next index and initializes the first item to zero
/// </summary>
public static void RemoveLast()
{
    ThrowIfArrayNullOrEmpty();

    // Move each item to the previous index. Note that we have to start with the last
    // item and work our way forward, or else we overwrite a value before we move it 
    for (int i = values.Length - 1; i > 0; i--)
    {
        values[i] = values[i - 1];
    }

    // Set the first item to zero
    values[0] = 0;
}

/// <summary>
/// Throws an exception if the array is null or empty
/// </summary>
public static void ThrowIfArrayNullOrEmpty()
{
    if (values == null) throw new Exception("Array is Null");
    if (values.Length == 0) throw new Exception("Array is Empty");
}