lists based on content from: java foundations, 3rd edition

16
Lists Based on content from: Java Foundations, 3rd Edition

Upload: lorraine-moore

Post on 28-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lists Based on content from: Java Foundations, 3rd Edition

Lists

Based on content from:Java Foundations, 3rd Edition

Page 2: Lists Based on content from: Java Foundations, 3rd Edition

ListsA list is a linear collection, like stacks and queues, but is more flexibleAdding and removing elements in lists can occur at either end or anywhere in the middleWe will examine three types of list collections: ordered lists unordered lists indexed lists

CS 221 - Computer Science II

Page 3: Lists Based on content from: Java Foundations, 3rd Edition

Ordered ListsThe elements in an ordered list are ordered by some inherent characteristic of the elements names in alphabetical order scores in ascending order

The elements themselves determine where they are stored in the list

CS 221 - Computer Science II

Page 4: Lists Based on content from: Java Foundations, 3rd Edition

Ordered ListsAn ordered list:

CS 221 - Computer Science II

Page 5: Lists Based on content from: Java Foundations, 3rd Edition

Unordered ListsThere is an order to the elements in an unordered list, but that order is not based on element characteristicsThe user of the list determines the order of the elementsA new element can be put on the front or the rear of the list, or it can be inserted after a particular element already in the list

CS 221 - Computer Science II

Page 6: Lists Based on content from: Java Foundations, 3rd Edition

Unordered ListsAn unordered list:

CS 221 - Computer Science II

Page 7: Lists Based on content from: Java Foundations, 3rd Edition

Indexed ListsIn an indexed list, elements are referenced by their numeric position in the listLike an unordered list, there is no inherent relationship among the elementsThe user can determine the orderEvery time the list changes, the indexes are updated

CS 221 - Computer Science II

Page 8: Lists Based on content from: Java Foundations, 3rd Edition

Indexed ListsAn indexed list:

CS 221 - Computer Science II

Page 9: Lists Based on content from: Java Foundations, 3rd Edition

Lists in the Java APIThe list classes in the Java API primarily support the concept of an indexed list (and somewhat an unordered list)The API does not have any classes that directly implement an ordered listThe ArrayList and LinkedList classes both implement the List<E> interface

CS 221 - Computer Science II

Page 10: Lists Based on content from: Java Foundations, 3rd Edition

Lists in the Java APISome of the operations from the List<E> interface:

CS 221 - Computer Science II

Page 11: Lists Based on content from: Java Foundations, 3rd Edition

SerializationAny class whose objects will be saved are tagged with the Serializable interface

CS 221 - Computer Science II

Page 12: Lists Based on content from: Java Foundations, 3rd Edition

Implementing ListsThe following operations are common to most types of lists:

CS 221 - Computer Science II

Page 13: Lists Based on content from: Java Foundations, 3rd Edition

Implementing ListsOperation particular to an ordered list:

Operations particular to an unordered lists:

CS 221 - Computer Science II

Page 14: Lists Based on content from: Java Foundations, 3rd Edition

Class Diagram of List Classes

CS 221 - Computer Science II

Page 15: Lists Based on content from: Java Foundations, 3rd Edition

Implementing a List with an Array

Since elements can be added anywhere in the list, shifting elements cannot be avoidedSo a straightforward implementation can be adopted:

CS 221 - Computer Science II

Page 16: Lists Based on content from: Java Foundations, 3rd Edition

Implementing a List with Links

A classic linked list is an obvious choice for implementing a list collectionWill need to implement Node classBoth head and tail references are maintained, as well as an integer count

CS 221 - Computer Science II