-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh1.txt
executable file
·76 lines (55 loc) · 1.52 KB
/
h1.txt
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Q1 Part A
public void add(int pos, E item) {
private boolean isFull;
private int maxPos;
private int lowPos;
//Gets maxPos;
for(int i = ListADT.size(); i>0; i--) {
if(ListADT.get(i)!=null) {
maxPos++;
}
}
//Get lowest position;
for(int i = 0; i<ListADT.size(); i++) {
if(ListADT.get(i) != null) {
lowPos++;
}
}
if (pos>maxPos && pos<0) throw new IndexOutOfBoundsException;
else {
//If array is full, adds a location.
for(int i = 0; i<ListADT.size(); i++) {
if(ListADT.get(i) == null && isFull == true) {
isFull = false;
}
}
if(isFull) {
ListADT.expandArray();
}
}
//Moves all objects to the left of pos.
for(int i = 0; i<(pos); i++) {
ListADT.set((lowPos+i)-1) = ListADT.get(lowPos+i);
}
//Adds new object to the ADT.
ListADT.set(lowPos + pos - 1) = item;
}
Q1 Part B
[X,X,"Sandra","Jack","Franz"]
[X,"Sandra","Sandra","Jack","Franz"]
[X,"Sandra","Jack","Jack","Franz"]
[X,"Sandra","Jack","Terricloth","Franz"]
Q1 Part C
Disadvantage 1: Array index is not equal to the list positioning.
Disadvantage 2: Slower due to more code making up for Disadvantage 1.
Q2
public void loopFromStart(List<String> myList, int startPos){
if(startPos>myList.size() || startPos<0) throw new IndexOutOfBoundsException;
else {
Iterator<String> itr = myList.iterator();
for(int i = 0; i<startPos; i++) {
myList.add(itr.next());
itr.remove();
}
}
}