1 / 5

Recitation 7

Recitation 7. Collections. Array List and Linked List. Array List and Linked List are implementations of the same interface: List. As a result, they have a number of methods that do the same job.

zoe
Download Presentation

Recitation 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recitation 7 Collections

  2. Array List and Linked List • Array List and Linked List are implementations of the same interface: List. • As a result, they have a number of methods that do the same job. • Different things: Array List actually uses an array to store objects. So when given an index, array list can directly access to that element. However, when we do deletion, insertion or increase the size, the time cost may be large.

  3. Every element in a linked list is actually stored in an instance of a class called Node. Each node contains the element, and a pointer points to the next node. • The linked list in Java is a little bit different . In Java, every node is actually double-linked, which means there are 2 pointers, one points to the previous node, and the other points to the next nodes. • A picture illustrates doubly linked list

  4. Program explanation • This program is modified from the last recitation. • AnObjectDatabaseUsingList is a class that has a list as property to store different kind of objects. You can use ObjectEditor to see the elements in an instance of it. The constructor take a list as input and assign the list to the list property. • Notice how ObjectEditor displays collection. • In Driver, there are 2 initializations, one uses Array List and another uses Linked List. AnObjectDatabaseUsingListcan work with either implementations. But different implementations may have different performances regarding the scenarios.

  5. Notice that the type of the list in AnObjectDatabaseUsingListis Object, which means you can put any kind of class in it. However, primitive types like int or char are not class, that’s why you need wrapper classes. • http://en.wikipedia.org/wiki/Primitive_wrapper_class • At the bottom of Driver is a block for test the performance of deletion (repeat 100000 times). Use that to compare the performance difference between array list and linked list. Also try to testsome other operations like insertion or random access.

More Related