2016-03-31 100 views
1

因此,我正在研究一个程序,我想搜索某个单词,但我并不十分确定如何设置搜索功能。这正是我试图使用的,但它所做的只是输出我输入的任何内容。Arraylist搜索

static void searchWords(ArrayList arlWords) 
    { 
     string strInput; 

     Console.Write("Search a Word: "); 
     strInput = Console.ReadLine(); 
     arlWords.IndexOf(strInput, 0); 
     Console.WriteLine("{0}", strInput); 
    } 
+0

对于某个单词,“*”的含义是什么意思:您计划在之后计划如何处理它? –

+0

它只是搜索我已经放入的单词,并告诉我,如果我有它或不是 –

+0

因为您的方法需要在列表中,您可以开始编写代码以找到该单词在该“列表” – starcorn

回答

1

请试试这个:

static void searchWords(ArrayList arlWords) 
{ 
    string strInput; 
    bool check= false; 
    Console.Write("Search a Word: "); 
    strInput = Console.ReadLine(); 
    for (int i = 0; i < arlWords.Items.Count; i++) 
    { 
    if (arlWords.Items[i] == strInput) 
     check = true; 
    } 
    if(check) Console.WriteLine("found"); 
    else Console.WriteLine("not found"); 
} 
+0

arrSample从哪里来? –

+0

感谢您的警告,修复 –

1

您已经定义它来写 'strInput',这样它会写。相反,您应该捕获'IndexOf'的结果并打印 - 或者它是否大于0。