Java ArrayLists: Flexible Array of Objects with Add and Remove Functionality - Prof. Eliza, Study notes of Computer Science

An overview of arraylists in java, a class that creates a dynamic array of objects with the ability to add and remove elements. Arraylists are part of the java.util package and allow data elements to be inserted and deleted from any location. Some methods from the arraylist interface, including constructors, size, add and remove methods, and retrieval methods.

Typology: Study notes

Pre 2010

Uploaded on 03/10/2009

koofers-user-2nc
koofers-user-2nc 🇺🇸

4.5

(1)

9 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture Notes 6: ArrayLists
Java has a nice class that creates a flexible array of objects, using shifting to make and cover
“holes” to allow adding and removing elements. When needed, more space is allocated for
a growing number of elements. ArrayList is part of the java.util package. ArrayLists
dynamically grow and shrink and allow data elements to be inserted and deleted from any
location (index). An ArrayList stores Objects, which means that when you retrieve an
element from an ArrayList you almost always need to cast it to its actual type.
How ArrayLists are actually implemented is not of concern to us. All we care about is
their interface. The following list provides some, but definitely not all, methods from the
ArrayList interface:
ArrayList() Constructor: creates an empty ArrayList.
boolean isEmpty() returns true if ArrayList empty, false otherwise.
void clear() empties the ArrayList.
int size() returns the number of elements in the ArrayList.
boolean add (Object o) Adds o to the end of the ArrayLists. Returns true.
void add(int i, Ob ject o) Inserts o at index i. Shifts elements with (now) larger
index to the right.
Object remove(int i) removes the indexed object and shifts elemnts with larger
index left.
Object get(int i) retrieves the indexed object without removing it.
int indexOf(Object o) returns index of first occurrence of o.
boolean contains(Object o) returns true if o occurs in the ArrayList, false oth-
erwise.
We will demonstrate the use ArrayLists in the Travelling Salesman Problem.
1

Partial preview of the text

Download Java ArrayLists: Flexible Array of Objects with Add and Remove Functionality - Prof. Eliza and more Study notes Computer Science in PDF only on Docsity!

Lecture Notes 6: ArrayLists

Java has a nice class that creates a flexible array of objects, using shifting to make and cover “holes” to allow adding and removing elements. When needed, more space is allocated for a growing number of elements. ArrayList is part of the java.util package. ArrayLists dynamically grow and shrink and allow data elements to be inserted and deleted from any location (index). An ArrayList stores Objects, which means that when you retrieve an element from an ArrayList you almost always need to cast it to its actual type.

How ArrayLists are actually implemented is not of concern to us. All we care about is their interface. The following list provides some, but definitely not all, methods from the ArrayList interface:

  • ArrayList() Constructor: creates an empty ArrayList.
  • boolean isEmpty() returns true if ArrayList empty, false otherwise.
  • void clear() empties the ArrayList.
  • int size() returns the number of elements in the ArrayList.
  • boolean add (Object o) Adds o to the end of the ArrayLists. Returns true.
  • void add(int i, Object o) Inserts o at index i. Shifts elements with (now) larger index to the right.
  • Object remove(int i) removes the indexed object and shifts elemnts with larger index left.
  • Object get(int i) retrieves the indexed object without removing it.
  • int indexOf(Object o) returns index of first occurrence of o.
  • boolean contains(Object o) returns true if o occurs in the ArrayList, false oth- erwise.

We will demonstrate the use ArrayLists in the Travelling Salesman Problem.