ArrayList Practice for our next Project

posted by: Mr. Bergquist 25 February 2012 One Comment

Do these Practice It! problems to familiarize yourself with ArrayLists.  The trickiest thing about them is keeping track of how the size changes as you add and remove values.  Keep in mind that if you remove a value, the next value in the list now has the same index as the one you removed!  I find it useful to think of these in terms of while loops.

Don’t forget that these are static methods!  We’ll come back to instance methods with our next project which will require you to use an ArrayList as a component of a larger class.  And don’t peak at the solutions if you want full credit.

1. Array List Mystery [143 Practice Midterm 5 (09wi) ArrayListMystery]

2. switchPairs [ArrayList switchPairs] – keep in mind that since the ArrayList is addressed by Reference, it is updated by your method automatically and these do NOT need to be returned (i.e. public static void markLength4… )

3. markLengthFour [ArrayList markLength4] — beware of infinite loops… what happens to your indexes when you add a value?

4. removeBadPairs [143 Practice Midterm 3 removeBadPairs] — this one will require very careful thinking about how indexes change as you remove elements…

1 Star2 Stars3 Stars4 Stars5 Stars (15 votes, average: 3.93 out of 5)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>