PIbd-23_Panina_A.D.Cruiser..../SetGeneric.java

43 lines
1.1 KiB
Java
Raw Normal View History

2023-12-23 16:20:46 +04:00
import java.util.ArrayList;
import java.util.List;
2023-12-09 17:25:15 +04:00
public class SetGeneric<T extends Object> {
2023-12-23 16:20:46 +04:00
private final List<T> _places;
private final int _maxCount;
public int Count;
2023-12-09 17:25:15 +04:00
public int Count() {
2023-12-23 16:20:46 +04:00
return Count;
2023-12-09 17:25:15 +04:00
}
public SetGeneric(int count)
{
2023-12-23 16:20:46 +04:00
_places = new ArrayList<>();
_maxCount = count;
2023-12-09 17:25:15 +04:00
}
2023-12-29 16:43:56 +04:00
public boolean Insert(T cruiser){
2023-12-23 16:20:46 +04:00
if(_places.size() == _maxCount)
return false;
2023-12-29 16:43:56 +04:00
Insert(cruiser, 0);
2023-12-23 16:20:46 +04:00
return true;
2023-12-09 17:25:15 +04:00
}
2023-12-23 16:20:46 +04:00
public boolean Insert(T monorail, int position){
if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount))
return false;
_places.add(position, monorail);
Count++;
return true;
2023-12-09 17:25:15 +04:00
}
public boolean Remove(int position)
{
2023-12-23 16:20:46 +04:00
if(!(position >= 0 && position < _places.size()))
2023-12-09 17:25:15 +04:00
{
2023-12-23 16:20:46 +04:00
return false;
2023-12-09 17:25:15 +04:00
}
2023-12-23 16:20:46 +04:00
_places.remove(position);
Count--;
return true;
2023-12-09 17:25:15 +04:00
}
public T Get(int position)
{
2023-12-23 16:20:46 +04:00
if (position < Count() && position >= 0) { return (T)_places.get(position); }
2023-12-09 17:25:15 +04:00
return null;
}
2023-12-29 16:43:56 +04:00
}