11package IntensiveCourse .Lesson1 ;
22
33import java .util .Collection ;
4+ import java .util .Comparator ;
45import java .util .Iterator ;
56import java .util .List ;
67import java .util .ListIterator ;
1112 * @author DRMPN
1213 *
1314 */
14- public class MyArrayList <E >{
15+ public class MyArrayList <E > implements List < E > {
1516
1617 private int size = 0 ;
1718 private Object [] elementData ;
@@ -56,7 +57,7 @@ public boolean add(E element) {
5657 /**
5758 * Inserts the given element to the given index of this list.
5859 *
59- * @param index - index of the element to add
60+ * @param index - index of the element to add
6061 * @param element - element to be appended to this list
6162 */
6263 public void add (int index , E element ) {
@@ -92,11 +93,13 @@ public E get(int index) {
9293 }
9394
9495 /**
95- * Returns the index of the first occurrence of the specified element in this list,
96+ * Returns the index of the first occurrence of the specified element in this
97+ * list,
9698 * or -1 if this list does not contain the element.
9799 *
98100 * @param o - element to search for
99- * @return the index of the first occurrence of the specified element in this list,
101+ * @return the index of the first occurrence of the specified element in this
102+ * list,
100103 * or -1 if this list does not contain the element
101104 */
102105 public int indexOf (Object o ) {
@@ -138,7 +141,8 @@ public E remove(int index) {
138141 }
139142
140143 /**
141- * Removes the first occurrence of the specified element from this list, if it is present.
144+ * Removes the first occurrence of the specified element from this list, if it
145+ * is present.
142146 * If this list does not contain the element, it is unchanged.
143147 * Returns true if this list contained the specified element.
144148 *
@@ -155,9 +159,10 @@ public boolean remove(Object o) {
155159 }
156160
157161 /**
158- * Replaces the element at the specified position in this list with the specified element.
162+ * Replaces the element at the specified position in this list with the
163+ * specified element.
159164 *
160- * @param index - index of the element to replace
165+ * @param index - index of the element to replace
161166 * @param element - element to be stored at the specified position
162167 * @return the element previously at the specified position
163168 */
@@ -179,6 +184,22 @@ public int size() {
179184 return size ;
180185 }
181186
187+ /**
188+ * Sorts this list.
189+ */
190+ public void sort () {
191+ Sort .quicksort (elementData , 0 , size - 1 );
192+ }
193+
194+ /**
195+ * Sorts his list using comparator.
196+ *
197+ * @param c - comparator
198+ */
199+ public void sort (Comparator <? super E > c ) {
200+ Sort .quicksort (elementData , 0 , size - 1 , c );
201+ }
202+
182203 private boolean ensureCapacity (int currentSize ) {
183204 return currentSize + 1 == elementData .length ;
184205 }
0 commit comments