done дон done
This commit is contained in:
parent
4d5540d8e4
commit
a7b27fc5d0
@ -60,7 +60,7 @@ public class CarCollectionFrame extends JFrame {
|
||||
CarsGenericCollection<DrawingCar, DrawingObjectCar> _cars = _storage.Get(listBoxStoragesJList.getSelectedValue());
|
||||
GameFrame form = new GameFrame();
|
||||
if (form.getSelectedCar() != null) {
|
||||
if (_cars.plus(_cars, form.SelectedCar) != -1) {
|
||||
if (_cars.plus(_cars, form.SelectedCar)) {
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||||
|
||||
} else {
|
||||
|
@ -23,11 +23,11 @@ public class CarsGenericCollection<T extends DrawingCar, U extends IMoveableObje
|
||||
_pictureHeight = picHeight;
|
||||
_collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
public int plus(CarsGenericCollection<T, U> collect, T obj)
|
||||
public boolean plus(CarsGenericCollection<T, U> collect, T obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
return collect._collection.Insert(obj);
|
||||
}
|
||||
|
@ -1,56 +1,52 @@
|
||||
package Generics;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SetGeneric<T extends Object> {
|
||||
private Object[] _places;
|
||||
private final List<T> _places;
|
||||
private final int _maxCount;
|
||||
public int Count;
|
||||
|
||||
public int Count() {
|
||||
return _places.length;
|
||||
return Count;
|
||||
}
|
||||
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_places = new Object[count];
|
||||
_places = new ArrayList<>();
|
||||
_maxCount = count;
|
||||
}
|
||||
|
||||
public int Insert(T car)
|
||||
{
|
||||
return Insert(car,0);
|
||||
public boolean Insert(T car){
|
||||
if(_places.size() == _maxCount)
|
||||
return false;
|
||||
Insert(car, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
public int Insert(T car, int position)
|
||||
{
|
||||
if (!(position >= 0 && position < Count()))
|
||||
return -1;
|
||||
if (_places[position] != null)
|
||||
{
|
||||
int indexEnd = position + 1;
|
||||
while (_places[indexEnd] != null)
|
||||
{
|
||||
indexEnd++;
|
||||
}
|
||||
for (int i = indexEnd + 1; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
|
||||
}
|
||||
_places[position] = car;
|
||||
return position;
|
||||
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;
|
||||
}
|
||||
|
||||
public boolean Remove(int position)
|
||||
{
|
||||
if (position < Count() && position >= 0)
|
||||
if(!(position >= 0 && position < _places.size()))
|
||||
{
|
||||
_places[position] = null;
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
_places.remove(position);
|
||||
Count--;
|
||||
return true;
|
||||
}
|
||||
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position < Count() && position >= 0) { return (T)_places[position]; }
|
||||
if (position < Count() && position >= 0) { return (T)_places.get(position); }
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user