[英]Efficient way to find a certain object in multiple ArrayLists in Java?
Is there a way to find a specific object in a group of ArrayLists such that I first want to find that specific object, then figure out which ArrayList it is in? 有没有一种方法可以在一组ArrayList中找到特定的对象,这样我首先要查找该特定对象,然后找出它在哪个ArrayList中? If there are multiple ways to do this, what is the most efficient way to do this? 如果有多种方法可以做到这一点,最有效的方法是什么?
EDIT: For some context 编辑:对于某些上下文
public class Player {
private int lifePoints;
private ArrayList<Card> hand;
private ArrayList<Card> deck;
private ArrayList<Card> graveyard;
private Monster[] monsterZones;
private Card[] spellTrapZones;
private Context context;
public Player(Context c) {
lifePoints = 8000;
monsterZones = new Monster[5];
spellTrapZones = new Card[5];
hand = new ArrayList<Card>();
deck = new ArrayList<Card>();
graveyard = new ArrayList<Card>();
context = c;
initialize();
}
public void initialize() {
deck = FileHandler.loadFromFile("test.deck", context);
}
public void draw() {
hand.add(deck.remove(0));
}
public void shuffle() {
Collections.shuffle(deck);
}
public void sendToGraveyard(Monster monster) {
graveyard.add(monster);
//I want to add code that would use the Monster parameter to find that specific Monster and remove it from the ArrayList it is in
}
} }
I will make my example as if the Object's name is Car. 我将以该对象的名称为Car作为示例。
Make a Tuple by using your preferred library or create a Object containing a tuple Car, List (your array). 使用您喜欢的库制作一个元组,或者创建一个包含元组Car,List(您的数组)的Object。 Name it CarTuple. 将其命名为CarTuple。
Try a HashMap like HashMap<String, CarTuple>()
尝试像HashMap<String, CarTuple>()
这样的HashMap<String, CarTuple>()
Where the String is the reference of the object (like an id). 其中String是对象的引用(如id)。 Then the real object and it's array list can be found in O(1). 然后可以在O(1)中找到真实对象及其数组列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.