简体   繁体   English

Java-如何获取数组中给定元素的索引

[英]Java - How to get the index of a given element in an array

I have an array of contacts like so : 我有很多这样的联系人:

public class Application {

private Scanner input;
private Contact[] contacts;
private int ArrayNum;

public Application() {
    input = new Scanner(System.in);
    contacts = new Contact[5];
    ArrayNum = 0;

}

And what I want to do is enter a name of someone on the contacts list and if they are found on their list return their index like so: 我想做的是在联系人列表中输入某人的名字,如果在他们的列表中找到他们,则返回他们的索引,如下所示:

System.out.println("Who do you want to remove?");

            String name = input.nextLine();

            for(Contact c: contacts){
                if(c.getName().equals(name)){

                    //Get the index here
                }
            }

I tried researching this but no answer or guide seems to be very clear on this so I'm hoping that someone can explain this for me. 我试图对此进行研究,但似乎没有明确的答案或指南,因此我希望有人可以为我解释一下。

Thank you for looking 谢谢你找

for(int index = 0; index < contacts.length; index++) {
    if(contacts[index].getName().equals(name)) {
        // use the index here
    }
}

I don't think this code snippet needs any further explanation. 我认为此代码段不需要任何进一步的解释。

Use a for loop that uses a counter instead. 使用一个使用计数器的for循环。

for(int i = 0; i < contacts.length, i++) {
    if(contacts[i].getName().equals(name)) {
        // do something with the index, i
    }
}

You could use a counter 您可以使用柜台

System.out.println("Who do you want to remove?"); System.out.println(“您要删除谁?”);

        String name = input.nextLine();
        int remove_index = -1;
        for(int i=0; i<contacts.length; i++){
            Contact c = contacts[i];
            if(c.getName().equals(name)){
                remove_index =i;
            }
        }

Another alternative that may be helpful down the line is to use an ArrayList<Contact> instead of a c-style array ( Contact[] ). 可能对您有所帮助的另一种替代方法是使用ArrayList<Contact>而不是c样式的数组( Contact[] )。 This class might add more handling complexity than it's worth, but it includes a lot of useful extra methods, including an int indexOf(<T> object) where <T> is the type you specified in your declaration (ie, Contact for ArrayList<Contact> contacts ). 此类可能会增加不必要的处理复杂性,但是它包含许多有用的额外方法,包括int indexOf(<T> object) ,其中<T>是您在声明中指定的类型(即Contact for ArrayList<Contact> contacts )。

Bad idea! 馊主意! You can't do that seriously, if you have an array. 如果您有一个数组,则不能认真地做。 Of course you can set an index to null, but then you have to search for a null entry to reference a new contact within the array. 当然,您可以将索引设置为null,但是随后您必须搜索一个null条目以引用数组中的新联系人。 Note: you have an Iterator while writing 注意:编写时有一个Iterator

for(Contact c: contacts){...

So one option is to iterate by index, but it's a bad option. 因此,一种选择是按索引进行迭代,但这是一个糟糕的选择。 Better make your Array a Set. 更好地将您的数组设为一组。 Then you might write: 然后,您可能会写:

for (Iterator<Contact> iter = mySet.iterator();iter.hasNext();) {
    final Contact next = iter.next();
    if(next.getName() == "NAME") {
         iter.remove();
         break;
    }

Always use Iterator.remove()! 始终使用Iterator.remove()! Otherwise you will get Exceptions earlier than you wish :D 否则,您将比您希望的更早获得异常:D

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

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