【问题标题】:CustomList class, what is a good way to remove at a specified index?CustomList 类,在指定索引处删除的好方法是什么?
【发布时间】:2015-12-08 11:24:25
【问题描述】:

我创建了一个 CustomList 类,其中包含一些方法,例如 Add()、Set() 和 RemoveAt()。此 CustomList 类旨在模仿 List 类的行为,而无需实际使用它。 在调试我的程序时,我注意到当我输入要删除的字符串的索引时,我的代码成功地删除了该字符串。但是当我调用 print 方法时,它会双重打印数组中的最后一个字符串。我假设错误在 RemoveAt() 而不是 Print() 中,因为 Print() 在未调用 RemoveAt() 时工作​​得很好。我想知道是否有人能指出我正确的方向。

class CustomList
{
    private int count;
    private String[] data;

    public int Count
    {
        get { return count; }
        set { value = count; }
    }
    public CustomList(int arrayNum)
    {
        data = new String[arrayNum];
    }
    public CustomList(): this(4)
    {
    }

    public void Add (String item)
    {
        if (count > data.Length)
        {
            String[] temp = new String[count * 2];
            for (int i = 0; i < data.Length; i++)
            {
                temp[i] = data[i];
            }
            data = temp;
        }
        data[count] = item;
        count++;
    }

    public int IndexOf (String item)
    {
        for (int i = 0; i < data.Length; i++)
        {
            if (data[i].Contains(item))
            {
                return i;
            }
        }
        return -1;
    }

    public bool Contains (String item)
    {
        if (IndexOf(item) == -1)
        {
            return false;
        }
        return true;
    }

    public void RemoveAt(int index)
    {
        if (index < count && index >= 0)
        {
            Array.Copy(data, index + 1, data, index, Count - (index + 1));
            count--;
        }
    }

    public bool Remove(String item)
    {
        if (data.Contains(item))
        {
            int index = Array.IndexOf(data, item);
            RemoveAt(index);
            return true;
        }
        return false;
    }

    public void Print()
    {
        for (int i = 0; i < count; i++)
        {
            Console.WriteLine(data[i]);
        }
    }

@germi,我对你所说的有点困惑。当我将大小设置为 4 并输入 4 个字符串时,它成功打印了数组中的 4 个项目:

【问题讨论】:

  • 模仿只需实现适当的接口(参见IList);
  • 您的Print没有工作正常。尝试将四个项目添加到 CustomList(不设置 ctor 中的容量),然后打印它。
  • @germi 总是提示用户输入列表的大小。我对你的建议有点困惑。
  • @corinne 你看到我的回答了吗?我想我可以确切地看到出了什么问题。
  • @GaryMcGill 是的,我看到了你的答案。为了清楚起见,您是在建议我将

标签: c# arrays arraylist indexof


【解决方案1】:

您的打印循环应该从 0 到 count-1,而不是从 0 到 count。如果您有 3 个项目 (count == 3),则这些项目的索引为 0、1、2。

由于您实现Remove 的方式,您只能避免访问太多项目,这不会缩小数组(因此该索引处仍有一个现在未使用的元素)。

&lt;= 更改为&lt;

另外,RemoveAt 方法应该是 count--,而不是 Count--

【讨论】:

  • 我进行了建议的更改,但似乎仍然无法使用 RemoveAt()。我用控制台的图像更新了我的帖子。当我在调用 RemoveAt(int index) where index = 1 后打印时,“pasta”从列表中删除,但列表随后打印为比萨饼、面包、薯条、薯条。
  • @corinne:是的,如果你把它改成&lt;(我看到你已经这样做了),那就足够了。
  • 好的,我知道还有另一个问题:您的RemoveAt 方法执行Count-- 而我认为您的意思是count--。换句话说,您不会更改 count 成员。
  • 我认为因为 Count 属性有一个“集合”,所以 Count-- 的作用与 count-- 相同。我想我应该更多地了解属性。无论如何,将其更改为计数 - 工作。 RemoveAt() 现在成功地删除了输入索引处的项目。谢谢
  • @corinne -- 运算符,当应用于这样的属性值时,将减少该属性返回的值的本地副本(然后立即丢弃)。如果您想通过该属性来执行此操作,则需要进行显式分配,例如 Count = Count - 1
猜你喜欢
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 2011-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-13
相关资源
最近更新 更多