Skip to content
Snippets Groups Projects
Commit f2485612 authored by Goik Martin's avatar Goik Martin
Browse files

Reallocation by "copyOf"

Renaiming variable to initialCapacity for the sake of better
comprehension
parent 8db8d936
No related branches found
No related tags found
No related merge requests found
package de.hdm_stuttgart.mi.sd1.store;
import java.util.Arrays;
/**
* A container holding a fixed
* number of integer values.
......@@ -7,7 +9,7 @@ package de.hdm_stuttgart.mi.sd1.store;
*/
public class IntegerStore {
final static int defaultCapacity = 4;
final static int initialCapacity = 4;
long[] values ; // Array containing our values
int numValues = 0; // Number of values present in the container so far.
......@@ -17,7 +19,7 @@ public class IntegerStore {
*
*/
public IntegerStore() {
values = new long[defaultCapacity];
values = new long[initialCapacity];
}
/**
* Create a new store being able to
......@@ -56,11 +58,7 @@ public class IntegerStore {
*/
public void addValue(long value) {
if (values.length <= numValues) { // Sufficient capacity available to add a new value?
final long[] currentArray = values;
values = new long[2 * currentArray.length]; // Double the current array's size.
for (int i = 0; i < currentArray.length; i++) {
values[i] = currentArray[i];
}
values = Arrays.copyOf(values, 2 * values.length); // Double the current array's size.
}
values[numValues++] = value;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment