如何获取数组元素的索引 [英] How to get the index of an array element

查看:62
本文介绍了如何获取数组元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取数组元素的索引?有没有方法?



我尝试过:



什么都没有。

How can I can get the index of an array element? Is there a method available?

What I have tried:

Nothing.

推荐答案

有一个 Array.IndexOf [ ^ ]方法,但它可能不会返回您想要的方法:它将返回第一个匹配的条目。因此,如果您有两个值为9的元素,它将始终返回索引最低的元素。
There is the Array.IndexOf[^] method, but it may not return the one you want: it will return the first matching entry. So if you have two elements with the value "9", it will always return the one with the lowest index.


Hi Mehdi,



只需在循环中搜索阵列中的元素。



这是一个例子:



Hi Mehdi,

Simply you make a loop for searching for that element in your array.

This is an example:

int[] Array = { 5, 8, 3, 7 };
int Element = 3;
int Index;

for (int i = 0; i < Array.Length; i++)
{
     if (Element == Array[i])
     {
         Index = i;
     }
}







我希望这有帮助,

:)




I hope this help,
:)


希望数组索引 [ ^ ]将指导您。


这篇关于如何获取数组元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆