add
Analysis public static void addNumbers(int n) {
ArrayList<Integer> nums = new ArrayList<>();
for (int i = 0; i < n; i++) {
nums.add(i); // Best case Theta(1), worst case Theta(n)
}
}
Select array assignments as the basic operation.
We want an amortized analysis... Average cost of the operation over a sequence of operations.
This table shows the total and average cost after
| assignment cost | resize cost | total | average |
1 | 1 | 0 | 1 | 1 |
2 | 1 | 1 | 3 | 1.5 |
3 | 1 | 2 | 6 | 2 |
4 | 1 | 0 | 7 | 1.75 |
5 | 1 | 4 | 12 | 2.4 |
6 | 1 | 0 | 13 | 2.167 |
7 | 1 | 0 | 14 | 2 |
8 | 1 | 0 | 15 | 1.875 |
9 | 1 | 8 | 24 | 2.667 |
... | ... | ... | ... | ... |
Total assignments equal to:
You are performing a code review on the following Java method. This is part of the software that manages an automobile's safety features. loggingList
is an ArrayList
that stores log messages that will eventually be saved to persistent storage. Which of the following is the most appropriate evaluation of this code?
public static void respondToCollision(Area area) {
loggingList.add("Airbag Depolyed!");
if (area == Area.FRONT)
deployAirbag();
}
A) LGTM
B) Spelling error in log message.
C) area == Area.FRONT
should be area.equals(Area.FRONT)
D) Efficiency issues could lead to loss of life.
E) Problem with code style: Missing braces after if
.