如何在while循环中保存具有不同名称的ArrayList?

弗朗西斯科·阿尔维斯(Francisco Alves)

我有点被困在一个Java项目中。我有这个问题,我不知道如何解决。

基本上,我创建了一个从物体到物体列出它们的机器人。对象不会移动,但是机器人(在我的代码中为代理)会移动。

我使用的一些变量来自FinalProject类,这是我的主要类。

我创建了一个ArrayLists矩阵作为我的世界,我想向它添加对象和代理。他们都从实体类继承。我的问题是,当我将它们添加到矩阵中时,我不知道如何命名它们。这是我的世界课程:

package finalproject;
import java.util.*;

public class World {
protected int cord_x , cord_y , agent_num , object_num , numero_x , numero_y, conta = 1, i = 0;
protected String op, nome;
protected String [] tipos;

public World (int cord_x, int cord_y, int agent_num, int object_num , String op){
    this.cord_x = cord_x;
    this.cord_y = cord_y;
    this.agent_num = agent_num;
    this.object_num = object_num;
    this.op = op;
    ArrayList<Entity>[][] mundo = (ArrayList<Entity>[][])new ArrayList<?>[cord_x][cord_y];
    if (op.equals("s")){
        gera_agent(agent_num , mundo);
        gera_object(object_num, mundo);
    }
}

private ArrayList<Entity>[][] gera_agent(int num , ArrayList<Entity>[][] mundo){
    String [] cores = {"vermelho", "azul", "verde", "preto"};
    String [] formas = {"triangulo", "quadrado", "retangulo", "losango"};
    String [] estrategias = {"random", "hamming", "closest"};
    i=0;
    while (i<num){
        int numeroY = new Random().nextInt(cord_x);
        int numeroX = new Random().nextInt(cord_y);
        int rcores = new Random().nextInt(cores.length);
        int rformas = new Random().nextInt(formas.length);
        int restrategias = new Random().nextInt(estrategias.length);

        String cor = (cores[rcores]);
        String forma = (formas[rformas]);
        String estrategia = (estrategias[restrategias]);
        nome = "Agente" + Integer.toString(conta);
        Entity nome = new Agent(nome, cor, forma, numeroX, numeroY, conta, estrategia, FinalProject.raio);
        mundo[numeroX][numeroY].add(nome);
        conta++;
        i++;
    }
    return mundo;
}

private ArrayList<Entity>[][] gera_object(int num , ArrayList<Entity>[][] mundo){
    String [] cores = {"vermelho", "azul", "verde", "preto"};
    String [] formas = {"triangulo", "quadrado", "rectangulo", "losango"};

    if (FinalProject.op1.equals("planeta")){
        tipos = new String[] {"rocha", "alien", "caratera"};
    }
    else if (FinalProject.op1.equals("catastrofe")){
        tipos =  new String []{"sobrevivente", "morto", "escombros"};
    }
    else if (FinalProject.op1.equals("domesticos")){
        tipos = new String [] {"mesa", "cadeira", "vassora"};
    }

    i=0;
    while (i<num){
        int numeroX = new Random().nextInt(cord_x);
        int numeroY = new Random().nextInt(cord_y);

        int rcores = new Random().nextInt(cores.length);
        int rformas = new Random().nextInt(formas.length);
        int rtipo;
        rtipo = new Random().nextInt(tipos.length);
        String cor = (cores[rcores]);
        String forma = (formas[rformas]);
        String tipo = (tipos[rtipo]);
        nome = "Object" + Integer.toString(conta);
        Entity nome = new Object(nome, cor, forma, numeroX, numeroY, conta, estrategia, FinalProject.raio);
        mundo[numeroX][numeroY].add(nome);
        conta++;
        i++;

    }
    return mundo;
}

基本上当我这样做时:实体nome =新Agent(nome,cor,forma,numeroX,numeroY,conta,estrategia,FinalProject.raio);mundo [numeroX] [numeroY] .add(nome); -这很愚蠢。我是否需要命名要添加到矩阵中的对象?如果可以,该怎么办?

在我的名字的某个点上,我通过以下方式使用一些输入变量来创建世界:

World mundo = new World(W_x , W_y , num_ag , num_ob , op2);

我的实体类:

package finalproject;
import java.util.ArrayList;

public abstract class  Entity {
    protected String name;
    protected String color;
    protected String shape;
    protected int xCoordenate;
    protected int yCoordenate;
    protected int id;
    public Entity (String name, String color , String shape , int xCoordenate , int yCoordenate , int id){
        this.name = name;
        this.color = color;
        this.shape = shape;
        this.xCoordenate = xCoordenate;
        this.yCoordenate = yCoordenate;
        this.id = id;
    }
尤根德拉·辛格(Yogendra singh)

我认为您的代码中的问题是您没有在使用ArrayList之前对其进行初始化,例如,我在下面的代码段中编写了一个: mundo[i][j] = new ArrayList<Integer>();

     int cord_x = 6, cord_y = 5, num = 10;
     ArrayList<Integer>[][] mundo = (ArrayList<Integer>[][])
                                             new ArrayList<?>[cord_x][cord_y];
     for(int i = 0; i < cord_x; i++){
         for(int j = 0; j < cord_y; j++){

             //***Instantiate the ArrayList---This is required***
             mundo[i][j] = new ArrayList<Integer>(); 
             for(int k = 0; k < num; k++){
                 // Add the elements in the array list
                 mundo[i][j].add(new Integer(i+j+k)); 
             }
         }
     }

     //Check the elements
     for(int i = 0; i < cord_x; i++){
         for(int j = 0; j < cord_y; j++){
             for(int k = 0; k < num; k++){
                 // prints the elements
                 System.out.println("mundo["+i+"]["+j+"]- place "
                      +k+" Element == " +mundo[i][j].get(k)); 
             }
         }
     }

上面的代码片段在第一次迭代中填充了元素,并在第二次迭代中将它们打印为:

mundo[0][0]- place 0 Element == 0
mundo[0][0]- place 1 Element == 1
mundo[0][0]- place 2 Element == 2
mundo[0][0]- place 3 Element == 3
......

我认为您可以在代码中采用两种方法。

  1. mundo[numeroX][numeroY].add(nome);在行之前添加检查以查看初始化,如下所示

    if(mundo[numeroX][numeroY] == null){
        mundo[numeroX][numeroY] = new ArrayList<Entity>();
    }
    
  2. 在while循环之前,您可以通过如下放置初始化片段来将所有数组列表元素初始化在一起:

     for(int i = 0; i < cord_x; i++){
         for(int j = 0; j < cord_y; j++){
             mundo[i][j] = new ArrayList<Entity>(); // <-- Instantiate the ArrayList
         }
     }
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在不同模块中使用两个具有相同名称的AngularJS服务?

来自分类Dev

在循环中创建多个具有不同名称的对象以存储在数组列表中

来自分类Dev

BehaviorSpace NetLogo:保存具有不同名称的矩阵

来自分类Dev

方案:如何创建一个将数据保存到具有不同名称的文件的循环?

来自分类Dev

如何在GDB中将具有相同名称的符号与不同的目标文件区分开?

来自分类Dev

如何在Django的不同目录中调用具有相同名称的模板?

来自分类Dev

如何使用R中的for循环保存具有不同名称的文件?

来自分类Dev

如何在循环中保存多个图?

来自分类Dev

如何在不同目录中填充具有相同名称的可执行文件?

来自分类Dev

如何在表中识别具有不同名称的相同列?

来自分类Dev

如何根据循环中的当前迭代使对象具有不同的名称?

来自分类Dev

如何在R中的for循环中保存图

来自分类Dev

是否有功能在python中保存多个具有不同名称的csv文件?

来自分类Dev

用不同的名称在循环中保存数组

来自分类Dev

在具有不同列名称的for循环中的left_join

来自分类Dev

如何使用for循环在xslt中处理具有相同名称但属性值不同的xml标记

来自分类Dev

BehaviorSpace NetLogo:保存具有不同名称的矩阵

来自分类Dev

如何仅查找具有不同名称的重复文件?

来自分类Dev

如何在forEach循环中保存数据?

来自分类Dev

Javascript:如何根据索引在循环内创建具有不同名称的新变量

来自分类Dev

如何在具有不同ID的while循环中使用hide show

来自分类Dev

如何在循环中保存单元地址

来自分类Dev

如何在Jenkins中使用Execute Shell命令传递可能具有不同名称的文件

来自分类Dev

如何导入具有固定路径的不同名称的文件

来自分类Dev

如何在 JSP 中使用具有不同名称的 for 循环创建 5 个文本框?

来自分类Dev

如何在 ASP.NET MVC 中保存多个具有相同名称属性的数据?

来自分类Dev

Python-将输入变量存储在每次迭代具有不同名称的循环中

来自分类Dev

使具有相同名称的新 ArrayList 表现不同

来自分类Dev

如何在不使用数组的情况下在 for 循环中声明和初始化多个变量(具有不同名称)?

Related 相关文章

  1. 1

    如何在不同模块中使用两个具有相同名称的AngularJS服务?

  2. 2

    在循环中创建多个具有不同名称的对象以存储在数组列表中

  3. 3

    BehaviorSpace NetLogo:保存具有不同名称的矩阵

  4. 4

    方案:如何创建一个将数据保存到具有不同名称的文件的循环?

  5. 5

    如何在GDB中将具有相同名称的符号与不同的目标文件区分开?

  6. 6

    如何在Django的不同目录中调用具有相同名称的模板?

  7. 7

    如何使用R中的for循环保存具有不同名称的文件?

  8. 8

    如何在循环中保存多个图?

  9. 9

    如何在不同目录中填充具有相同名称的可执行文件?

  10. 10

    如何在表中识别具有不同名称的相同列?

  11. 11

    如何根据循环中的当前迭代使对象具有不同的名称?

  12. 12

    如何在R中的for循环中保存图

  13. 13

    是否有功能在python中保存多个具有不同名称的csv文件?

  14. 14

    用不同的名称在循环中保存数组

  15. 15

    在具有不同列名称的for循环中的left_join

  16. 16

    如何使用for循环在xslt中处理具有相同名称但属性值不同的xml标记

  17. 17

    BehaviorSpace NetLogo:保存具有不同名称的矩阵

  18. 18

    如何仅查找具有不同名称的重复文件?

  19. 19

    如何在forEach循环中保存数据?

  20. 20

    Javascript:如何根据索引在循环内创建具有不同名称的新变量

  21. 21

    如何在具有不同ID的while循环中使用hide show

  22. 22

    如何在循环中保存单元地址

  23. 23

    如何在Jenkins中使用Execute Shell命令传递可能具有不同名称的文件

  24. 24

    如何导入具有固定路径的不同名称的文件

  25. 25

    如何在 JSP 中使用具有不同名称的 for 循环创建 5 个文本框?

  26. 26

    如何在 ASP.NET MVC 中保存多个具有相同名称属性的数据?

  27. 27

    Python-将输入变量存储在每次迭代具有不同名称的循环中

  28. 28

    使具有相同名称的新 ArrayList 表现不同

  29. 29

    如何在不使用数组的情况下在 for 循环中声明和初始化多个变量(具有不同名称)?

热门标签

归档