diff --git a/src/CarCollectionFrame.java b/src/CarCollectionFrame.java index 5c05416..950437c 100644 --- a/src/CarCollectionFrame.java +++ b/src/CarCollectionFrame.java @@ -60,7 +60,7 @@ public class CarCollectionFrame extends JFrame { CarsGenericCollection _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 { diff --git a/src/Generics/CarsGenericCollection.java b/src/Generics/CarsGenericCollection.java index 8cdced6..37a9ddc 100644 --- a/src/Generics/CarsGenericCollection.java +++ b/src/Generics/CarsGenericCollection.java @@ -23,11 +23,11 @@ public class CarsGenericCollection(width * height); } - public int plus(CarsGenericCollection collect, T obj) + public boolean plus(CarsGenericCollection collect, T obj) { if (obj == null) { - return -1; + return false; } return collect._collection.Insert(obj); } diff --git a/src/Generics/SetGeneric.java b/src/Generics/SetGeneric.java index eea4685..8384d41 100644 --- a/src/Generics/SetGeneric.java +++ b/src/Generics/SetGeneric.java @@ -1,56 +1,52 @@ package Generics; +import java.util.ArrayList; +import java.util.List; + public class SetGeneric { - private Object[] _places; + private final List _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; } }