有没有办法在一组ArrayLists中找到一个特定的对象,这样我首先要找到那个特定的对象,然后找出它所在的ArrayList?如果有多种方法可以做到这一点,那么最有效的方法是什么?
编辑:对于某些情况
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
}
}
答案 0 :(得分:0)
我将使我的例子好像对象的名字是Car。
使用首选库创建一个元组,或者创建一个包含元组Car,List(您的数组)的Object。将其命名为CarTuple。
尝试像HashMap<String, CarTuple>()
String是对象的引用(如id)。然后可以在O(1)中找到真实对象及其数组列表。