Announcements - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Announcements

Description:

remove, resizing, insert in the middle - expensive, time proportional to the list size ... Fuji. 101. Linked list example declaration (same as ArrayList) ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 8
Provided by: CSCF
Learn more at: http://www.cs.utexas.edu
Category:

less

Transcript and Presenter's Notes

Title: Announcements


1
Announcements Review
  • Last Time Collections
  • ArrayList -
  • look up - constant time
  • remove, resizing, insert in the middle -
    expensive, time proportional to the list size
  • Today a new collection
  • Linked List - similar operations as ArrayList,
    but different storage model
  • resizing, removal, insertion - constant time
  • find - proportional to list size.
  • Announcements
  • Lab 9 Inheritance Defining using class
    hiearchies
  • Read
  • RS 11

2
Linked List Storage Model
start
end
null
next
prev
Key
node
  • No diriect indexing, as in arrays, so you must
    iterate over the list to enumerate an element
  • Adding at the front or the back is constant
    time, i.e., resizing is cheap

3
InterestingLinkedListltEgt Methods
  • size() - returns number of objects
  • add(Object o) - adds at end of list
  • get(int i) - returns ith object in the list
  • set(int i, Object o) - stores o at ith position
    in list
  • add(int i, Object o) - adds o at ith position,
    shifts other objects to the right
  • E remove() - removes and returns element current
    position, shifts elements to the left
  • removeFirst(), removeLast(), remove(E v),
    remove(int i), remove()

4
InterestingLinkedListltEgt Methods
  • E clone() - return a shallow copy of the list
  • Boolean addAll(LinkedListltEgt c) - add collection
    c to end of list, return true if list changes
  • E peek() - return the first element without
    removing it from the list
  • Boolean hasNext(), E next() - iteration

5
Linked List
apples
null
33
41
8
14
101
McIntosh
Red
Gala
Golden Delicious
Fuji
Linked list example declaration (same as
ArrayList) LinkedListltItemsgt apples new
LinkedListltItemsgt() Items a new
Items(McIntosh, 33) apples.add(a) a new
Items(Red, 41) apples.add(a) ...
6
Iterators for LinkedList
  • // Basic format
  • IteratorltEgt itr list.iterator()
  • while (itr.hasNext())
  • E element itr.next() // do something with
    the object
  • // Example Delete Gala apples inventory
  • LinkedListltItemsgt apples new LinkedListltItemsgt()
  • ...
  • IteratorltItemsgt i apples.iterator()
  • while (i.hasNext())
  • Items item i.next()
  • if (item.getName().equals(Gala))
  • i.remove()

7
BlueJ
Write a Comment
User Comments (0)
About PowerShow.com