Answers to Self-Study Questions

Test Yourself #2

Question 1:

for (int pos = 0; pos < 2*k; pos+2) {
    L.add(pos, L.get(pos));
}

Question 2:

int pos = 0;
while (pos < L.size()) {
    String str = (String) L.get(pos);
    if (str.equals("hello")) {
        L.remove(pos);
    }
    else {
        pos++;
    }
}

Test Yourself #3

Question 1:

public Object remove(int pos) {
    // check for bad pos
    if (pos < 0 || pos >= numItems) 
        throw new IndexOutOfBoundsException();

    // get the item to be removed from pos
    Object ob = items[pos];

    // move items over to fill removed pos
    for (int k = pos; k < numItems-1; k++) {
        items[k] = items[k+1];
    }

    // decrease the number of items
    numItems--;

    // return the removed item
    return ob;
}

public Object get(int pos) {
    // check for bad pos
    if (pos < 0 || pos >= numItems) 
        throw new IndexOutOfBoundsException();

    // return the item at pos
    return items[pos];
}