1 / 11

List and Iterator

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?

ellie
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. 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. List and Iterator

  2. 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();

  3. 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()

  4. 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>();

  5. 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.

  6. 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.

  7. 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

  8. 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.

  9. 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();

  10. 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.

  11. 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.

More Related