List - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

List

Description:

The List interface contains four methods: ListIterator listIterator() returns ... Person p3 = new Person('Sara', 'Claxton', '455-22-1335'); students.add(1, p1) ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 7
Provided by: DA964
Category:
Tags: claxton | list

less

Transcript and Presenter's Notes

Title: List


1
List
 
  • Chapter 5.3- The List Interface
  • The List interface is implemented by classes that
    represent an ordered collection of elements.
  • The List interface contains four methods
  • ListIterator listIterator() returns a list
    iterator of the elements in the list
  • Iterator iterator()- returns an iterator of the
    elements in the list
  • int size()- returns the number of elements in the
    list
  • void add(Object)- adds the specified element to
    the end of the list.

2
Iterator ListIterator
 
  • Chapter 5.3- The Iterator and ListIterator
    interfaces
  • The Iterator and ListIterator interfaces are used
    by classes that represent a collection of
    objects, and gives us a way to step through the
    objects one at a time.
  • The Iterator Interface contains three methods
  • boolean hasNext()- returns true if there exists
    another object in the list
  • Object next()- returns a reference to the next
    object in the list
  • void remove()- removes the object (most recently
    returned by the next method) from the list
  • ListIterator contains the above three methods, as
    well as two others
  • void add(Object obj)- inserts the object into
    the list immediately after the last element was
    returned by the next method
  • void set(Object obj)- replaces the last
    element returned by the next method

3
ArrayList
 
  • Chapter 6.7- ArrayList
  • ArrayList implements List and is the most basic
    Data Structure in Java
  • ArrayList is part of java.util and provides us
    with the same functionality as an array.
  • ArrayList stores 1-Dimentional Arrays
  • ArrayList stores a list of Objects and references
    them by index.
  • An array remains fixed throughout its existance,
    whereas an ArrayList grows and shrinks when
    needed.
  • (The Array List class would have been nice to
    use for the PascalsTriangle program.)
  • When you change an object that is within an
    ArrayList, you must replace it in the ArrayList.
    The object does not automatically change
    otherwise.
  • ArrayList indexes begin at 0 also.

4
ArrayList
 
  • Declaring an ArrayList
  • Constructor ArrayList()
  • Treat an ArrayList like an object.
  • ArrayList students new ArrayList()
  • Initializing Elements in an ArrayList
  • Methods void add(int index, Object obj)
    -inserts object
  • void add(Object obj) -adds object to end of
    list
  • Person p1 new Person(Mike, Pioro,
    445-43-4456)
  • Person p2 new Person(Rich, Johnson,
    421-54-0987)
  • Person p3 new Person(Sara, Claxton,
    455-22-1335)
  • students.add(1, p1)
  • students.add(0, p2)
  • students.add(p3)

5
ArrayList
 
  • Assigning Elements after initialized
  • Methods Object set(int index, Object obj)
    -returns the previous obj
  • Student s4 new Student(Colin, Zieroth,
    435-54-8765)
  • Student sOld students.set(0, s4)
  • Referencing Elements in an ArrayList
  • Methods Object get(int index) -returns
    object
  • Student sHere students.get(2)
  • Removing Elements from an ArrayList
  • Methods Object remove(int index) -removes
    object at index Student sRemoved
    students.remove(1)
  • Getting the length of the ArrayList
  • Methods int size() -returns the number of
    elements
  • int length students.size()

6
ArrayList Implementing Iterator
 
  • Creating an Iterator to step through the
    ArrayList
  • Methods Iterator iterator() returns an
    Iterator of the elements in list
  • ListIterator listIterator() returns a
    different type of iterator
  • ListIterator listIter students.listIterator()
  • Implementing an Iterator
  • ListIterator listIter students.listIterator()
  • while (listIter.hasNext())
  • System.out.println(((Person)
    iter.next()).getLastName())
Write a Comment
User Comments (0)
About PowerShow.com