使用 Java 索引数组中的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31736220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Index of an element in an array using Java
提问by Somdutta
The following code in Java return -1. I thought that it should return 3.
Java 中的以下代码返回 -1。我认为它应该返回 3。
int[] array = {1,2,3,4,5,6};
System.out.println(Arrays.asList(array).indexOf(4));
Can you help me understand how this function works.
你能帮我理解这个功能是如何工作的吗?
Thanks
谢谢
回答by Paul Boddington
Before Java 5, Arrays.asListused to accept an Object[]. When generics and varargs were introduced into the language this was changed to
在 Java 5 之前,Arrays.asList用于接受Object[]. 当泛型和可变参数被引入语言时,这被更改为
public static <T> List<T> asList(T... arr)
In your example, Tcannot be intbecause intis a primitive type. Unfortunately, the signature matches with Tequal to int[]instead, which is a reference type. The result is that you end up with a Listcontaining an array, not a Listof integers.
在您的示例中,T不能是int因为int是原始类型。不幸的是,签名与T等于int[]相反,这是一个引用类型。结果是你最终得到一个List包含一个数组,而不是一个List整数。
In Java 4, your code would not have compiled because an int[]is not an Object[]. Not compiling is preferable to producing a strange result, and in Effective Java, Josh Bloch says retrofitting asListto be a varargs method was a mistake.
在 Java 4 中,您的代码不会被编译,因为 anint[]不是Object[]. 不编译比产生奇怪的结果更可取,在 Effective Java 中,Josh Bloch 说改型asList为可变参数方法是一个错误。
回答by Jean-Fran?ois Savard
Arrays.asListexpect an object (or more). Your array (array) is a sole object, thus calling the method will create a list of only one object, which is your array.
Arrays.asList期待一个对象(或更多)。您的数组 ( array) 是一个唯一对象,因此调用该方法将创建一个只有一个对象的列表,这就是您的数组。
Calling indexOfon your list will return -1 because 4will never be found as your list contains the array object, not the list of datas that your array contains.
调用indexOf您的列表将返回 -1,因为4永远不会被找到,因为您的列表包含数组对象,而不是数组包含的数据列表。
回答by glglgl
int[] array = {1,2,3,4,5,6};
Arrays.stream(array).boxed().collect(Collectors.toList()).indexOf(4);
should do what you want while keeping the original int[].
应该做你想做的,同时保持原来的int[]。
You get an IntStreamof it, box it into a Stream<Integer>, collect it to a List<Integer>and then can do an .indexOf()search.
你得到一个IntStream,把它装进一个Stream<Integer>,把它收集到一个List<Integer>,然后可以进行.indexOf()搜索。
回答by Shar1er80
You've got answers as as to what is happening to your array when you use Arrays.asList().
当您使用Arrays.asList().
Other things to consider:
其他需要考虑的事项:
- Since your array is sorted, consider
Arrays.binarySearch() - If your array is not sorted, a simple custom indexOf method can perform the same without wasting cycles converting the
int[]to aList<Integer>
- 由于您的数组已排序,请考虑
Arrays.binarySearch() - 如果阵列未排序,一个简单的自定义的indexOf方法可以在不浪费周期转换执行相同
int[]的List<Integer>
Example:
例子:
public static void main(String[] args) throws Exception {
int[] array = {1, 2, 3, 4, 5, 6};
System.out.print("Binary Search: ");
System.out.println(Arrays.binarySearch(array, 4));
int[] array2 = {5, 8, 2, 5, 3, 4, 1};
System.out.print("Manual Search: ");
System.out.println(indexOf(array2, 4));
}
public static int indexOf(int[] array, int search) {
for (int i = 0; i < array.length; i++) {
if (array[i] == search) {
return i;
}
}
return -1;
}
Results:
结果:
Binary Search: 3
Manual Search: 5
回答by Oghli
You cannot use primitive types such as int as parameters to generic classes in Java since int is not a class,you shoud use Integer instead :
您不能使用原始类型(例如 int)作为 Java 中泛型类的参数,因为 int 不是类,您应该使用 Integer 代替:
Integer[] array = {1,2,3,3,4,5};
List Arraylist=Arrays.asList(array);
System.out.println(Arraylist.indexOf(1));
System.out.println(Arraylist.indexOf(4));
output:
输出:
0
4
note that Arraylist.indexOf(i)return the index of first occurrence of the element iin list :
请注意,Arraylist.indexOf(i)返回元素i在列表中第一次出现的索引:
System.out.println(Arraylist.indexOf(3));
it will return 2which is first occurrence of element 3 , not 3since there is 2 elements of 3 in list.
它将返回2第一个出现的元素 3 ,而不是3因为列表中有 2 个元素 3 。
回答by Ralph
Since you are using an array of primitive type, this will not work. The array is of type int. Were you using Integertype for example, that will work.
-1 means the index of the specified element was not found.
由于您使用的是原始类型的数组,因此这将不起作用。该数组的类型为 int。Integer例如,您是否使用类型,这将起作用。-1 表示未找到指定元素的索引。
This is what you want to do:
这是你想要做的:
Integer[] array = {1,2,3,4,5,6};
System.out.println(Arrays.asList(array).indexOf(4));
回答by Sean
Because when you do this with a primary data type array, it will be treated as the first element in the List.
因为当您使用主要数据类型数组执行此操作时,它将被视为 List 中的第一个元素。
int[] array = {1,2,3,4,5,6};
System.out.println(Arrays.asList(array));
You will get this:
你会得到这个:
[[I@474e8d67]
回答by CoderCroc
int[] array = {1,2,3,4,5,6};
- In java
arrayis considered as anObject int[]is particularlyObjectand notObject[]- To call
Arrays.asListyou need zero or moreObjects
- 在java中
array被认为是Object int[]是特别Object而不是Object[]- 打电话给
Arrays.asList你需要零个或多个Objects
Your method gets called in this way,
你的方法以这种方式被调用,
Arrays.asList((Object)(array));//Yes
Arrays.asList(1,2,3,4,5,6);//No
So you will have List<int[]>and not List<Integer>
所以你会有List<int[]>和没有List<Integer>
Now, you might have asked your self that if
Arrays.asList(1,2,3,4)works than why notArrays.asList(arrayInt)?
现在,您可能会问自己,如果
Arrays.asList(1,2,3,4)有效,为什么不有效Arrays.asList(arrayInt)?
In Arrays.asList(1,2,3,4)all the values are autoboxedin to the Integerwhich is solely an Objectso in above case Integer[]cad do the trick.
在Arrays.asList(1,2,3,4)所有的值都被自动装箱到,Integer这只是Object在上面的情况下Integer[]cad 做的伎俩。

