Java 如何找到匹配特定值的int数组的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18179701/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:03:56  来源:igfitidea点击:

How to find index of int array which match specific value

javaarrayslistloops

提问by HybrisHelp

I have

我有

int myArray[]= {12,23,10,22,10}; 

So i want to get index of 23from myArraywith out iterating any loop (for,while...) .

所以我想index of 23从不 myArray迭代任何循环 ( for, while...) 。

I would do something like Arrays.asList(myArray).indexOf(23)

我会做类似的事情 Arrays.asList(myArray).indexOf(23)

This is not work for me . I get -1as output .

这对我不起作用。我得到-1作为输出。

This is work with String[]Like

这是与String[]Like 一起工作

  String myArray[]= {"12","23","10","22","10"}; 
  Arrays.asList(myArray).indexOf("23")

So why this is not working with int[]? ?

那么为什么这不起作用int[]??

采纳答案by Ruchira Gayan Ranaweera

Integer myArray[]= {12,23,10,22,10};
System.out.println(Arrays.asList(myArray).indexOf(23)); 

will solve the problem

将解决问题

Arrays.asList(myArray).indexOf(23)this search about objects so we have to use object type of intsince intis primitive type.

Arrays.asList(myArray).indexOf(23)这个关于对象的搜索所以我们必须使用对象类型,int因为它int是原始类型。

String myArray[]= {"12","23","10","22","10"}; 
Arrays.asList(myArray).indexOf("23");

In second case this will work because Stringis object.

在第二种情况下,这将起作用,因为它String是对象。

When we define a List,We define it as List<String>or List<Integer>. so primitives are not use in List. Then Arrays.asList(myArray).indexOf("23")find index of equivalent Object.

当我们定义 a 时List,我们将其定义为List<String>or List<Integer>。所以原语在List. 然后Arrays.asList(myArray).indexOf("23")找到等效对象的索引。

回答by Jlewis071

I concur with Ruchira, and also want to point out that the problem has to do with the fact that intis a primitive while Stringand Integerare actual objects. (note I would have posted this as a comment but can't until 50 reputation ;) )

我同意 Ruchira 的观点,也想指出这个问题与int原始 whileStringInteger实际对象有关。(请注意,我会将其作为评论发布,但直到 50 声望才能发布;))

回答by Andromeda

If you want to convert an array of primitives to a list of boxed primitives, take advantage of Apache Commons. Once you have the list, as shown below, use the API in List to find object by index.

如果要将原语数组转换为装箱原语列表,请利用Apache Commons。获得列表后,如下所示,使用 List 中的 API 按索引查找对象。

List<Integer> list = Arrays.asList(ArrayUtils.toObject(myArray));

回答by SK9

You can consider keeping your array sorted and use binary subdivision to decide an index. Insert and lookup would both be O(log(n)) in this case (instead of O(1) and O(n) respectively).

您可以考虑保持数组排序并使用二进制细分来确定索引。在这种情况下,插入和查找都是 O(log(n))(而不是分别为 O(1) 和 O(n))。

If you're doing a lot of looking up you might want to use a different data structure such as a hash map, not just for performance but also because it can be easier to write around.

如果您要进行大量查找,您可能希望使用不同的数据结构,例如哈希映射,这不仅是为了性能,还因为它可以更容易地编写。