Arraylistnotes

Arraylistnotes - Arrays are very useful to keep track of...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Arrays are very useful to keep track of multiple values/objects of the same type. However, they have disadvantages: H 1. You need to specify the capacity of an array when you allocate it, and that size is fixed (it cannot be changed once the array is allocated) If you need more space, you need to: a) Allocate a bigger array b) Copy all elements from the old array to the new array (using a loop) c) Store the address of the new array in the variable which contains the address of the new array Doable, but annoying. 2. Suppose that you want to insert an element at position i or delete an element from position i in your array Insertion at position i: Shift everything at positions i < x < number of elements in the array to position x+1 (to make space at position i for new element) Insert your new element at position i Deleting from position i: Shift everything at positions i < x < number of elements in the array to position x - 1 Doable, but annoying What we would like: A class where the objects are containers for other values/objects Something which has the features of an array (multiple elements) but will free us from managing the bookkeeping details of an array. f Container classes were created for this purpose: ArrayList, LinkedList, HashSet, TreeSet, . .. In this course, we will use ArrayList because its functionality is very similar to that of arrays. s Declaring container object variables: D A container class is very similar to any other class. You declare variables whose type is that class.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/22/2011 for the course COMP 202 taught by Professor Verbrugge during the Fall '07 term at McGill.

Page1 / 3

Arraylistnotes - Arrays are very useful to keep track of...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online