如何获得的数组值指数? [英] How to get index of value in array?

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

问题描述

我有一个这样的数组:

String[] array = { "Designation1", "Designation2", "Designation3" };

如果用户把Designation2作为输入,那么code应该返回 1

If user put "Designation2" as input then the code should return 1.

这可能是很简单的问题,但我在Java中很新。所以,请给一些建议。

It may be very simple question, but I am very new in Java. So please give some suggestions.

推荐答案

您可以循环中的字符串数组中,并找到该指数的字符串匹配你在找什么。

You can loop over the Strings in the array and find the index for which the String matches what you are looking for.

int index = -1;
for (int i=0; i<array.length;i++) {
    if (array[i].equals(value)) {
        index = i;
        break;
    }
}

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

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