The list given has values [1, 3, 2, 7].
After calling list.duplicate(), the list should store the values
[1, 1, 3, 3, 2, 2, 7, 7].
public class ArrayIntList {
private int[] elementData;
private int size = 0;
// Doubles-up each element in this list
public void duplicate() {
// TODO:
}
}
Answer in Java/C# only
public class ArrayIntList { private int[] elementData; private int size = 0; // Doubles-up each element in this list public void duplicate() { int[] result = new int[elementData.length * 2]; int index = 0; for (int i = 0; i < result.length; i += 2) { result[i] = elementData[index]; result[i+1] = elementData[index]; ++index; } elementData = result; size *= 2; } }
Get Answers For Free
Most questions answered within 1 hours.