instantiate objects

1

There is some way to instantiate an object n number of times, that is, the instance is

Objeto nombre_var = new Objeto();

that is, if you want to instantiate an undetermined number of objects within a for cycle

for(i=0;i<5;i++){
 Objeto x+'i' = new Objeto();

Something as it is done with the indices of the vectors. Thank you very much

    
asked by Nestor Bautista 14.11.2018 в 15:08
source

2 answers

2

You can use a list:

List<Objeto> objetos = new ArrayList<>();
for(i = 0; i<5; i++){
    objetos.add(new Objeto());
}

With which accessing the fourth object would be

objetos.get(3);

If you know the exact number of instances, you could directly use an array:

Objeto [] objetos= new Objeto[5];
    for(int i = 0; i<5; i++){
    objetos[0] = new Objeto();
}
    
answered by 14.11.2018 / 15:14
source
1

If what you want to create is a static array you should put something like this:

Objeto []nombre_var = new Objeto[Capacidad_Deseada];    
for(int i =0; i< 200; i++)
    {
        Objeto aux = new Objeto();
        hola[i] = aux;

    }

If you want to use dynamic memory, you can use arraylist or linked lists.

    
answered by 14.11.2018 в 15:24