Data Structures & Algorithms Lecture Notes

10 September 2010 • Classes and Abstract Data Types


Outline

Array Definition

Array Properties

Array Operations

Array Example

int [] collapse(int a[], int x)

  // Suppress runs of x in a.
  // collapse([0,0,1,1,0,0,0], 0) = [0,1,1,0]

  final int n = a.length
  int cpy [] = new int [n]
  int next = 0, i = 0

  while (i < n)
    cpy[next++] = a[i]
    if (a[i++] == x)
      while ((i < n) && (a[i] == x))
        i++

  return Arrays.copyOf(cpy, next)

See the code

Classes

Class Example

Methods

Method Example

Constructors

Default Constructors

Instance Variables

Instance-Variable Example

class AnArray {
  void put(int i, int e) { ... }
  int get(int i) { ... }

  private int size;
  private int elements [];
  }

Class-Component Access

Instance Variable Initialization

Default Initialization

Explicit Initialization

Constructor Initialization

Abstract Data Types

ADT Interfaces

ADT Operators

ADT Example

Classes and Abstract Data Types

What’s the difference? Why have both? ADTs are of objects, but are not objects.

What An Array Can Do

An Array ADT

class ArrayADT {

  ArrayADT(int n) { ... }
  void resize(int n) { ... }

  int get(int i) { ... }
  int size() { ... }

  void put(int i, int, v) { ... }
  void swap(int i, int j) { ... }

  // and so on.
  }

Summary

References


This page last modified on 10 September 2010.

Creative
    Commons License