2016-11-26 68 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