I have an array like this :
我有一个像这样的数组:
String[] array = { "Designation1", "Designation2", "Designation3" };
If user put "Designation2" as input then the code should return 1
.
如果用户输入“Designation2”作为输入,则代码应返回1。
It may be very simple question, but I am very new in Java. So please give some suggestions.
这可能是一个非常简单的问题,但我是Java的新手。所以请提出一些建议。
7 个解决方案
#1
3
You can loop over the Strings in the array and find the index for which the String matches what you are looking for.
您可以循环遍历数组中的字符串,并找到String与您要查找的字符串匹配的索引。
int index = -1;
for (int i=0; i<array.length;i++) {
if (array[i].equals(value)) {
index = i;
break;
}
}
#2
4
Consider using List instead of array (of just wrap your array in List). This way you will have access to method like indexOf(element)
which will return index of first founded element, of -1 if no element in array was found.
考虑使用List而不是数组(只是在List中包装你的数组)。通过这种方式,您可以访问indexOf(element)这样的方法,该方法将返回第一个已创建元素的索引,如果找不到数组中的元素,则返回-1。
String[] array = { "Designation1", "Designation2", "Designation3" };
List<String> list = Arrays.asList(array);
System.out.println(list.indexOf("Designation2")); //prints 1
System.out.println(list.indexOf("foo")); //prints -1
#3
0
You could just use a for loop
你可以使用for循环
String[] array = { "Designation1", "Designation2", "Designation3" };
Scanner kb=new Scanner(System.in);
String input=kb.next();
int index;
for(int i=0;i<array.length;i++)
{
if(array[i].equalsIgnoreCase(input))
index=i;
}
#4
0
You can do it like this.
你可以这样做。
String userinput="Designation2";
String[] array = { "Designation1", "Designation2", "Designation3" };
int length=array.length();
int index=0;
for(int i=0;i<length;i++)
{
if(array[i].equals(userinput))
{
index=i;
break;
}
}
And index will give you the array key that user wants. Regards..
而索引将为您提供用户想要的数组键。问候..
#5
0
There are no direct search method for array so you need to convert it to list first
数组没有直接搜索方法,因此您需要先将其转换为列表
String[] array = { "Designation1", "Designation2", "Designation3" };
assert Arrays.asList(array).indexOf("Designation2") == 1;
assert Arrays.asList(array).indexOf("Anything else") == -1;
Do not forget that -1 mean 'not found'
别忘了-1意味着'找不到'
Or you can sort it and use binarySearch
或者您可以对其进行排序并使用binarySearch
#6
0
You can use ArrayUtils from Apache Commons.
您可以使用Apache Commons中的ArrayUtils。
String[] array = { "Designation1", "Designation2", "Designation3" };
System.out.println(ArrayUtils.indexOf(array, "Designation1"));//0
System.out.println(ArrayUtils.indexOf(array, "Designation2"));//1
System.out.println(ArrayUtils.indexOf(array, "Designation3"));//2
#7
0
In order to do this task, you can use two arrays, one for key names and ones for their values. Simply search for the key in the first array, get the index of the key name and use it to get the value from the second array. It's not the most efficient, and this is probably not the best answer, but it works for me.
为了完成此任务,您可以使用两个数组,一个用于键名,一个用于值。只需在第一个数组中搜索键,获取键名的索引并使用它从第二个数组中获取值。它不是最有效的,这可能不是最好的答案,但它对我有用。
Just make sure the indexes in the arrays line up, for example:
只需确保数组中的索引排成一行,例如:
{"my","three","keys"};
{"My","Three","Values"};
In this case, the key/value setup would be;
在这种情况下,键/值设置将是;
my/My three/Three keys/Values
我/我的三/三键/值
In your case, you don't need to use the value array, just use the index.
在您的情况下,您不需要使用值数组,只需使用索引。
Also try using ArrayList instead of arrays, as you can use ArrayList.indexOf(key) to get the index of key in the ArrayList.
也可以尝试使用ArrayList而不是数组,因为您可以使用ArrayList.indexOf(key)来获取ArrayList中键的索引。
Hope this helps you and others with this problem. ☺
希望这可以帮助您和其他人解决这个问题。 ☺
#1
3
You can loop over the Strings in the array and find the index for which the String matches what you are looking for.
您可以循环遍历数组中的字符串,并找到String与您要查找的字符串匹配的索引。
int index = -1;
for (int i=0; i<array.length;i++) {
if (array[i].equals(value)) {
index = i;
break;
}
}
#2
4
Consider using List instead of array (of just wrap your array in List). This way you will have access to method like indexOf(element)
which will return index of first founded element, of -1 if no element in array was found.
考虑使用List而不是数组(只是在List中包装你的数组)。通过这种方式,您可以访问indexOf(element)这样的方法,该方法将返回第一个已创建元素的索引,如果找不到数组中的元素,则返回-1。
String[] array = { "Designation1", "Designation2", "Designation3" };
List<String> list = Arrays.asList(array);
System.out.println(list.indexOf("Designation2")); //prints 1
System.out.println(list.indexOf("foo")); //prints -1
#3
0
You could just use a for loop
你可以使用for循环
String[] array = { "Designation1", "Designation2", "Designation3" };
Scanner kb=new Scanner(System.in);
String input=kb.next();
int index;
for(int i=0;i<array.length;i++)
{
if(array[i].equalsIgnoreCase(input))
index=i;
}
#4
0
You can do it like this.
你可以这样做。
String userinput="Designation2";
String[] array = { "Designation1", "Designation2", "Designation3" };
int length=array.length();
int index=0;
for(int i=0;i<length;i++)
{
if(array[i].equals(userinput))
{
index=i;
break;
}
}
And index will give you the array key that user wants. Regards..
而索引将为您提供用户想要的数组键。问候..
#5
0
There are no direct search method for array so you need to convert it to list first
数组没有直接搜索方法,因此您需要先将其转换为列表
String[] array = { "Designation1", "Designation2", "Designation3" };
assert Arrays.asList(array).indexOf("Designation2") == 1;
assert Arrays.asList(array).indexOf("Anything else") == -1;
Do not forget that -1 mean 'not found'
别忘了-1意味着'找不到'
Or you can sort it and use binarySearch
或者您可以对其进行排序并使用binarySearch
#6
0
You can use ArrayUtils from Apache Commons.
您可以使用Apache Commons中的ArrayUtils。
String[] array = { "Designation1", "Designation2", "Designation3" };
System.out.println(ArrayUtils.indexOf(array, "Designation1"));//0
System.out.println(ArrayUtils.indexOf(array, "Designation2"));//1
System.out.println(ArrayUtils.indexOf(array, "Designation3"));//2
#7
0
In order to do this task, you can use two arrays, one for key names and ones for their values. Simply search for the key in the first array, get the index of the key name and use it to get the value from the second array. It's not the most efficient, and this is probably not the best answer, but it works for me.
为了完成此任务,您可以使用两个数组,一个用于键名,一个用于值。只需在第一个数组中搜索键,获取键名的索引并使用它从第二个数组中获取值。它不是最有效的,这可能不是最好的答案,但它对我有用。
Just make sure the indexes in the arrays line up, for example:
只需确保数组中的索引排成一行,例如:
{"my","three","keys"};
{"My","Three","Values"};
In this case, the key/value setup would be;
在这种情况下,键/值设置将是;
my/My three/Three keys/Values
我/我的三/三键/值
In your case, you don't need to use the value array, just use the index.
在您的情况下,您不需要使用值数组,只需使用索引。
Also try using ArrayList instead of arrays, as you can use ArrayList.indexOf(key) to get the index of key in the ArrayList.
也可以尝试使用ArrayList而不是数组,因为您可以使用ArrayList.indexOf(key)来获取ArrayList中键的索引。
Hope this helps you and others with this problem. ☺
希望这可以帮助您和其他人解决这个问题。 ☺