简体   繁体   English

给定一个字符串数组,如何找出特定字符串的索引?

[英]Given an array of Strings, how to find out the index of a particular String?

I have, 我有,

String[] directions = {"North", "South", "West", "East"};

Not, I have a String s which is any one of those 4 String literals. 不,我有一个String,它是这4个String文字中的任何一个。

How do I get the index of s in directions? 如何获得s的方向索引?

A possible enum 可能的枚举

public enum Direction {
    NORTH("North"), SOUTH("South"), EAST("East"), WEST("West");

    private String text;
    private Direction(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}

public class TestDirection {
    public static void main(String[] args) {
        System.out.printf("%10s: %-4s%n", "Direction", "Ordinal");
        for (Direction dir : Direction.values()) {
            System.out.printf("%10s: %-4d%n", dir.getText(), dir.ordinal());
        }
    }
}

Which prints out: 打印出:

 Direction: Ordinal
     North: 0   
     South: 1   
      East: 2   
      West: 3  

but again, the devil is in the details. 但同样,细节决定成败。 As the enum API for this method states: 如该方法的枚举API所述:

Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero). 返回此枚举常量的序数(其在枚举声明中的位置,其中初始常量的序数为零)。 Most programmers will have no use for this method. 大多数程序员都不会使用这种方法。 It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap. 它设计用于复杂的基于枚举的数据结构,例如EnumSet和EnumMap。

You can achieve this using a for loop as well.. 您也可以使用for循环来实现。

int i, index;
for i = 0; i < directions.lenght()-1; i++{
    if(s.equals(directions[i]){
        index = i;
        break;
    }
}

Try this simple snippet: 试试这个简单的代码片段:

String[] directions = {"North", "South", "West", "East"};

int directionIndex = Arrays.asList(directions).indexOf("West");

This will give you the index of any string within the array or returns -1 if it is absent. 这将为您提供数组中任何字符串的索引,如果不存在则返回-1

You can for example use 您可以例如使用

java.util.Arrays.asList(directions).indexOf(s)

A more general question with several excellent answers can be found here . 这里可以找到一个更通用的问题,并提供了几个出色的答案。 The above solution is one of the answers listed there. 以上解决方案是此处列出的答案之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在字符串数组中找到给定String的索引? - How to find an index of given String in array of strings? 如何在字符串数组中的特定索引处追加String - How to append String at particular index in an array of Strings 对于给定的字符串数组,如何在字符串的索引处打印该字符串的字符 - How would you print out the character of that string at the index of the string for a given array of Strings 如何将字符串插入字符串数组中的特定索引并在此之后移动字符串? - How to Insert string to a particular index in string array and shifting strings after that? 如何从给定的值中找到 Java 中 STRING 数组的索引? - How to find index of STRING array in Java from a given value? 如何找到数组中给定int的每个索引? - How to find every index of a given int in an array? 给定一个字符串数组,编写一个递归方法,在 O(n) 中搜索给定字符串并返回索引。 LMK 如何修复错误 - Given an array of strings, write a recursive method that searches for a given string in O(n) and returns the index. LMK how to fix error 在给定字符串组合生成的字符串中找出字典中第一个出现的字符串 - Find out string that would come in first in dictionary among strings generated by combination of given strings 在可能发生重叠的字符串数组中查找字符串的索引 - Find index of string in an array of strings where overlap could occur 删除给定字符串中的数组字符串? - remove the array strings in the given string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM