2016-11-26 66 views
-4

INTRO:一直在寻找这个答案,尽管有几次搜索,但在归档中没有找到它。要么是太简单的问题,要么是做一些违反C#的规律的东西#从双向链表中的对象返回一个变量

我是新来的双向链表,他们正在做我的头!

我有问题得到一个双向链表,从列表中的对象吐出一个变量。

我试图简化一个新文件中的问题来解决我的头,但我只是找不到正确的语法来查找DLL中的对象内的变量。

我已经将它分解为DLL中的五个对象,每个对象都有一个标题。 现在我想查找一个对象并返回它的标题。 但是改为:aaaaaaaaaaaaaaaaaaarrrgh!我到底在做什么错了?

namespace ADS 
{ 
public partial class DLL_Generic_Object : Form 

{ 
    public DLL_Generic_Object() 
    { 
     InitializeComponent(); 
    } 

    //create class 

    public partial class Weapon 
    { 
     string Title; 

     public Weapon(string title) 
     {     
      this.Title = title;  
     } 

    } 

    //create objects; 

    Weapon object1 = new Weapon("Revolver"); 
    Weapon object2 = new Weapon("Candlestick"); 
    Weapon object3 = new Weapon("Lead Pipe"); 
    Weapon object4 = new Weapon("Rope"); 
    Weapon object5 = new Weapon("Knife"); 

    public class Structures 
    { 
     public string getTitle(LinkedList<Object> nameofDLL) 
     { 
      string gotTitle = "How do I Find the Title variable of an Object in the DLL?"; 

      return gotTitle; 
     } 
    } 

    //create an object Doubly Linked List 
    LinkedList<object> weapons = new LinkedList<object>(); 

    private void btnExecute_Click(object sender, EventArgs e) 
    { 
     PrintNodes(weapons); //This will show the DLL is empty. 

     //Add nodes to the list 
     weapons.AddFirst(object1); //add a node to the front of the list 

     //Add a node after a specific node. 
     weapons.AddAfter(weapons.Find(object1), object2); 
     weapons.AddAfter(weapons.Find(object2), object4); 

     //Add a node before a specific node. 
     weapons.AddBefore(weapons.Find(object4), object3); 

     //Add a node to the end of the list 
     weapons.AddLast(object5); 

     PrintNodes(weapons); // This will show the DLL has 5 Nodes. 

    // Find the value of a node 
    } 
    public string FindTitle(LinkedList<Object> nameofDLL) 
    { 
     // initialise a Structures class 
     Structures structure = new Structures(); 

     // Find the value of a node 
     string value = structure.getTitle(weapons) + "\r\n"; 

     return value; 
    } 
    public void PrintNodes(LinkedList<object> values) 
    { 
     if (values.Count != 0) //check if there are any nodes in the list 
     { 
      txtOutput.Text += "The number of nodes is: " + values.Count.ToString() + "\r\n"; 

      txtOutput.Text += FindTitle(weapons); 

     } 

     else 
      txtOutput.Text += "The Doubly Linked List is empty" + "\r\n"; 
    } 
} 

}

+0

当您不知道列表中对象的类型时使用'object'。如果列表中只包含武器,为什么不是'LinkedList '? –

+0

公平的电话。我试图保持它的通用性,但如果它工作的话,很乐意将LinkedList更改为! –

+0

它不会因为更改为而摔倒,所以非常感谢。但我仍然需要知道如何从DLL中返回标题值。我不明白我要去哪里错... –

回答

0

你似乎是从错误的角度接近问题。在双链表的特定索引处尝试获取元素是不可取的,因为这不是它的设计目的。使用DLL的目的是使用列表中的元素而不是列表本身来导航集合。例如,如果您从某个源获取给定节点,则可以使用NextPrevious属性来导航列表。

如果目标是遍历整个列表,则有两种常用方法可以实现。第一和最简单的是foreach方法:

​​

第二种方法是使用while。这种方法稍微复杂一点,但它更清楚地表明了行为和一个LinkedList的意图:

var node = linkedList.First; 
while (node != null) 
{ 
    Console.WriteLine(node.Value.Title); 
    node = node.Next; 
} 

如果你绝对需要在一个特定的指数,以获得元素,它是可能的,最简单的方法是使用ElementAtElementAtOrDefault LINQ方法。它们之间的区别是,如果给定索引超出列表范围,ElementAt将抛出异常,而ElementAtOrDefault将返回给定类型的默认值(在Weapon类型的情况下,默认值为null) - 你应该选择哪种方法取决于您希望为自己的特定实现,其行为:

int idx = 5; 
var elem = linkedList.ElementAt(idx); 

但正如我在我的评论说,上述方法基本上把LinkedList的作为常规列表,因此,如果您需要做的这经常是你需要问自己为什么你不只是使用列表。

+0

感谢您的反馈Abion47。这非常有帮助。 –

+0

@ChadLockwood如果这回答你的问题,请务必将其标记为答案。 – Abion47