Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我需要使用for循环来查找数组列表中具有相同单词的字符串_C#_For Loop_Arraylist - Fatal编程技术网

C# 我需要使用for循环来查找数组列表中具有相同单词的字符串

C# 我需要使用for循环来查找数组列表中具有相同单词的字符串,c#,for-loop,arraylist,C#,For Loop,Arraylist,我需要找到一种方法来使用for循环来查找同一数组列表中有多少个字符串是同一个单词。我需要使用for循环,因为每次它循环并找到另一个相同的单词时,我都想将其添加到int变量中:下面是尝试使用但没有使用的代码;不行。可变卧室一直说它是0,而不是 foreach (string row in RoomType) // RoomType is the ArrayList { if (row.Equals("Bedroom"))

我需要找到一种方法来使用for循环来查找同一数组列表中有多少个字符串是同一个单词。我需要使用for循环,因为每次它循环并找到另一个相同的单词时,我都想将其添加到int变量中:下面是尝试使用但没有使用的代码;不行。可变卧室一直说它是0,而不是

foreach (string row in RoomType) // RoomType is the ArrayList
            {

                if (row.Equals("Bedroom"))
                {
                    bedrooms++;
                }
            }

此代码有效,它基于您的代码。我假设您可能在代码中做了一些事情,
卧室
在循环过程中被重新声明。不过这只是一个猜测

var RoomType = new ArrayList();
var bedrooms = 0;

RoomType.Add("workflow");
RoomType.Add("Bedroom");
RoomType.Add("Bedroom");

foreach (string row in RoomType) // RoomType is the ArrayList
{

    if (row.Equals("Bedroom"))
    {
        bedrooms++;
    }
}

Console.WriteLine(bedrooms);
另一种方式:

var row = RoomType.Cast<string>().ToArray();
var count = row.Aggregate(0, (c, i) => (i.Equals("Bedroom")) ? ++c : c);
var row=RoomType.Cast().ToArray();
var计数=行集合(0,(c,i)=>(i.Equals(“卧室”)?+c:c);

如果不强制使用循环,可以尝试从以下内容开始:

int bedrooms = RoomType.ToArray().Count(row => row.Equals("Bedroom"));

如果您被迫使用循环,那么请多发布一些代码,以便我们可以看到这些对象的设置位置和方式。

我们可以看到更多的代码吗?