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

问题描述 投票:0回答:1

我有点陷入了java项目。我有这个问题,我不知道如何解决。

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

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

我创建了一个 ArrayList 矩阵作为我的世界,我想向其中添加对象和代理。它们都继承自 Entity 类。我的问题是,当我将它们添加到矩阵中时,我不知道如何以不同的方式命名它们。这是我的世界级:

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;
}

基本上当我这样做时: Entity nome = new 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;
    }
java arraylist
1个回答
0
投票

我认为您代码中的问题是您在使用 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
         }
     }
    
© www.soinside.com 2019 - 2024. All rights reserved.