2016-12-16 44 views
-2

我有一個列表,在這個列表中我存儲了2個字符串(標題和消息)的多個數組。在列表/數組內搜索

我希望能夠搜索標題和如果標題存在我希望程序輸出整個數組(標題和消息)。

我該怎麼做?

正如你可以看到樓下它是區分#2,我需要幫助:)

List<string[]> loggBoken = new List<string[]>(); 
case 1: 

    string[] post = new string[2]; 
    post[0] = Console.ReadLine(); 
    post[1] = Console.ReadLine(); 
    loggBoken.Add(post); 

case 2: 
    **search title** 
    **go through the list** 
    **if title exists, write out the entire array** 
+0

「標題」是什麼意思?數組的第一個元素? –

+0

是的,如果我不清楚@IanH,對不起。 – LePelican

+0

你的清單是怎樣的? – 2016-12-16 21:32:19

回答

0

這應該做的伎倆:

string title = Console.ReadLine(); 
foreach (string[] arr in loggBroken) 
{ 
    if (arr[0] == title) Array.ForEach(arr, s => Console.WriteLine(s)); 
} 
+0

非常感謝!如果我嚴重解釋我的問題,我很抱歉。 – LePelican

0

LINQ的做法是:

// find the array that on the first position contains your title in question 
string[] match = loggBokken.Find(x=>x[0].Contains("your title")); 
// print joining the content of the array separated by a space 
Console.WriteLine(String.Join(" ", match)); 
+0

非常感謝! :) – LePelican

+0

@LePelican不客氣。你實際上用正常的語言編寫了整個算法。下次嘗試以此爲您的主角,並嘗試將您的單詞放入代碼中:'for''if if ... ...請不要忘記標記幫助您的答案。 - –