PIbd -21 Bakalskaya E.D. LabWork4 Hard #7
@ -1,12 +1,21 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class SetGeneric<T extends DrawingLocomotive>{
|
||||
private T[] _places;
|
||||
public int Count(){
|
||||
return _places.length;
|
||||
private ArrayList<T> _places;
|
||||
public int Count()
|
||||
{
|
||||
return _places.size();
|
||||
}
|
||||
public SetGeneric(int count) {
|
||||
_places = (T[]) new DrawingLocomotive[count];
|
||||
|
||||
public int maxCount;
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
maxCount = count;
|
||||
_places = new ArrayList<T>(count);
|
||||
}
|
||||
|
||||
public int Insert(T loco)
|
||||
@ -17,47 +26,65 @@ public class SetGeneric<T extends DrawingLocomotive>{
|
||||
public int Insert(T loco, int position)
|
||||
{
|
||||
int NoEmpty = 0, temp = 0;
|
||||
for (int i = position; i < Count(); i++)
|
||||
if(position < 0 || position > Count())
|
||||
return -1;
|
||||
if(Count() > maxCount)
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
if (_places[i] != null) NoEmpty++;
|
||||
}
|
||||
if (NoEmpty == Count() - position - 1) return -1;
|
||||
|
||||
if (position < Count() && position >= 0)
|
||||
{
|
||||
for (int j = position; j < Count(); j++)
|
||||
{
|
||||
if (_places[j] == null)
|
||||
{
|
||||
temp = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// shift right
|
||||
for (int i = temp; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
_places[position] = loco;
|
||||
_places.add(position, loco);
|
||||
return position;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position >= Count() || position < 0)
|
||||
return null;
|
||||
|
||||
T tmp = _places[position];
|
||||
_places[position] = null;
|
||||
return tmp;
|
||||
else
|
||||
{
|
||||
_places.remove(position);
|
||||
}
|
||||
return _places.get(position);
|
||||
}
|
||||
|
||||
public T Get(int position)
|
||||
{
|
||||
// TODO проверка позиции
|
||||
if (position < 0 || position >= Count()) return null;
|
||||
return _places[position];
|
||||
public Iterable<T> GetLocomotives(final Integer maxLocomotives) {
|
||||
return new Iterable<T>() {
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return new Iterator<T>() {
|
||||
private int currentIndex = 0;
|
||||
private int count = 0;
|
||||
|
||||
//есть ли в коллекции еще место (значения)
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentIndex < _places.size() &&
|
||||
(maxLocomotives == null || count < maxLocomotives);
|
||||
}
|
||||
|
||||
//ну и, соответственно, переходим к следующему, если хэз некст == true
|
||||
@Override
|
||||
public T next() {
|
||||
if (hasNext()) {
|
||||
count++;
|
||||
return _places.get(currentIndex++);
|
||||
}
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// public T Get(int position)
|
||||
// {
|
||||
// // TODO проверка позиции
|
||||
// if (position < 0 || position >= Count()) return null;
|
||||
// return _places[position];
|
||||
// }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user