List ADT, Lists and Java API - Lecture Notes | COMP SCI 367, Study notes of Data Structures and Algorithms

W2(b) Lecture Notes (Skrentny) Material Type: Notes; Class: Introduction to Data Structures; Subject: COMPUTER SCIENCES; University: University of Wisconsin - Madison; Term: Spring 2014;

Typology: Study notes

2013/2014

Uploaded on 03/11/2014

kmfischer3
kmfischer3 🇺🇸

12 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Copyright 2014 Jim Skrentny CS 367 L3 - 1
CS 367 Announcements
Tuesday, Januar y 28, 2014
Email [email protected] by this Friday if you:
have a conflict with any of the exams listed on course website
participate in religious observances that may interfere with course requirements
have a VISA from the McBurney Disability Resource Center
Course Website - http://pages.cs.wisc.edu/~cs367-1/
Sign Up for Piazza
New to Linux?
WACM Linux Orientation Session, tomorrow 1/29, 5:30 pm, 1221 CS
also see New to Linux? link on website
Last Time
Implementing the Bag ADT
using Object class for generality
casting when using Object
using Java generics for generality
List ADT intro
Today
List ADT
coding as a Java interface
using Lists
implementing using an array (SimpleArrayList)
Lists and the Java API
Next Time
Iterators
what they are
using them
defining them
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download List ADT, Lists and Java API - Lecture Notes | COMP SCI 367 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS 367 Announcements

Tuesday, January 28, 2014

Email [email protected] by this Friday if you:

• have a conflict with any of the exams listed on course website

• participate in religious observances that may interfere with course requirements

• have a VISA from the McBurney Disability Resource Center

Course Website - http://pages.cs.wisc.edu/~cs367-1/

Sign Up for Piazza

New to Linux?

• WACM Linux Orientation Session, tomorrow 1/29, 5:30 pm, 1221 CS

• also see “New to Linux?” link on website

Last Time

Implementing the Bag ADT

• using Object class for generality

• casting when using Object

• using Java generics for generality

List ADT intro

Today

List ADT

• coding as a Java interface

• using Lists

• implementing using an array (SimpleArrayList)

Lists and the Java API

Next Time

Iterators

• what they are

• using them

• defining them

Recall the List ADT

Concept

Operations

• add an item at the end of the list

• add an item at a specified position

• determine whether or not a list contains a specified item

• determine the size of the list

• determine whether or not the list is empty

• get the item at a specified position

• remove the item at a specified position

Issues

Null – not allowed, detect then signal with IllegalArgumentException

Bad position – detect then signal with IndexOutOfBoundsException

Empty list – handle as a bad position

List ADT as a Generic Java Interface (cont.)

  • Return the number of items in the List.
  • @return the number of items in the List */ int size ();
  • Return true iff the List is empty.
  • @return true if the List is empty, false otherwise */ boolean isEmpty ();
  • Return the item at position pos in the List.
  • @param pos the position of the item to return
  • @return the item at position pos
  • @throws IndexOutOfBoundsException if pos is less than 0
  • or greater than or equal to size() */ E get (int pos);
  • Remove and return the item at position pos in the List,
  • moving the items originally in positions pos+1 through
  • size() one place to the left to fill in the gap.
  • @param pos the position at which to remove the item
  • @return the item at position pos
  • @throws IndexOutOfBoundsException if pos is less than 0
  • or greater than or equal to size() */ E remove (int pos); }

Using the List ADT

 Assume L is a List. What does the following code fragment do in general?

for (int i = 0; i < L.size(); i++) { L.remove(i); }

Implementing the List ADT using a Generic Array-based List

public class SimpleArrayList implements ListADT {

private E[] items; //the items in the List private int numItems; //the # of items in the List

public SimpleArrayList() {

//*** required ListADT methods ***

public void add(E item) { ... } public void add(int pos, E item) { ... } public E remove(int pos) { ... } public E get (int pos) { ... } public boolean contains (E item) { ... } public int size() { ... } public boolean isEmpty() { ... }

//*** additional optional array list methods ***

Implementing contains

 Complete the method below so that it returns true iff the given item is in the list.

public boolean contains(E item) {

Java API Lists