正在删除数组中的第一个/最后一个值

本文关键字:第一个 最后一个 删除 数组 | 更新日期: 2023-09-27 18:28:47

问题:

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

removeFirst()

 public int removeFirst() //removes the first item from the array
        {
            if (isEmpty())
                throw new Exception("List is Empty");

            count--;
            for (int i = 0; i < count; i++)
            {
                values[i] = values[i + 1];

            }
            int value = values[0];
            return value;
        }

Removelast()

public int removeLast() //todo //removes the last item from the array
        {
            if (isEmpty())
        throw new Exception("List is Empty");
    count--;
    return values[count];
        }

displayUI()

public void displayUI()//displays contents of list
        {
            Console.Write("Values currently in array: ");
            foreach (var item in values)
                Console.Write( item.ToString() + ", ");

        }

问题是,当我使用DisplayUI()方法向我显示当前在values[]数组中的值时,该值不会被删除,而是被设置为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");
}
相关文章: