List and iterator

11
List and Iterator

Transcript of List and iterator

Page 1: List and iterator

List and Iterator

Page 2: 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?• List lst = new List();

Page 3: 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()

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

Page 5: List and iterator

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.

Page 6: List and 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.

Page 7: List and iterator

• What are the 3 methods of the Iterator?• boolean hasNext(); 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

Page 8: List and iterator

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.

Page 9: List and iterator

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

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

Page 11: List and iterator

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.