Изменил(а) на 'MyParametrClass.java'

This commit is contained in:
Zhelovanov_Dmitrii 2022-12-22 21:57:30 +04:00
parent 1d7e6e80a0
commit 9a93dd2af5

View File

@ -2,34 +2,35 @@ import java.util.ArrayList;
import java.util.Random; import java.util.Random;
public class MyParametrClass<T extends EntityBattleship, U extends IDrawningBlocks> { public class MyParametrClass<T extends EntityBattleship, U extends IDrawningBlocks> {
private final Object[] EntityArray; private final ArrayList<T> EntityArray;
private final Object[] BlocksArray; private final ArrayList<U> BlocksArray;
int entitiesCount = 0; int entitiesCount = 0;
int blocksCount = 0; int blocksCount = 0;
public MyParametrClass(int entitiesCount, int blocksCount) { public MyParametrClass(int entitiesCount, int blocksCount) {
EntityArray = new Object[entitiesCount]; EntityArray = new ArrayList<T>();
BlocksArray = new Object[blocksCount]; BlocksArray = new ArrayList<U>();
} }
public void Insert(T battleship){ public void Insert(T battleship){
if(entitiesCount < EntityArray.length) {
EntityArray[entitiesCount] = battleship; EntityArray.add(battleship);
entitiesCount++; entitiesCount++;
}
} }
public void Insert(U drawningBlocks){ public void Insert(U drawningBlocks){
if(blocksCount < BlocksArray.length) {
BlocksArray[blocksCount] = drawningBlocks; BlocksArray.add(drawningBlocks);
blocksCount++; blocksCount++;
}
} }
public DrawningBattleship GetDrawningBattleship() { public DrawningBattleship GetDrawningBattleship() {
Random random = new Random(); Random random = new Random();
int entityIndex = random.nextInt(EntityArray.length); int entityIndex = random.nextInt(EntityArray.size());
int blockIndex = random.nextInt(BlocksArray.length); int blockIndex = random.nextInt( BlocksArray.size());
EntityBattleship battleship = (T)EntityArray[entityIndex];
IDrawningBlocks blocks = (U)BlocksArray[blockIndex]; EntityBattleship battleship = EntityArray.get(entityIndex);
IDrawningBlocks blocks = BlocksArray.get(blockIndex);
if (battleship instanceof EntityLinkor) { if (battleship instanceof EntityLinkor) {
return new DrawningLinkor(battleship, blocks); return new DrawningLinkor(battleship, blocks);