Question 1: for (int pos = 0; pos < 2 * k; pos += 2) { words.add(pos, words.get(pos)); } Question 2: int pos = 0; while (pos < words.size()) { String word = words.get(pos); if (word.equals("hello")) { words.remove(pos); } else { pos++; } }
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]; }