Замена массива на список

This commit is contained in:
AnnZhimol 2022-11-12 18:33:16 +04:00
parent 2321881dd1
commit b9677dd6e4

View File

@ -1,25 +1,26 @@
import java.util.ArrayList;
import java.util.Random;
public class CreaterGeneric<T extends EntityWarship, U extends IDrawingObjectBlock> {
Object[] Warships;
Object[] Blocks;
ArrayList<T> Warships;
ArrayList<U> Blocks;
int WarshipsCount=0;
int BlocksCount=0;
public CreaterGeneric(int warshipsCount,int blocksCount){
Warships=new Object[warshipsCount];
Blocks=new Object[blocksCount];
Warships=new ArrayList<>(warshipsCount);
Blocks=new ArrayList<>(blocksCount);
}
public int AddWarship(T warship){
if(WarshipsCount<Warships.length){
Warships[WarshipsCount]=warship;
if(WarshipsCount<=Warships.size()){
Warships.add(warship);
WarshipsCount++;
return WarshipsCount-1;
}
return -1;
}
public int AddBlock(U block){
if(BlocksCount<Blocks.length){
Blocks[BlocksCount]=block;
if(BlocksCount<=Blocks.size()){
Blocks.add(block);
BlocksCount++;
return BlocksCount-1;
}
@ -28,8 +29,8 @@ public class CreaterGeneric<T extends EntityWarship, U extends IDrawingObjectBlo
public DrawingWarship NewWarshipCreating()
{
Random rand=new Random();
T warship = (T)Warships[rand.nextInt(WarshipsCount)];
U block = (U)Blocks[rand.nextInt(BlocksCount)];
T warship = Warships.get(rand.nextInt(WarshipsCount));
U block = Blocks.get(rand.nextInt(BlocksCount));
if(warship instanceof EntityAdvancedWarship){
return new DrawingAdvancedWarship(warship,block);
}