Java Arraylist - 如何获取特定元素/名称?

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

Arraylist - how to get a specific element/name?

javaarraylist

提问by Adem ?kmen

I've tried to search the WWW but failed to find an answer. Couldn't find one here either.

我试图搜索 WWW 但未能找到答案。这里也找不到。

Here's my question: How do I get a specific name(element?) from a Customer in an ArrayList? I'm imagining it looks something like this:

这是我的问题:如何从 ArrayList 中的 Customer 获取特定名称(元素?)?我想象它看起来像这样:

ArrayList<Customer> list = new ArrayList();

String name = list.get(2) // which would return the Customer at 2's place. 

But what if I want to search for a customer by name, lets say a customer named Alex? How do I do that?

但是,如果我想按姓名搜索客户怎么办,比如说一个名为 Alex 的客户?我怎么做?

Bonus question: How do I then delete that customer?

奖金问题:然后我如何删除该客户?

采纳答案by bhspencer

As others have said this isn't all that efficient and a HashMap will give you fast lookup. But if you must iterate over the list you would do it like this:

正如其他人所说,这并不是那么有效,HashMap 将为您提供快速查找。但是如果你必须遍历列表,你会这样做:

    String targetName = "Jane";
    Customer result = null;
    for (Customer c : list) {
        if (targetName.equals(c.getName())) {
            result = c;
            break;
        }
    }

If you need to remove an item from a list while iterating over it you need to use an iterator.

如果您需要在迭代时从列表中删除项目,则需要使用迭代器。

    String targetName = "Jane";
    List<Customer> list = new ArrayList<Customer>();
    Iterator<Customer> iter = list.iterator();
    while (iter.hasNext()) {
        Customer c = iter.next();
        if (targetName.equals(c.getName())) {
            iter.remove();
            break;
        }
    }

回答by Thierry

With an ArrayList, you have to loop... If you can, use a Map (HashMap, TreeMap) to quickly find an element. This works if you always seek by name, for example. (use name as key of the map)

有了ArrayList,就得循环……如果可以的话,用Map(HashMap,TreeMap)来快速找到一个元素。例如,如果您总是按名称搜索,则此方法有效。(使用名称作为地图的键)

回答by childofsoong

There isn't a way to explicitly do what you want, unless you want to iterate through the whole collection, comparing the desired name to the current one. If you want this type of functionality, you could try a Map such as HashMap.

没有一种方法可以明确地做你想做的事情,除非你想遍历整个集合,将所需的名称与当前的名称进行比较。如果你想要这种类型的功能,你可以尝试像HashMap.

回答by sashwat

Implement equals and hashcode for Customer object. Use customer name attribute for this.

为 Customer 对象实现 equals 和 hashcode。为此使用客户名称属性。

Use ArrayList.indexof to find the index of the element. use the remove method in Arraylist to remove the object by index.

使用 ArrayList.indexof 查找元素的索引。使用 Arraylist 中的 remove 方法按索引删除对象。

回答by miro99

Your are going to have to iterate through your array using something like this in a function call.

您将不得不在函数调用中使用类似的方法遍历数组。

void int HasName(string name){
    for(int i=0; i < list.size(); i++) {
        String s = list.get(i).getName();
        //search the string
        if(name.equals(s)) {
            return i
        }
    }
    return -1
}

If you really need to search by name consider looking into HashMap.

如果您确实需要按名称搜索,请考虑查看 HashMap。