如何查找匹配特定值的int数组的索引[duplicate]

时间:2022-11-28 21:30:23

This question already has an answer here:

这个问题在这里已有答案:

i have

我有

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

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

所以我想从myArray得到23的索引而没有迭代任何循环(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 -1 as 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 []? ?

4 个解决方案

#1


12  

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 int since int is 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 String is 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.

当我们定义List时,我们将其定义为List 或List 。所以在List中不使用原语。然后Arrays.asList(myArray).indexOf(“23”)找到等效Object的索引。

#2


4  

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

我同意Ruchira,并且还想指出问题与int是一个原语而String和Integer是实际对象这一事实有关。 (注意我会发布这个作为评论,但不能直到50声誉;))

#3


2  

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));

#4


1  

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.

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

#1


12  

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 int since int is 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 String is 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.

当我们定义List时,我们将其定义为List 或List 。所以在List中不使用原语。然后Arrays.asList(myArray).indexOf(“23”)找到等效Object的索引。

#2


4  

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

我同意Ruchira,并且还想指出问题与int是一个原语而String和Integer是实际对象这一事实有关。 (注意我会发布这个作为评论,但不能直到50声誉;))

#3


2  

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));

#4


1  

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.

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