List and Iterator
This presentation is the property of its rightful owner.
Sponsored Links
1 / 11

List and Iterator PowerPoint PPT Presentation


  • 70 Views
  • Uploaded on
  • Presentation posted in: General

List and Iterator. List Interface. Java has 3 classes which implement the List Interface - LinkedList, ArrayList and Vector. How do you use the ArrayList in your program? import java.util.* How to create a List object? List lst = new ArrayList(); Can we do this? Why?

Download Presentation

List and Iterator

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


List and iterator

List and Iterator


List interface

List Interface

  • Java has 3 classes which implement the List Interface - LinkedList, ArrayList and Vector.

  • How do you use the ArrayList in your program?

  • import java.util.*

  • How to create a List object?

  • List lst = new ArrayList();

  • Can we do this? Why?

  • List lst = new List();


List and iterator

  • How to output the List object?

  • With the List object, we don’t need to use loop structure to print out each element, simply output the List object. Only the output will be surrounded by square brackets.

  • Name some of the List methods.

  • boolean isEmpty()

  • int size()

  • Object set(int index, Object o)

  • void add(int index, Object o)

  • boolean add(Object o)

  • Iterator<E> iterator()


List and iterator

  • Do we need to cast the element retrieved from a list object?

  • Yes, unless the list object was created using type parameters, meaning?

  • List doubleList = new ArrayList();

  • /* fill in the Arraylist with some Double objects */

  • Double aDouble = doubleList.get(0); => ok?

  • List<Double> doubleList = new ArrayList<Double>();


List interface1

List Interface

  • List interface. What is an interface? P135

    1) All methods in the interface are both public and abstract.

    2) Any class implementing the interface has to implement every method, otherwise…

  • More rule on p139.


Iterator

Iterator

  • What is Iteration?

  • Iteration is a continual repeating of something.

  • What java structure fits this description?

  • Loops are iteration structure

  • What is an iterator?

  • An iterator’a sole purpose is to traverse a collection, one element at a time.

  • Is Iterator a class?

  • NO, it is an interface.


List and iterator

  • What are the 3 methods of the Iterator?

  • booleanhasNext(); E next(); void remove()

  • How to use a generic iterator?

  • List<String> list = new ArrayList<String>();

    <code to initialize list with strings>

    Iterator<String> itr = list.iterator();

    while(itr.hasNext())

    System.out.println(itr.next())

  • What is above while loop equivalent to?

  • for-each loop


Practice

practice

  • Write down the answers on the paper.

  • Write a generic List object of Integer.

  • Get an iterator from the List object.

  • Loop through the list and if the number is not mutiples of 3, remove it.


What is wrong with the codes

What is wrong with the codes?

Iterator<someType) itr = list.iterator();

while(true)

System.out.println(itr.next());

Iterator<someType) itr = list.iterator();

someType ob= itr.next();

itr.remove();

itr.remove();


List and iterator

  • What is the different to use iterator from for-each loop

  • Use a for-each loop for accessing and modifying objects in a list. Use an iterator for removal of objects.


Homework

Homework

  • Read Barron’s chapter 7 and BPJ lesson 40 about recursion. Write down any notes that you think are important. They can be in any form like questions and answers, or simply questions which you don’t understand. Everyone has to submit at least 25 notes or remarks.


  • Login