【问题标题】:Get first element of LinkedList<T>获取 LinkedList<T> 的第一个元素
【发布时间】:2012-06-11 11:41:11
【问题描述】:

我是一名初级程序员,我在 C# 中遇到了这个问题。解决方案可能很简单,但这不是我能决定的。

我有一个继承 LinkedList 的自定义类,我需要一个方法来返回第一个元素并将其从列表中删除。 代码:

class CustomClass : LinkedList<CustomElement>
{
    public CustomElement getFirstElement(){
        //here is the problem and I don't know how to solve it
        CustomElement ce = this.First;
        this.RemoveFirst();
        return first;
    }
}

问题是this.First 返回 LinkedListNode。我试过这个:

LinkedListNode<CustomElement> first = this.First;

但是返回语句失败,因为方法类型是CustomElement

【问题讨论】:

  • 如果您想在读取第一个元素时移除(出列)第一个元素,请考虑使用队列。
  • 不知道为什么会有这么多反对意见。通过谷歌立即找到它,它包含我解决问题所需的确切信息。

标签: c# linked-list


【解决方案1】:

documentation 中所述,LinkedListNode&lt;T&gt;Value 属性可用于访问存储在列表项中的值。因此,分配CustomElement ce = this.First.Value;

【讨论】:

    猜你喜欢
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多