Lists Answers

Lists Answers - 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

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Answers to Self-Study Questions Test Yourself #2 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++; } } 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
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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]; } Page 1 of 1 List Answers 2008/3/27 http://pages.cs.wisc.edu/~cs367-1/topics/Lists/answers.html...
View Full Document

This note was uploaded on 03/27/2008 for the course CS 367 taught by Professor Marvinsolomon during the Spring '08 term at Wisconsin.

Ask a homework question - tutors are online