查找数组类的索引

时间:2014-06-14 11:00:17

标签: c# arrays indexing

我在数组中找到了我的值索引。我想出了如何检查数组中是否存在我的值,但是如何找到索引,我不知道。我通过谷歌搜索,尝试了所有其他功能,没有取得任何进展。因此,如果有人能够解释并给出一些如何实现我想要的结果的例子,那将是非常棒的,感谢您的时间。

public phrases[] sentences = new phrases[1];

    // STRUCT
public struct phrases
{
    public string key;
    public string value;
}

Array.FindIndex(sentences, key => key.value == "kaka");

2 个答案:

答案 0 :(得分:0)

以这种方式工作:

词组[]句子=新词组[4];

句子[3] =新短语(){key =“kaka”,value =“kaka”};

var index = Array.FindIndex(sentences,key => key.value ==“kaka”);

答案 1 :(得分:0)

你可以使用类似这样的方法:

int getIndexMethod(phrases[] array, string valueToFind)
{
    for (int i = 0; i < array.Length; i++)
    {
        if(array[i].value == valueToFind)
        {
           return i;
        }
    }
    return -1;
}

顺便说一下,你的:

Array.FindIndex(sentences, key => key.value == "kaka");

也应该运行良好,只需将其值返回到某个变量,例如:

int index = Array.FindIndex(sentences, key => key.value == "kaka");