ArrayLists

advertisement
ArrayLists
Arrays
Fixed size . . . Can’t change the
length or delete rows
All items must be the same type
ArrayLists
Variable size . . . Can change the
length and delete rows
Items can be of multiple types
Creating an ArrayList
ArrayList listname = new ArrayList();
Optionally you can specify an initial size
Adding items to the ArrayList
listName.Add(item);
Example on Slide 35, chapter 8
Expands the length of the list
Changing an item in an ArrayList
listName[index] = newvalue;
Obtaining an item in the ArrayList
typeofitem value = (typeofitem) listName[index];
Other ArrayList methods and properties
listname.Clear() //Removes all items, sets length to zero
listname.Count //Obtains the number of items in the list
listname.RemoveAt(index) //Removes a single item, shortens the length
Download