简体   繁体   English

你如何找到数组中元素的索引?

[英]How do you find the index of an element inside of an array?

import java.util.Scanner;
public class Lab6a
{
    public static void main (String args[])
    {
        int a[] = {34, 29, 16, 3};
        for (int i=0; i>=0; i--)
        {
            System.out.println("a[i] = " + a[1]);
        }
    }
}

This will print out 29. I need a way to search my array for 29 and return it's index and then store the index as an integer这将打印出 29。我需要一种方法来在我的数组中搜索 29 并返回它的索引,然后将索引存储为整数

You can find index of particular value like this.!您可以像这样找到特定值的索引。!

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

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

and also change for loop in your code并在代码中更改for循环

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

or或者

 for (int i = array.length -1 ; i>=0; i--)
import java.util.Scanner;
    public class Lab6a
    {
        public static void main (String args[])
        {
            int i;
            int a[] = {34, 29, 16, 3};
            for (i=0; i<=3; i++)
            {
                System.out.println("a[i] = " + i);
            }
        }
    }

You can also write for(i=0;i<a.length;i++) at the needed postion.This will iterate from 0 to length - 1(ie 4) .你也可以在需要的位置写for(i=0;i<a.length;i++) 。这将从0 to length - 1(ie 4)迭代0 to length - 1(ie 4)

Notes: length() is a method on java.lang.String and works only on arrays.However if you are using collections like arraylists you can also use size() which is a method specified in java.util.Collection .Actually size works well to iterate through objects and is flexible unlike length ,which is used mostly with constants.注意:length() 是java.lang.String上的一个方法,仅适用于数组。但是,如果您使用像数组列表这样的集合,您也可以使用size() ,这是java.util.Collection指定的方法。实际上size有效可以很好地遍历对象,并且不像length那样灵活,它主要与常量一起使用。

You can try something like this if you want to keep your array.如果你想保留你的数组,你可以尝试这样的事情。

int a[] = {34, 29, 16, 3};
int i=0;
while (i<a.length)
{
    if(a[i] == 29) {
        System.out.println("index of 29 is " + i);
        break;
    }
    i++;
}
import java.util.Scanner;
public class Lab6a
{
    public static void main (String args[])
    {
        int a[] = {34, 29, 16, 3}; // the array
        for (int i = 0; i < a.length; i++) // check for each position in array
        {
            System.out.println(a[i] + " = " + a.indexOf(a[i])); // 34 = 0, 29 = 1 etc.
        }
    }
}

As far as I understand, you have several code mistakes.据我了解,您有几个代码错误。 Try this code (put it in your main function).试试这个代码(把它放在你的主函数中)。 Most likely, it should work just fine.很可能,它应该可以正常工作。

int a[] = {34, 29, 16, 3};
int index = -1;
for (int i = 0; i < a.length; i++)
{
    if (a[i] ==  29)
    {
        index = i;
        System.out.print("a[" + i + "] = " + a[i]);
    }
}
if (index == -1)
    System.out.print("Such array element not found.")

The index variable will store the index value of the element you have found. index变量将存储您找到的元素的索引值。 If it was not found, -1 will be stored there.如果没有找到, -1将存储在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM