-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayOrderedList.java
More file actions
executable file
·53 lines (41 loc) · 1.35 KB
/
Copy pathArrayOrderedList.java
File metadata and controls
executable file
·53 lines (41 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.example.arrayList;
import com.example.interfaces.OrderedListADT;
public class ArrayOrderedList<T extends Comparable<? super T>> extends AbstractArrayList<T> implements OrderedListADT<T> {
public ArrayOrderedList() {
this(DEFAULT_CAPACITY);
}
public ArrayOrderedList(int initialCapacity) {
this.count = 0;
this.front = 0;
this.rear = 0;
this.list = createNewArray(initialCapacity);
}
private T[] createNewArray(int size) {
return (T[]) new Comparable[size];
}
protected void expandCapacity() {
T[] aux = createNewArray(this.list.length * 2);
System.arraycopy(this.list, 0, aux, 0, this.count);
this.list = aux;
}
/**
* {@inheritDoc }
*/
@Override
public void add(T element) {
if (element == null)
throw new NullPointerException("The element to must not be null");
if (this.rear == this.list.length)
this.expandCapacity();
// !!! Comparison order in the While, avoid specific case
int indexToAdd = 0;
while (indexToAdd < this.count && this.list[indexToAdd].compareTo(element) < 0) {
indexToAdd++;
}
shiftRight(indexToAdd);
this.list[indexToAdd] = element;
this.rear++;
this.count++;
this.modCount++;
}
}