SlideShare ist ein Scribd-Unternehmen logo
1 von 11
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();
• 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()
• 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 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
• 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.
• 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
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?
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();
• 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
• 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.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (16)

Presentation on python data type
Presentation on python data typePresentation on python data type
Presentation on python data type
 
Unit 4
Unit 4Unit 4
Unit 4
 
data structures and algorithms Unit 1
data structures and algorithms Unit 1data structures and algorithms Unit 1
data structures and algorithms Unit 1
 
Es6 day3
Es6 day3Es6 day3
Es6 day3
 
Python data type
Python data typePython data type
Python data type
 
Python l3
Python l3Python l3
Python l3
 
Python list functions
Python list functionsPython list functions
Python list functions
 
List Manipulation in FME
List Manipulation in FMEList Manipulation in FME
List Manipulation in FME
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
lists
listslists
lists
 
Introduction to Python programming Language
Introduction to Python programming LanguageIntroduction to Python programming Language
Introduction to Python programming Language
 
Data Structure lec#2
Data Structure lec#2Data Structure lec#2
Data Structure lec#2
 
Programming with Python - Week 2
Programming with Python - Week 2Programming with Python - Week 2
Programming with Python - Week 2
 
Intro to Lists
Intro to ListsIntro to Lists
Intro to Lists
 
The awesome algorithm
The awesome algorithmThe awesome algorithm
The awesome algorithm
 
Java Tutorial Lab 7
Java Tutorial Lab 7Java Tutorial Lab 7
Java Tutorial Lab 7
 

Andere mochten auch

Business analytics and data mining
Business analytics and data miningBusiness analytics and data mining
Business analytics and data miningJames Wong
 
Access data connection
Access data connectionAccess data connection
Access data connectionJames Wong
 
List in webpage
List in webpageList in webpage
List in webpageJames Wong
 
Crypto passport authentication
Crypto passport authenticationCrypto passport authentication
Crypto passport authenticationJames Wong
 
Valentines Golden Ratio.pdf
Valentines Golden Ratio.pdfValentines Golden Ratio.pdf
Valentines Golden Ratio.pdfbwlomas
 
Data and assessment
Data and assessmentData and assessment
Data and assessmentJames Wong
 
Ooad lab manual
Ooad  lab manualOoad  lab manual
Ooad lab manualPraseela R
 
Constitución 1812
Constitución 1812Constitución 1812
Constitución 1812mihaela16
 
Python data structures
Python data structuresPython data structures
Python data structuresYoung Alista
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsYoung Alista
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cacheYoung Alista
 
Text classification
Text classificationText classification
Text classificationYoung Alista
 
Exception handling
Exception handlingException handling
Exception handlingYoung Alista
 
Key exchange in crypto
Key exchange in cryptoKey exchange in crypto
Key exchange in cryptoYoung Alista
 

Andere mochten auch (20)

Business analytics and data mining
Business analytics and data miningBusiness analytics and data mining
Business analytics and data mining
 
Access data connection
Access data connectionAccess data connection
Access data connection
 
List in webpage
List in webpageList in webpage
List in webpage
 
Crypto passport authentication
Crypto passport authenticationCrypto passport authentication
Crypto passport authentication
 
Valentines Golden Ratio.pdf
Valentines Golden Ratio.pdfValentines Golden Ratio.pdf
Valentines Golden Ratio.pdf
 
Reflection
ReflectionReflection
Reflection
 
Bumper Mounting LED Light Bar ELM-5027W
Bumper Mounting LED Light Bar ELM-5027WBumper Mounting LED Light Bar ELM-5027W
Bumper Mounting LED Light Bar ELM-5027W
 
Data and assessment
Data and assessmentData and assessment
Data and assessment
 
Ooad lab manual
Ooad  lab manualOoad  lab manual
Ooad lab manual
 
Constitución 1812
Constitución 1812Constitución 1812
Constitución 1812
 
Constitución 1812 erf
Constitución 1812 erfConstitución 1812 erf
Constitución 1812 erf
 
Python data structures
Python data structuresPython data structures
Python data structures
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cache
 
Poo java
Poo javaPoo java
Poo java
 
Text classification
Text classificationText classification
Text classification
 
List in webpage
List in webpageList in webpage
List in webpage
 
Exception handling
Exception handlingException handling
Exception handling
 
Key exchange in crypto
Key exchange in cryptoKey exchange in crypto
Key exchange in crypto
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 

Ähnlich wie List and iterator

Java class 5
Java class 5Java class 5
Java class 5Edureka!
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptxASRPANDEY
 
collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptxSoniaKapoor56
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonchanna basava
 
Java10 Collections and Information
Java10 Collections and InformationJava10 Collections and Information
Java10 Collections and InformationSoftNutx
 
A2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.pptA2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.pptRithwikRanjan
 
L11 array list
L11 array listL11 array list
L11 array listteach4uin
 
ArrayList class and useful methods.pptx
ArrayList class and useful methods.pptxArrayList class and useful methods.pptx
ArrayList class and useful methods.pptxAbid523408
 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementationLavanyaJ28
 
Python programming
Python programmingPython programming
Python programmingsirikeshava
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets Hitesh-Java
 

Ähnlich wie List and iterator (20)

STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Java class 5
Java class 5Java class 5
Java class 5
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptx
 
collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptx
 
tupple.pptx
tupple.pptxtupple.pptx
tupple.pptx
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
 
Java10 Collections and Information
Java10 Collections and InformationJava10 Collections and Information
Java10 Collections and Information
 
A2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.pptA2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.ppt
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
L11 array list
L11 array listL11 array list
L11 array list
 
F# array searching
F#  array searchingF#  array searching
F# array searching
 
Lists
ListsLists
Lists
 
Collections Training
Collections TrainingCollections Training
Collections Training
 
ArrayList class and useful methods.pptx
ArrayList class and useful methods.pptxArrayList class and useful methods.pptx
ArrayList class and useful methods.pptx
 
lecture 02.2.ppt
lecture 02.2.pptlecture 02.2.ppt
lecture 02.2.ppt
 
Collection Framework-1.pptx
Collection Framework-1.pptxCollection Framework-1.pptx
Collection Framework-1.pptx
 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementation
 
Python programming
Python programmingPython programming
Python programming
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 

Mehr von James Wong

Multi threaded rtos
Multi threaded rtosMulti threaded rtos
Multi threaded rtosJames Wong
 
Data mining and knowledge discovery
Data mining and knowledge discoveryData mining and knowledge discovery
Data mining and knowledge discoveryJames Wong
 
Big picture of data mining
Big picture of data miningBig picture of data mining
Big picture of data miningJames Wong
 
How analysis services caching works
How analysis services caching worksHow analysis services caching works
How analysis services caching worksJames Wong
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsJames Wong
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherenceJames Wong
 
Abstract data types
Abstract data typesAbstract data types
Abstract data typesJames Wong
 
Abstraction file
Abstraction fileAbstraction file
Abstraction fileJames Wong
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cacheJames Wong
 
Abstract class
Abstract classAbstract class
Abstract classJames Wong
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisJames Wong
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaJames Wong
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJames Wong
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and pythonJames Wong
 

Mehr von James Wong (20)

Data race
Data raceData race
Data race
 
Multi threaded rtos
Multi threaded rtosMulti threaded rtos
Multi threaded rtos
 
Recursion
RecursionRecursion
Recursion
 
Data mining and knowledge discovery
Data mining and knowledge discoveryData mining and knowledge discovery
Data mining and knowledge discovery
 
Cache recap
Cache recapCache recap
Cache recap
 
Big picture of data mining
Big picture of data miningBig picture of data mining
Big picture of data mining
 
How analysis services caching works
How analysis services caching worksHow analysis services caching works
How analysis services caching works
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessors
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherence
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cache
 
Object model
Object modelObject model
Object model
 
Abstract class
Abstract classAbstract class
Abstract class
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and python
 
Inheritance
InheritanceInheritance
Inheritance
 
Api crash
Api crashApi crash
Api crash
 

Kürzlich hochgeladen

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Kürzlich hochgeladen (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

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