Processing 如何将一个对象从另一个对象添加或删除到数组中
所以在我的程序中,我有一个叫做“生物”的对象,我通过一个数组把它加载进去,一切都很好。但我想为生物添加一种重新引入的方法,从而将其副本添加到阵列中,但问题是阵列保存在另一个对象中。这是我目前的代码:(我删掉了一些不相关的代码) 运行我的代码的主要部分:Processing 如何将一个对象从另一个对象添加或删除到数组中,processing,Processing,所以在我的程序中,我有一个叫做“生物”的对象,我通过一个数组把它加载进去,一切都很好。但我想为生物添加一种重新引入的方法,从而将其副本添加到阵列中,但问题是阵列保存在另一个对象中。这是我目前的代码:(我删掉了一些不相关的代码) 运行我的代码的主要部分: // Setting creature Creature[] Creatures = new Creature[20]; void setup() { size(1280,720,P2D); // Initializing Creat
// Setting creature
Creature[] Creatures = new Creature[20];
void setup() {
size(1280,720,P2D);
// Initializing Creatures
for(int i = 0; i < Creatures.length; i++) {
Creatures[i] = new Creature(true, "");
}
}
void draw()
{
update();
//TODO: Add more
}
void update() {
//Updating the creatures
for(int i = 0; i < Creatures.length; i++) {
Creatures[i].update();
}
}
如果有人愿意在这方面帮助我(我的意思是从生物类中为生物数组添加一个新的生物),我将非常高兴 将生物数组指针传递给生物的成员函数应该有帮助,对吗?不要使用数组,使用数组列表。它更具活力 因此,在生物内部,我们将拥有:
class Creature {
ArrayList<Creature> creatures = new ArrayList<Creature>();
//You can access the above ArrayList and add to it at any point in this class with creature.add().
//The rest of your class below.
}
更新
我想花点时间全面解释最终的实现
您已经在生物
类中创建了阵列列表
,这意味着您不需要草图中的生物[]
阵列。因此,删除它,以及Setup()
中的for循环
当您想要更新草图(不是生物
类)中生物
阵列列表中的生物
对象时,您只需执行以下操作:
for(int i = 0; i < creature.creatures.size; i++) {
creature.creatures.get(i).update();
}
注意:要添加的数量
只是一个占位符
结论:
// Setting Creature Object
Creature creature = new Creature(); //Whatever paramaters you want to add for the constructor.
void setup() {
size(1280,720,P2D);
// Initalizing Creature objects inside list.
for(int i = 0; i < QUANTITY_TO_ADD; i++) {
creatures.creature.add(new Creature(true, ""));
}
}
void draw()
{
update();
//TODO: Add more
}
void update() {
//Updating the creatures
for(int i = 0; i < creature.creatures.size(); i++) {
creature.creatures.get(i).update();
}
}
现在,您的实现是动态的,并且只存储了ArrayList的一个副本,所以您不必担心在类之间保存更新。ArrayList在
生物
中初始化,您可以使用add()
和get()
方法修改或获取任意位置的任何元素,只要您使用生物
类的对象访问它。确认,你想使用你的生物
类将生物
对象添加到你在另一个文件或类中创建的数组中吗?如果是这样的话,你可以让你的阵列成为静态的。是的,这样生物就可以重新生成并克隆它自己。这是你的问题克隆对象的实际代码,或者您的问题只是试图从生物
类中的第一个片段访问生物
数组?我想从生物类中访问生物数组,我想我知道如何从那里克隆它。我更新的答案有帮助吗?我是编程新手,所以我不完全知道你的意思,但我想你的意思是我应该在生物中获得一个函数,并通过它传递生物数组,如果我理解正确的话,我认为这是行不通的,因为我无法编辑它并添加更多内容creatures@A.Moulding你用什么语言?等等,我想我知道你的意思。这实际上会有所帮助,我可以继续加载该函数(比如update();),然后当生物内部的一个变量说要克隆他自己时,他可以返回数组,修改后添加一个新的生物。我正在使用Java处理。我仍在挣扎,我该如何创建一个允许生物传递的函数[](所以是一个生物数组)因为当我尝试时它给了我一个错误:void reproduction(生物[]){}错误是“…variableDeclarated”但是,我必须在主文件中做些什么才能将这些生物保存在主文件中,通过复制上面的代码,我会得到一个错误:意外标记:>@A.com您接受了答案,那么它现在起作用了吗?或者我还有什么办法可以帮您吗?
for(int i = 0; i < creature.creatures.size; i++) {
creature.creatures.get(i).update();
}
// Setting Creature Object
Creature creature = new Creature(); //Whatever paramaters you want to add for the constructor.
void setup() {
size(1280,720,P2D);
// Initalizing Creature objects inside list.
for(int i = 0; i < QUANTITY_TO_ADD; i++) {
creatures.creature.add(new Creature(true, ""));
}
}
void draw()
{
update();
//TODO: Add more
}
void update() {
//Updating the creatures
for(int i = 0; i < creature.creatures.size(); i++) {
creature.creatures.get(i).update();
}
}