java - 从另一个类方法调用对象数组的特定成员

标签 java arrays list methods

我现在正在研究项目的人工智能部分。我正在调用 AI 类中的一个方法,该方法旨在计算我绘制的角斗士对象实际需要到达的位置。我向该方法传递了一个列表,其中包含我想要放置的所有对象。 AI 类之前的方法已经确定了他们希望彼此之间的距离,我将其存储为gladiator[0..1..2..etc].movementGoal。

虽然该项目不是实时的,即我最终只想“单步执行”它,但我确实希望同时发生移动。这意味着我迭代列表的标准方法将不起作用,因为我需要有关其他角斗士的移动决策的信息,以便在这些决策相互作用时找出任何一个角斗士的实际移动。

当我在类之外并且仅以列表形式访问另一个特定角斗士的变量时,如何访问它们?

编辑:

我想我可以迭代并测试变量gladiatorNumber是否正确,然后什么时候提取该信息?这可能有点绕,但这是我能想到的全部。

编辑2:

根据要求,一些代码。我在 Ai 类中的方法如下所示:

public void moveAI(List<Gladiator> gladiators) {

我的角斗士是这样定义的:

public class Gladiator {

Gladiator 类被创建为数组,然后添加到单独主类的列表中。我真的不想包含比这更多的代码,因为代码太多了。基本上,它归结为如何从 AI 类中调用gladiator[0],即使我在主类中创建了所述对象,并​​且在 AI 类中仅以列表形式包含它们。假设 Gladiator 中的所有变量都是公共(public)的。我收到的错误是找不到引用角斗士 [0...1...2...etc] 的符号。

最佳答案

我认为你的问题归结为想要将角斗士数组传递给另一个类。那应该相当容易。如果您的主类中有这两个定义(请注意,您只需要一个,我推荐该列表,因为它更通用,数组具有固定长度)。

你想要这样的东西:

public class Main {
// ....stuff
// This is the main class that keeps the list of gladiators
private List<Gladiator> gladiatorsList;
private Gladiator[] gladiatorsArray;
private MovementAI movementAI;

public Main() {
    // You initialize gladiatorsList and gladiatorsArray as before
    // gladiatorsList = ...
    // gladiatorsArrray = ...
    // Now you want to pass this list/array to another class (the AI), you
    // can do this in the constructor of that class like so:
    movementAI = new MovementAI(gladiatorsList);
}

// ...stuff as before

}

人工智能

public class MovementAI {

private List<Gladiator> gladiators;

// Giving the class the list-reference, this list will be the same as the
// list in main, when main-list changes so does this one, they point to the
// same list-object, so the reference is only needed once.
public MovementAI(List<Gladiator> gladiatorsList) {
    this.gladiators = gladiatorsList;
}

// The class already has a reference to the list from its constructor so it
// doesn't need the list again as a parameter
public void moveAI() {

}

// If you don't want to keep a reference to the list in this class but only
// use it in a method (I would not recommend this)
public MovementAI() {

}

// You need to pass it gladiatorsList everytime you call this method.
public void moveAI(List<Gladiator> gladiators) {

}

}

我在你的上一条评论中看到,你决定让人工智能决定在符合标准的情况下重新绘制,这是不推荐的,你应该在你的类中保持职责分离,减少错误的发生,更好的发展。建议让 AI 更改角斗士列表(移动它们、杀死它们等),并且渲染器类简单地绘制每个角斗士。

你似乎还想让每个角斗士都能够将另一个角斗士作为目标,最好让他们将目标作为对象,这样你就不必搜索整个列表来找出答案角斗士编号指的是哪个角斗士,您不必考虑在列表中排序。像这样的事情:

public class Gladiator {
// ..other stuff

private Gladiator target;
public Gladiator getTarget() {
    return target;
}

public void setTarget(Gladiator target) {
    this.target = target;
}
}

关于java - 从另一个类方法调用对象数组的特定成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16921650/

相关文章:

java - 使用 native SQL 将外部列连接到 Hibernate 实体

Java:将 XML 文档解析为文本时出错

java - Spring - 在 RabbitMQ 监听器中验证传入消息

javascript - JSON 中的对象数组的行为与本地创建的对象数组不同

python - Pandas DataFrame 删除元组或列列表

java - 以线程安全的方式更新ConcurrentHashMap

arrays - 替换 2 个数组的元素?

strcpy 中的 char * 和 char[]

python,包含元组的列表,找到连续的数字

python - 在 for 循环中从列表中解压多个参数