首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ArrayLists之间移动项目?Java

如何在ArrayLists之间移动项目?Java
EN

Stack Overflow用户
提问于 2015-01-19 13:40:02
回答 3查看 862关注 0票数 1

我正在制作一个游戏,一个rpg (是的,又是另一个试图制作其中之一的家伙),我已经成功地制作了其中的大部分,除了库存和它与世界其他地方的行为,现在我想以一种逻辑的方式在列表之间移动项目,例如,我杀死了一个怪物,怪物会在ArrayList worldInventory中创建一个番茄,番茄()将在x,y,如果玩家经过番茄,番茄应该被移动到ArrayList库存,如果玩家想装备番茄,番茄应该被移动到ArrayList装备。

因此,为了使事情更容易理解,我有3份清单:

代码语言:javascript
复制
 ArrayList<Item> worldInventory
 ArrayList<Item> Inventory
 ArrayList<Item> equipped.

我希望在它们之间移动Tomato()的副本,如果一个Tomato()的值在列表中被更改,并且它被移动了,我希望该Tomato()保留它的值。

我会给帮助我的人一个大尺寸的网络巧克力饼干,非常感谢。谢谢n.n .

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-01-19 13:43:53

关于物品下降:

代码语言:javascript
复制
Tomato tomato = new Tomato();
worldInventory.add(tomato);

在收件上:

代码语言:javascript
复制
worldInventory.remove(tomato);
inventory.add(tomato);

关于装备:

代码语言:javascript
复制
inventory.remove(tomato);
equipped.add(tomato);

只是想知道你为什么需要装备好的ArrayList?

您可以在您的WorldObject ( boolean equipped = false)中设置一个布尔标志,并将其设置为true。

票数 1
EN

Stack Overflow用户

发布于 2015-01-19 13:43:51

如果将对象添加到列表中,则该对象将保留其所有字段值。只是:

代码语言:javascript
复制
Tomato yourTomatoObject = otherList.remove(index);
inventory.add(yourTomatoObject);
票数 0
EN

Stack Overflow用户

发布于 2015-01-19 13:44:22

你可以这样做:

代码语言:javascript
复制
public static void main(String[] args) {
    ArrayList<String> test = new ArrayList<String>();
    ArrayList<String> test1 = new ArrayList<String>();
    test.add("test");
    test1.add(test.get(0));
    test.remove(0);
    System.out.println("" + test.size());
    System.out.println("" + test1.get(0));
}

您可以将它添加到一个数组列表中,然后从另一个数组中删除它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28025974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档