From 45096484946ecdcd69fc1eed04abee4ef87c20a8 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 21 Oct 2022 16:04:51 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20SetLocomotivesG?= =?UTF-8?q?eneric?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SetLocomotivesGeneric.java | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 SetLocomotivesGeneric.java diff --git a/SetLocomotivesGeneric.java b/SetLocomotivesGeneric.java new file mode 100644 index 0000000..b74c1e7 --- /dev/null +++ b/SetLocomotivesGeneric.java @@ -0,0 +1,57 @@ +public class SetLocomotivesGeneric +{ + private final T[] _places; + + public int Count() { + return _places.length; + } + + public SetLocomotivesGeneric(int count) { + _places = (T[]) new Object[count]; + } + + public boolean Insert (T locomotive) { + return Insert(locomotive, 0); + } + + public boolean Insert (T locomotive, int position) { + if (position >= _places.length || position < 0) return false; + if (_places[position] == null) { + _places[position] = locomotive; + return true; + } + + int emptyEl = -1; + for (int i = position + 1; i < Count(); i++) + { + if (_places[i] == null) emptyEl = i; + break; + } + if (emptyEl == -1) + { + return false; + } + for (int i = emptyEl; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = locomotive; + return true; + } + + public boolean Remove (int position) { + if (position >= _places.length || position < 0) return false; + _places[position] = null; + return true; + } + + public T Get(int position) + { + if (position >= _places.length || position < 0) + { + return null; + } + return _places[position]; + } + +}