如何从给定的值查找Java中的字符串数组索引?

时间:2022-03-24 21:26:50

I wanted to know if there's a native method in array for Java to get the index of the table for a given value ?

我想知道Java数组中是否有一个本机方法来获取给定值的表的索引?

Let's say my table contains these strings :

假设我的表包含这些字符串:

public static final String[] TYPES = {
        "Sedan",
        "Compact",
        "Roadster",
        "Minivan",
        "SUV",
        "Convertible",
        "Cargo",
        "Others"
    };

Let's say the user has to enter the type of car and that then in the background the program takes that string and get's it's position in the array.

假设用户需要输入car的类型然后在后台程序接受这个字符串并获取它在数组中的位置。

So if the person enters : Sedan It should take the position 0 and store's it in the object of Cars created by my program ...

因此,如果这个人进入:轿厢,它应该在0位置,并将它存储在我的程序创建的汽车对象中……

12 个解决方案

#1


24  

String carName = // insert code here
int index = -1;
for (int i=0;i<TYPES.length;i++) {
    if (TYPES[i].equals(carName)) {
        index = i;
        break;
    }
}

After this index is the array index of your car, or -1 if it doesn't exist.

在这个索引之后是你的车的数组索引,或者-1如果它不存在的话。

#2


118  

Arrays.asList(TYPES).indexOf("Sedan")

#3


7  

for (int i = 0; i < Types.length; i++) {
    if(TYPES[i].equals(userString)){
        return i;
    }
}
return -1;//not found

You can do this too:

你也可以这样做:

return Arrays.asList(Types).indexOf(userSTring);

#4


7  

I had an array of all English words. My array has unique items. But using…

我有一大堆英语单词。我的数组有唯一的项目。但使用……

Arrays.asList(TYPES).indexOf(myString);

…always gave me indexOutOfBoundException.

indexOutOfBoundException…总是给我。

So, I tried:

所以,我试着:

Arrays.asList(TYPES).lastIndexOf(myString);

And, it worked. If your arrays don't have same item twice, you can use:

它工作。如果您的数组没有相同的项目两次,您可以使用:

Arrays.asList(TYPES).lastIndexOf(myString);

#5


6  

Use Arrays class to do this

使用数组类来实现这一点

Arrays.sort(TYPES);
int index = Arrays.binarySearch(TYPES, "Sedan");

#6


6  

try this instead

试试这个相反

org.apache.commons.lang.ArrayUtils.indexOf(array, value);

#7


2  

There is no native indexof method in java arrays.You will need to write your own method for this.

java数组中没有本地索引方法。您需要为此编写自己的方法。

#8


2  

No built-in method. But you can implement one easily:

没有内置的方法。但是你可以很容易地实现一个:

public static int getIndexOf(String[] strings, String item) {
    for (int i = 0; i < strings.length; i++) {
        if (item.equals(strings[i])) return i;
    }
    return -1;
}

#9


1  

Try this Function :

试试这个功能:

public int indexOfArray(String input){
     for(int i=0;i<TYPES,length();i++)
       {
         if(TYPES[i].equals(input))
         {
          return i ;
         }
        }
      return -1     // if the text not found the function return -1
      }

#10


0  

An easy way would be to iterate over the items in the array in a loop.

一种简单的方法是在循环中遍历数组中的项。

for (var i = 0; i < arrayLength; i++) {
 // (string) Compare the given string with myArray[i]
 // if it matches store/save i and exit the loop.
}

There would definitely be better ways but for small number of items this should be blazing fast. Btw this is javascript but same method should work in almost every programming language.

肯定会有更好的方法,但对于少量的项目来说,这应该很快。顺便说一句,这是javascript,但同样的方法应该适用于几乎所有的编程语言。

#11


0  

Testable mockable interafce

可测试的虚伪interafce

public interface IArrayUtility<T> {

    int find(T[] list, T item);

}

implementation

实现

public class ArrayUtility<T> implements IArrayUtility<T> {

    @Override
    public int find(T[] array, T search) {
        if(array == null || array.length == 0 || search == null) {
            return -1;
        }

        int position = 0;

        for(T item : array) {

            if(item.equals(search)) {
                return position;
            } else {
                ++position;
            }
        }

        return -1;
    }

}

Test

测试

@Test
public void testArrayUtilityFindForExistentItemReturnsPosition() {
    // Arrange
    String search = "bus";
    String[] array = {"car", search, "motorbike"};

    // Act
    int position = arrayUtility.find(array, search);

    // Assert
    Assert.assertEquals(position, 1);
}

#12


0  

Use this as a method with x being any number initially. The string y being passed in by console and v is the array to search!

用这个作为x的一种方法。通过控制台和v传递的字符串y是要搜索的数组!

public static int getIndex(int x, String y, String[]v){
    for(int m = 0; m < v.length; m++){
        if (v[m].equalsIgnoreCase(y)){
            x = m;
        }
    }
    return x;
}

#1


24  

String carName = // insert code here
int index = -1;
for (int i=0;i<TYPES.length;i++) {
    if (TYPES[i].equals(carName)) {
        index = i;
        break;
    }
}

After this index is the array index of your car, or -1 if it doesn't exist.

在这个索引之后是你的车的数组索引,或者-1如果它不存在的话。

#2


118  

Arrays.asList(TYPES).indexOf("Sedan")

#3


7  

for (int i = 0; i < Types.length; i++) {
    if(TYPES[i].equals(userString)){
        return i;
    }
}
return -1;//not found

You can do this too:

你也可以这样做:

return Arrays.asList(Types).indexOf(userSTring);

#4


7  

I had an array of all English words. My array has unique items. But using…

我有一大堆英语单词。我的数组有唯一的项目。但使用……

Arrays.asList(TYPES).indexOf(myString);

…always gave me indexOutOfBoundException.

indexOutOfBoundException…总是给我。

So, I tried:

所以,我试着:

Arrays.asList(TYPES).lastIndexOf(myString);

And, it worked. If your arrays don't have same item twice, you can use:

它工作。如果您的数组没有相同的项目两次,您可以使用:

Arrays.asList(TYPES).lastIndexOf(myString);

#5


6  

Use Arrays class to do this

使用数组类来实现这一点

Arrays.sort(TYPES);
int index = Arrays.binarySearch(TYPES, "Sedan");

#6


6  

try this instead

试试这个相反

org.apache.commons.lang.ArrayUtils.indexOf(array, value);

#7


2  

There is no native indexof method in java arrays.You will need to write your own method for this.

java数组中没有本地索引方法。您需要为此编写自己的方法。

#8


2  

No built-in method. But you can implement one easily:

没有内置的方法。但是你可以很容易地实现一个:

public static int getIndexOf(String[] strings, String item) {
    for (int i = 0; i < strings.length; i++) {
        if (item.equals(strings[i])) return i;
    }
    return -1;
}

#9


1  

Try this Function :

试试这个功能:

public int indexOfArray(String input){
     for(int i=0;i<TYPES,length();i++)
       {
         if(TYPES[i].equals(input))
         {
          return i ;
         }
        }
      return -1     // if the text not found the function return -1
      }

#10


0  

An easy way would be to iterate over the items in the array in a loop.

一种简单的方法是在循环中遍历数组中的项。

for (var i = 0; i < arrayLength; i++) {
 // (string) Compare the given string with myArray[i]
 // if it matches store/save i and exit the loop.
}

There would definitely be better ways but for small number of items this should be blazing fast. Btw this is javascript but same method should work in almost every programming language.

肯定会有更好的方法,但对于少量的项目来说,这应该很快。顺便说一句,这是javascript,但同样的方法应该适用于几乎所有的编程语言。

#11


0  

Testable mockable interafce

可测试的虚伪interafce

public interface IArrayUtility<T> {

    int find(T[] list, T item);

}

implementation

实现

public class ArrayUtility<T> implements IArrayUtility<T> {

    @Override
    public int find(T[] array, T search) {
        if(array == null || array.length == 0 || search == null) {
            return -1;
        }

        int position = 0;

        for(T item : array) {

            if(item.equals(search)) {
                return position;
            } else {
                ++position;
            }
        }

        return -1;
    }

}

Test

测试

@Test
public void testArrayUtilityFindForExistentItemReturnsPosition() {
    // Arrange
    String search = "bus";
    String[] array = {"car", search, "motorbike"};

    // Act
    int position = arrayUtility.find(array, search);

    // Assert
    Assert.assertEquals(position, 1);
}

#12


0  

Use this as a method with x being any number initially. The string y being passed in by console and v is the array to search!

用这个作为x的一种方法。通过控制台和v传递的字符串y是要搜索的数组!

public static int getIndex(int x, String y, String[]v){
    for(int m = 0; m < v.length; m++){
        if (v[m].equalsIgnoreCase(y)){
            x = m;
        }
    }
    return x;
}