【问题标题】:Removing the first/last value in an array删除数组中的第一个/最后一个值
【发布时间】:2014-11-03 10:58:12
【问题描述】:

问题:

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

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;
        }

删除最后一个()

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 方法时,没有任何变化,它仍然认为所有项目都在数组中,如果它返回它们,它们一定是。

【问题讨论】:

  • 这与stackoverflow.com/questions/26703096/… 以及您提出的其他数组列表实现问题有何不同?如果这是家庭作业,您应该咨询您的老师,以确保您实际上正在学习他们希望您学习的内容。否则,只需使用List&lt;T&gt;
  • 我不能使用列表。如果我可以的话我会。听起来比这简单得多。
  • 那么这是家庭作业。
  • 是的,但是我在到期之前就这样做了,整天都在努力理解这些东西,这是为一个将于 12 月下旬到期的项目设置的,就像我之前所说的那样帖子,直到星期三我才能再次和我的老师说话,所以我会尽可能地寻求帮助。
  • 您的删除代码没有任何问题,除了返回值。也许您想返回已删除的值?如果是这种情况,int value = values[0] 应该在 for 循环之前。否则在别处寻找错误。

标签: c# arrays


【解决方案1】:

我认为您可能想要摆脱 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");
}

【讨论】:

    猜你喜欢
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多