Data Structures & Algorithms Lecture Notes

21 September 2010 • Classes and Abstract Data Types


Outline

Array Definition

Array Properties

Arrays

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-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 21 January 2010.

Creative
    Commons License