Arrays and Collections


The Collections Framework

A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers).

http://java.sun.com/docs/books/tutorial/collections/index.html

Java provides a Collections Framework, a set of classes in the java.util package, that enable the programmer to store and manipulate data in various ways. This library makes it very easy for the programmer to create and manipulate collections of information. Additionally, the implementations are optimized for efficiency. Many other programming languages also support a similar library.

Though having such a library makes life easier for the programmer, it is extremely important that students learn how such libraries are implemented. CS 112 and CS 245 both focus heavily on the implementation and performance issues associated with building various types of collections.


Arrays

The array is the most basic type of collection. Though other types of collections are easier to use, we'll begin with arrays. It is very important for you to understand how to use and manipulate arrays.

An array allows the programmer to store a list of elements, all of the same type, in a contiguous block of memory. The size of an array must be specified when it is instantiated and cannot change.

+Declaring Arrays

+Accessing Individual Array Elements

+Arrays and Loops

+Arrays and Methods

+Arrays of Objects

+Inserting Elements into a Sorted Array

+Deleting Elements from a Sorted Array

+Two-Dimensional Arrays


Array Lists

The java.util package provides a class ArrayList that efficiently provides the functionality of a standard array. Additionally, the ArrayList allows the programmer to insert new items anywhere in the list, without having to move other elements, and it also automatically resizes the underlying array when it becomes full. Feel free to browse the API to get a sense for the types of methods supported by the ArrayList. Unfortunately, for now you will be required to stick to arrays. We'll revisit use of ArrayLists as the semester progresses.


Sami Rollins

Date: 2007-08-10