C#抛出错误中的数组搜索

C#抛出错误中的数组搜索,c#,.net,csc,C#,.net,Csc,我一直在玩C#游戏,试图让基本的东西顺利完成,但我被这个奇怪的错误难住了。我试图搜索一个名称数组,然后返回任何匹配项的位置。目前我正在打印姓名,但我能找到位置 但由于某种原因,当我键入要搜索的名称并按enter键时,程序崩溃,并显示“ArraySearch.exe已停止工作”。你知道怎么了吗 恕我直言,我对这种语言/范式还是新手:p 谢谢!:) 使用系统; 公开课考试 { 公共静态void Main() { string[]stringArray={“Nathan”、“Bob”、“Tom”

我一直在玩C#游戏,试图让基本的东西顺利完成,但我被这个奇怪的错误难住了。我试图搜索一个名称数组,然后返回任何匹配项的位置。目前我正在打印姓名,但我能找到位置

但由于某种原因,当我键入要搜索的名称并按enter键时,程序崩溃,并显示“ArraySearch.exe已停止工作”。你知道怎么了吗

恕我直言,我对这种语言/范式还是新手:p

谢谢!:)

使用系统;
公开课考试
{
公共静态void Main()
{   
string[]stringArray={“Nathan”、“Bob”、“Tom”};
bool[]matches=新bool[stringArray.Length];
字符串searchTerm=Console.ReadLine();

对于(int i=1;i
i
将达到3,这将在此处给出超出范围的异常:

matches[i] = true;
使用调试很容易发现这一点。(尝试学习使用它。非常有用)


匹配项
有3个元素,因此索引范围从0到2。

i
将达到3,这将在此处给出超出范围的异常:

matches[i] = true;
using System;

public class Test
{
    public static void Main()
    {
        string[] stringArray = { "Nathan", "Bob", "Tom" };
        bool[] matches = new bool[stringArray.Length];
        string searchTerm = "Bob";
        for (int i = 1; i <= stringArray.Length; i++)
        {
            if (searchTerm == stringArray[i - 1])
            {
                matches[i - 1] = true;
            }
        }
        for (int i = 1; i <= stringArray.Length; i++)
        {
            if (matches[i - 1])
            {
                Console.WriteLine("We found another " + stringArray[i - 1]);
            }
        }
    }
}
使用调试很容易发现这一点。(尝试学习使用它。非常有用)

匹配
有3个元素,因此索引范围为0到2。

使用系统;
using System;

public class Test
{
    public static void Main()
    {
        string[] stringArray = { "Nathan", "Bob", "Tom" };
        bool[] matches = new bool[stringArray.Length];
        string searchTerm = "Bob";
        for (int i = 1; i <= stringArray.Length; i++)
        {
            if (searchTerm == stringArray[i - 1])
            {
                matches[i - 1] = true;
            }
        }
        for (int i = 1; i <= stringArray.Length; i++)
        {
            if (matches[i - 1])
            {
                Console.WriteLine("We found another " + stringArray[i - 1]);
            }
        }
    }
}
公开课考试 { 公共静态void Main() { string[]stringArray={“Nathan”、“Bob”、“Tom”}; bool[]matches=新bool[stringArray.Length]; 字符串searchTerm=“Bob”; 对于(inti=1;i
使用系统;
公开课考试
{
公共静态void Main()
{
string[]stringArray={“Nathan”、“Bob”、“Tom”};
bool[]matches=新bool[stringArray.Length];
字符串searchTerm=“Bob”;

对于(inti=1;i,我强烈建议使用lambda进行这种处理。 行数少,容易理解

例如:

using System;
using System.Linq;

namespace ConsoleApplication5
{
    public class Test
    {
            public static void Main()
            {
                string[] stringArray = { "Nathan", "Bob", "Tom" };
                bool exists = stringArray.Any(x => x.Equals("Nathan"));
                Console.WriteLine(exists);
            }
     }
}
上面的方法调用Any()为您进行搜索。有许多选项可供探索。您可以将Any()替换为First()、Last()等。如果我是您,我肯定会在System.Linq库中探索集合上的此类操作


(请记住,数组也是一个集合)

我强烈建议使用lambda进行这种处理。 行数少,容易理解

例如:

using System;
using System.Linq;

namespace ConsoleApplication5
{
    public class Test
    {
            public static void Main()
            {
                string[] stringArray = { "Nathan", "Bob", "Tom" };
                bool exists = stringArray.Any(x => x.Equals("Nathan"));
                Console.WriteLine(exists);
            }
     }
}
上面的方法调用Any()为您进行搜索。有许多选项可供探索。您可以将Any()替换为First()、Last()等。如果我是您,我肯定会在System.Linq库中探索集合上的此类操作


(请记住,数组也是一个集合)

Ooh,谢谢!我是用命令行和记事本做的,所以调试是不可能的。但当我使用VS时,我一定要这么做。@user2856410:只是补充一下,最好在所有表达式中使用基于零的索引。如果你这样做的话,就不会发生这种情况。Ooh,谢谢!我是用命令行做的,没有tepad,所以调试实际上是不可能的。不过,当我使用VS时,我一定会这样做。@user2856410:只是补充一下,最好在所有表达式中使用基于零的索引。如果您这样做,就不会发生这种情况。