field java int数组_java-如何在int数组中查找元素的索引?

java-如何在int数组中查找元素的索引?

如何在类型int的Java数组中找到某个值的索引?

我尝试在未排序的数组上使用Arrays.binarySearch,但有时只能给出正确的答案。

16个解决方案

118 votes

Integer[] array = {1,2,3,4,5,6};

Arrays.asList(array).indexOf(4);

请注意,此解决方案是线程安全的,因为它创建了类型为List的新对象。

另外,您也不想循环或类似地调用它,因为每次都会创建一个新对象

Pablo Fernandez answered 2019-09-26T20:33:14Z

24 votes

如果您使用的是番石榴集合,则另一个选择是Ints.indexOf

// Perfect storm:

final int needle = 42;

final int[] haystack = [1, 2, 3, 42];

// Spoiler alert: index == 3

final int index = Ints.indexOf(haystack, needle);

当空间,时间和代码重用非常宝贵时,这是一个很好的选择。 这也很简洁。

btiernay answered 2019-09-26T20:33:45Z

14 votes

看一下API,它说您必须先对数组进行排序

所以:

Arrays.sort(array);

Arrays.binarySearch(array, value);

如果您不想对数组进行排序:

public int find(double[] array, double value) {

for(int i=0; i

if(array[i] == value)

return i;

}

Jamie Curtis answered 2019-09-26T20:34:17Z

11 votes

将此方法复制到您的班级中

public int getArrayIndex(int[] arr,int value) {

int k=0;

for(int i=0;i

if(arr[i]==value){

k=i;

break;

}

}

return k;

}

通过传递两个参数Array和value调用此方法,并将其返回值存储在整数变量中。

int indexNum = getArrayIndex(array,value);

谢谢

Dalvinder Singh answered 2019-09-26T20:34:49Z

3 votes

您可以将其转换为列表,然后使用indexOf方法:

Array.asList(array).indexOf(1);

[http://download.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#asList(T...)][http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#indexOf(java.lang.Object)]

agmcleod answered 2019-09-26T20:35:20Z

3 votes

使用二进制搜索之前,您需要对值进行排序。 否则,手动方法是尝试选项卡中的所有整数。

public int getIndexOf( int toSearch, int[] tab )

{

for( int i=0; i< tab.length ; i ++ )

if( tab[ i ] == toSearch)

return i;

return -1;

}//met

一种替代方法是将所有索引映射到映射中的每个值。

tab[ index ] = value;

if( map.get( value) == null || map.get( value) > index )

map.put( value, index );

然后map.get(value)获取索引。

问候,斯特凡

@pst,感谢您的评论。 您可以发布其他替代方法吗?

Snicolas answered 2019-09-26T20:36:10Z

2 votes

您可以使用现代Java解决此问题。 请使用以下代码:

static int findIndexOf(int V, int[] arr) {

return IntStream.range(1, arr.length).filter(i->arr[i]==V).findFirst().getAsInt();

}

Vahid answered 2019-09-26T20:36:34Z

1 votes

简单:

public int getArrayIndex(int[] arr,int value) {

for(int i=0;i

if(arr[i]==value) return i;

return -1;

}

Gil SH answered 2019-09-26T20:36:53Z

1 votes

Integer[] arr = { 0, 1, 1, 2, 3, 5, 8, 13, 21 };

List arrlst = Arrays.asList(arr);

System.out.println(arrlst.lastIndexOf(1));

alok answered 2019-09-26T20:37:10Z

0 votes

您可以遍历数组直到找到所需的索引,或者改用asList()。 请注意,您可以使用asList()将阵列转换为列表。

rid answered 2019-09-26T20:37:35Z

0 votes

/**

* Method to get the index of the given item from the list

* @param stringArray

* @param name

* @return index of the item if item exists else return -1

*/

public static int getIndexOfItemInArray(String[] stringArray, String name) {

if (stringArray != null && stringArray.length > 0) {

ArrayList list = new ArrayList(Arrays.asList(stringArray));

int index = list.indexOf(name);

list.clear();

return index;

}

return -1;

}

Manmohan Soni answered 2019-09-26T20:37:52Z

0 votes

你可以这样做:

public class Test {

public static int Tab[] = {33,44,55,66,7,88,44,11,23,45,32,12,95};

public static int search = 23;

public static void main(String[] args) {

long stop = 0;

long time = 0;

long start = 0;

start = System.nanoTime();

int index = getIndexOf(search,Tab);

stop = System.nanoTime();

time = stop - start;

System.out.println("equal to took in nano seconds ="+time);

System.out.println("Index of searched value is: "+index);

System.out.println("De value of Tab with searched index is: "+Tab[index]);

System.out.println("==========================================================");

start = System.nanoTime();

int Bindex = bitSearch(search,Tab);

stop = System.nanoTime();

time = stop - start;

System.out.println("Binary search took nano seconds ="+time);

System.out.println("Index of searched value is: "+Bindex);

System.out.println("De value of Tab with searched index is: "+Tab[Bindex]);

}

public static int getIndexOf( int toSearch, int[] tab ){

int i = 0;

while(!(tab[i] == toSearch) )

{ i++; }

return i; // or return tab[i];

}

public static int bitSearch(int toSearch, int[] tab){

int i = 0;

for(;(toSearch^tab[i])!=0;i++){

}

return i;

}

}

添加了XOR :)

Andre answered 2019-09-26T20:38:20Z

0 votes

在使用for循环的主要方法中:-在我的示例中,第三个for循环就是该问题的答案。-在我的示例中,我制作了一个由20个随机整数组成的数组,为变量分配了最小的数字,并在计算循环数的同时当数组的位置达到最小值时停止了循环。

import java.util.Random;

public class scratch {

public static void main(String[] args){

Random rnd = new Random();

int randomIntegers[] = new int[20];

double smallest = randomIntegers[0];

int location = 0;

for(int i = 0; i < randomIntegers.length; i++){ // fills array with random integers

randomIntegers[i] = rnd.nextInt(99) + 1;

System.out.println(" --" + i + "-- " + randomIntegers[i]);

}

for (int i = 0; i < randomIntegers.length; i++){ // get the location of smallest number in the array

if(randomIntegers[i] < smallest){

smallest = randomIntegers[i];

}

}

for (int i = 0; i < randomIntegers.length; i++){

if(randomIntegers[i] == smallest){ //break the loop when array location value ==

break;

}

location ++;

}

System.out.println("location: " + location + "\nsmallest: " + smallest);

}

}

代码输出所有数字及其位置,以及最小数字后跟最小数字的位置。

extremeoats answered 2019-09-26T20:38:51Z

0 votes

如果有人还在寻找答案,

您可以使用[Apache Commons Library] [1]中的ArrayUtils.indexOf()。

如果您使用的是Java 8,则还可以使用Stream API:

public static int indexOf(int[] array, int valueToFind) {

if (array == null) {

return -1;

}

return IntStream.range(0, array.length)

.filter(i -> valueToFind == array[i])

.findFirst()

.orElse(-1);

}

[1]:[https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/ArrayUtils.html#indexOf(int [],%20int)]

Deep Shah answered 2019-09-26T20:39:42Z

0 votes

static int[] getIndex(int[] data, int number) {

int[] positions = new int[data.length];

if (data.length > 0) {

int counter = 0;

for(int i =0; i < data.length; i++) {

if(data[i] == number){

positions[counter] = i;

counter++;

}

}

}

return positions;

}

Rahul9191 answered 2019-09-26T20:40:00Z

-2 votes

Integer[] array = {1, 2, 3, 4, 5, 6};

for (int i = 0; i < array.length; i++) {

if (array[i] == 4) {

system.out.println(i);

break;

}

}

vani answered 2019-09-26T20:40:18Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值