简体   繁体   English

Java ArrayList尝试检查具有此名称的对象是否存在

[英]Java ArrayList trying to check if object with this name exists

I'm having a bit of trouble in my head trying to solve this: I'm working on a "rankList", an arrayList made of "Score". 我在尝试解决这个问题时遇到了麻烦:我正在研究“ rankList”,即由“ Score”制成的arrayList。 Score it's the object that has the following atributes: name,wins,loses,draws. 得分是具有以下属性的对象:名称,胜利,失败,抽奖。 My class Ranking has an ArrayList of Score objects. 我的课程Rank具有一个Score对象的ArrayList。 To create a new Score object I just use the name (and set the rest to 0 since it's new). 要创建一个新的Score对象,我只需使用名称(并将其余的设置为0,因为它是新名称)。 However I'm trying to check if the player's name it's already in rankList I don't have to create new but sum a win or lose or draw. 但是我正在尝试检查玩家的名字是否已经在rankList中,我不必创建新的名字,但总之输赢或平局。 I have been reading arround that I have to override equals then others say I have to override contains... It's getting a big mess in my head. 我一直在读《我的世界》,我必须重写等于,然后其他人说我必须重写“包含...”。 My fastest solution would be to write an "for" that goes arround the arrayList and use the getName().equals("name"); 我最快的解决方案是编写一个“ for”,它绕着arrayList并使用getName().equals("name"); however this is getting too messi in my code. 但是,这在我的代码中变得太乱了。 I have checkPlayer (if the palyer is in the list): 我有checkPlayer(如果在列表中有palyer):

 public boolean checkPlayer(String playerName) {
    for (int i = 0; i < this.rankList.size(); i++) {
        if (this.rankList.get(i).getName().equals(playerName)) {
            return true;

        }
    }
    return false;
}

then if I want to incrase the wins i have this : 然后,如果我想增加胜利,我会得到:

public void incraseWins(String playerName) {
    if (checkPlayer(playerName)) {
        for (int i = 0; i < this.rankList.size(); i++) {
            if (this.rankList.get(i).getName().equals(playerName)) {
                this.rankList.get(i).setWins(this.rankList.get(i).getWins() + 1);
                break;
            }
        }
    } else {
        createPlayer(playerName);
    //more for to get to the player i'm looking for...
        for (int i = 0; i < this.rankList.size(); i++) {
            if (this.rankList.get(i).getName().equals(playerName)) {
                this.rankList.get(i).setWins(this.rankList.get(i).getWins() + 1);
                break;
            }
        }

    }

So i guess there is a better way to do this... :/ 所以我想有一个更好的方法来做到这一点...:/

ArrayList is not the right data structure here. ArrayList在这里不是正确的数据结构。 To check if an element exists in the array you are searching the entire arraylist. 要检查数组中是否存在元素,请搜索整个arraylist。 Which means it's O(N). 这意味着它是O(N)。

To keep an array list is sorted order and do a binary search on it would definitely be faster as suggested in the comments. 要保留数组列表的排序顺序,并按照注释中的建议进行二进制搜索,绝对会更快。 But that wouldn't solve all your problems either because insert into the middle would be slow. 但这并不能解决您的所有问题,因为插入中间位置会很慢。 Please see this Q&A: When to use LinkedList over ArrayList? 请参阅此问答: 何时在ArrayList上使用LinkedList?

One suggestion is to use a Map . 一种建议是使用Map You would then be storing player name, player object pairs. 然后,您将存储播放器名称,播放器对象对。 This would give you very quick look ups. 这将使您快速查找。 Worst case is O(log N) i believe. 我认为最糟糕的情况是O(log N)。

It's also worth mentioning that you would probably need to make a permanent record of these scores eventually. 还值得一提的是,您最终可能需要永久记录这些分数。 If so an indexed RDBMS would give you much better performance and make your code a lot simpler. 如果是这样,建立索引的RDBMS将为您提供更好的性能,并使您的代码更简单。

尝试使用带有键的哈希表,它将更加高效!

e..Why not using map<>. e ..为什么不使用map <>。

a binary search is good idea if you must use List,code like this 如果必须使用List,则二进制搜索是一个好主意,代码如下

    List<Method> a= new ArrayList<>();
    //some method data add...
    int index =  Collections.binarySearch(a, m);
    Method f = a.get(index);

and class method is impl of Comparable,then override compareTo() method 并且类方法是Comparable的隐式,然后重写compareTo()方法

public class Method implements Comparable<Method>{
........
@Override
public int compareTo(Method o) {
    return this.methodName.compareTo(o.getMethodName());
}

if you don't want use binsearch,CollectionUtils in commons can help you 如果您不想使用binsearch,则CommonU的CollectionUtils可以为您提供帮助

        CollectionUtils.find(a, new Predicate() {
        @Override
        public boolean evaluate(Object object) {
            return ((Method)object).getMethodName().equals("aaa");
        }
    });

in fact CollectionUtils.find is also a 'for' 实际上CollectionUtils.find也是“ for”

 for (Iterator iter = collection.iterator(); iter.hasNext();) {
            Object item = iter.next();
            if (predicate.evaluate(item)) {
                return item;
            }
        }

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

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