Lists Based on content from: Java Foundations, 3rd Edition.
-
Upload
lorraine-moore -
Category
Documents
-
view
215 -
download
0
Transcript of Lists Based on content from: Java Foundations, 3rd Edition.
![Page 1: Lists Based on content from: Java Foundations, 3rd Edition.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/1.jpg)
Lists
Based on content from:Java Foundations, 3rd Edition
![Page 2: Lists Based on content from: Java Foundations, 3rd Edition.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/2.jpg)
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.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/3.jpg)
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.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/4.jpg)
Ordered ListsAn ordered list:
CS 221 - Computer Science II
![Page 5: Lists Based on content from: Java Foundations, 3rd Edition.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/5.jpg)
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.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/6.jpg)
Unordered ListsAn unordered list:
CS 221 - Computer Science II
![Page 7: Lists Based on content from: Java Foundations, 3rd Edition.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/7.jpg)
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.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/8.jpg)
Indexed ListsAn indexed list:
CS 221 - Computer Science II
![Page 9: Lists Based on content from: Java Foundations, 3rd Edition.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/9.jpg)
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.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/10.jpg)
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.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/11.jpg)
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.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/12.jpg)
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.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/13.jpg)
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.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/14.jpg)
Class Diagram of List Classes
CS 221 - Computer Science II
![Page 15: Lists Based on content from: Java Foundations, 3rd Edition.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/15.jpg)
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.](https://reader030.fdocuments.in/reader030/viewer/2022032604/56649e665503460f94b6041f/html5/thumbnails/16.jpg)
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