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

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