SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Lists in Java
Part of the Collections Framework
Kinds of Collections
• Collection--a group of objects, called elements
– Set--An unordered collection with no duplicates
• SortedSet--An ordered collection with no duplicates
– List--an ordered collection, duplicates are allowed
• Map--a collection that maps keys to values
– SortedMap--a collection ordered by the keys
• Note that there are two distinct hierarchies
Using Collections
• import java.util.*
or import java.util.Collection;
• There is a sister class, java.util.Collections; that
provides a number of algorithms for use with
collections: sort, binarySearch, copy,
shuffle, reverse, max, min, etc.
Collections are interfaces
• Collection is actually an interface
• Each kind of Collection has one or more
implementations
• You can create new kinds of Collections
• When you implement an interface, you promise to
supply the required methods
• Some Collection methods are optional
– How can an interface declare an optional method?
Creating a Collection
• All Collection implementations should have two
constructors:
– A no-argument constructor to create an empty collection
– A constructor with another Collection as argument
• All the Sun-supplied implementations obey this
rule, but--
• If you implement your own Collection type, this
rule cannot be enforced, because an Interface
cannot specify constructors
Collection: Basic operations
int size( );
boolean isEmpty( );
boolean contains(Object element);
boolean add(Object element); // Optional
boolean remove(Object element); // Optional
Iterator iterator( );
Collection: Iterator
boolean hasNext( );
// true if there is another element
Object next( );
// returns the next element (advances the iterator)
void remove( ); // Optional
// removes the element returned by next
}
public interface Iterator {
Using an Iterator
• static void printAll (Collection coll) {
Iterator iter = coll.iterator( );
while (iter.hasNext( )) {
System.out.println(iter.next( ) );
}
}
• Note that this code is polymorphic--it will work
for any collection
Collection: Bulk operations
boolean containsAll(Collection c);
boolean addAll(Collection c); // Optional
boolean removeAll(Collection c); // Optional
boolean retainAll(Collection c); // Optional
void clear( ); // Optional
• addAll, removeAll, retainAll return true if the
object receiving the message was modified
Mixing Collection types
• Note that most methods, such as
boolean containsAll(Collection c);
are defined for any type of Collection, and take
any type of Collection as an argument
• This makes it very easy to work with different
types of Collections
singleton
• Collections.singleton(e) returns an
immutable set containing only the element e
• c.removeAll(Collections.singleton(e));
will remove all occurrences of e from the
Collection c
Collection: Array operations
• Object[ ] toArray( );
– creates a new array of Objects
• Object[ ] toArray(Object a[ ]);
– Allows the caller to provide the array
• Examples:
Object[ ] a = c.toArray( );
String[ ] a;
a = (String[ ]) c.toArray(new String[0]);
The List interface
• A List is ordered and may have duplicates
• Operations are exactly those for Collections
int size( );
boolean isEmpty( );
boolean contains(Object e);
boolean add(Object e);
boolean remove(Object e);
Iterator iterator( );
boolean containsAll(Collection c);
boolean addAll(Collection c);
boolean removeAll(Collection c);
boolean retainAll(Collection c);
void clear( );
Object[ ] toArray( );
Object[ ] toArray(Object a[ ]);
List implementations
• List is an interface; you can’t say new List ( )
• There are two implementations:
– LinkedList gives faster insertions and deletions
– ArrayList gives faster random access
• It’s poor style to expose the implementation, so:
• Good: List list = new LinkedList ( );
Bad: LinkedList list = new LinkedList ( );
Inherited List methods
• list.remove(e) removes the first e
• add and addAll add to the end of the list
• To append one list to another:
list1.addAll(list2);
• To append two lists into a new list:
List list3 = new ArrayList(list1);
list3.addAll(list2);
• Again, it's good style to hide the implementation
List: Positional access
Object get(int index); // Required --
// the rest are optional
Object set(int index, Object element);
void add(int index, Object element);
Object remove(int index);
abstract boolean addAll(int index, Collection c);
• These operations are more efficient with the ArrayList
implementation
List: Searching
int indexOf(Object o);
int lastIndexOf(Object o);
• equals and hashCode work even if
implementations are different
Interface List: Iteration
• Iterators specific to Lists:
ListIterator listIterator( );
ListIterator listIterator(int index);
– starts at the position indicated (0 is first element)
• Inherited methods:
boolean hasNext( );
Object next( );
void remove( );
• Additional methods:
boolean hasPrevious()
Object previous()
List: Iterating backwards
boolean hasPrevious( );
Object previous( );
int nextIndex( );
int previousIndex( );
• Think of the iterator as “between” elements
• Hence, next followed by previous gives you
the same element each time
List: More operations
• void add(Object o);
– Inserts an object at the cursor position
• Object set(Object o); // Optional
– Replace the current element; return the old one
• Object remove(int index); // Optional
– Remove and return the element at that position
List: Range-view
• List subList(int from, int to); allows you to
manipulate part of a list
• A sublist may be used just like any other list
The End
http://java.sun.com/docs/books/tutorial/collections
/interfaces/collection.html
http://java.sun.com/docs/books/tutorial/collections
/interfaces/list.html

Weitere ähnliche Inhalte

Was ist angesagt?

Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Array vs array list
Array vs array listArray vs array list
Array vs array listRavi Shetye
 
Java collections
Java collectionsJava collections
Java collectionsAmar Kutwal
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - JavaDrishti Bhalla
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaEdureka!
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets Hitesh-Java
 
5 collection framework
5 collection framework5 collection framework
5 collection frameworkMinal Maniar
 
Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections frameworkRiccardo Cardin
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection frameworkankitgarg_er
 
Python advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmPython advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmJohn(Qiang) Zhang
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & setsRatnaJava
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 

Was ist angesagt? (20)

Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Array vs array list
Array vs array listArray vs array list
Array vs array list
 
Java collections
Java collectionsJava collections
Java collections
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
 
Array list(1)
Array list(1)Array list(1)
Array list(1)
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
 
List
ListList
List
 
Java collection
Java collectionJava collection
Java collection
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
 
Collections framework
Collections frameworkCollections framework
Collections framework
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Priority Queue
Priority QueuePriority Queue
Priority Queue
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
Collections
CollectionsCollections
Collections
 
Python advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmPython advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithm
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & sets
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 

Andere mochten auch

Andere mochten auch (13)

Moving to ws2003
Moving to ws2003Moving to ws2003
Moving to ws2003
 
Ch1
Ch1Ch1
Ch1
 
Us bdrv tips2
Us bdrv tips2Us bdrv tips2
Us bdrv tips2
 
Dna structure
Dna structureDna structure
Dna structure
 
Twar05003 win hec05
Twar05003 win hec05Twar05003 win hec05
Twar05003 win hec05
 
W982 05092004
W982 05092004W982 05092004
W982 05092004
 
Discoverer online training 10g r2
Discoverer online training 10g r2Discoverer online training 10g r2
Discoverer online training 10g r2
 
Tips tricks052003
Tips tricks052003Tips tricks052003
Tips tricks052003
 
9idwh
9idwh9idwh
9idwh
 
Sirt roundtable malicious-emailtrendmicro
Sirt roundtable malicious-emailtrendmicroSirt roundtable malicious-emailtrendmicro
Sirt roundtable malicious-emailtrendmicro
 
Ch03
Ch03Ch03
Ch03
 
Arc ims tips
Arc ims tipsArc ims tips
Arc ims tips
 
Swine flu f inal
Swine flu f inalSwine flu f inal
Swine flu f inal
 

Ähnlich wie Lists

Ähnlich wie Lists (20)

Collections
CollectionsCollections
Collections
 
Collections
CollectionsCollections
Collections
 
JavaCollections.ppt
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
 
JavaCollections.ppt
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
 
Best core & advanced java classes in mumbai
Best core & advanced java classes in mumbaiBest core & advanced java classes in mumbai
Best core & advanced java classes in mumbai
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
Collections
CollectionsCollections
Collections
 
Collections in java
Collections in javaCollections in java
Collections in java
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_Framework
 
collectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptxcollectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptx
 
Lecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysLecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arrays
 
COLLECTIONS.pptx
COLLECTIONS.pptxCOLLECTIONS.pptx
COLLECTIONS.pptx
 
Java util
Java utilJava util
Java util
 
collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptx
 
Collection Framework-1.pptx
Collection Framework-1.pptxCollection Framework-1.pptx
Collection Framework-1.pptx
 
Collections (1)
Collections (1)Collections (1)
Collections (1)
 
Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshan
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 

Kürzlich hochgeladen

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Kürzlich hochgeladen (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

Lists

  • 1. Lists in Java Part of the Collections Framework
  • 2. Kinds of Collections • Collection--a group of objects, called elements – Set--An unordered collection with no duplicates • SortedSet--An ordered collection with no duplicates – List--an ordered collection, duplicates are allowed • Map--a collection that maps keys to values – SortedMap--a collection ordered by the keys • Note that there are two distinct hierarchies
  • 3. Using Collections • import java.util.* or import java.util.Collection; • There is a sister class, java.util.Collections; that provides a number of algorithms for use with collections: sort, binarySearch, copy, shuffle, reverse, max, min, etc.
  • 4. Collections are interfaces • Collection is actually an interface • Each kind of Collection has one or more implementations • You can create new kinds of Collections • When you implement an interface, you promise to supply the required methods • Some Collection methods are optional – How can an interface declare an optional method?
  • 5. Creating a Collection • All Collection implementations should have two constructors: – A no-argument constructor to create an empty collection – A constructor with another Collection as argument • All the Sun-supplied implementations obey this rule, but-- • If you implement your own Collection type, this rule cannot be enforced, because an Interface cannot specify constructors
  • 6. Collection: Basic operations int size( ); boolean isEmpty( ); boolean contains(Object element); boolean add(Object element); // Optional boolean remove(Object element); // Optional Iterator iterator( );
  • 7. Collection: Iterator boolean hasNext( ); // true if there is another element Object next( ); // returns the next element (advances the iterator) void remove( ); // Optional // removes the element returned by next } public interface Iterator {
  • 8. Using an Iterator • static void printAll (Collection coll) { Iterator iter = coll.iterator( ); while (iter.hasNext( )) { System.out.println(iter.next( ) ); } } • Note that this code is polymorphic--it will work for any collection
  • 9. Collection: Bulk operations boolean containsAll(Collection c); boolean addAll(Collection c); // Optional boolean removeAll(Collection c); // Optional boolean retainAll(Collection c); // Optional void clear( ); // Optional • addAll, removeAll, retainAll return true if the object receiving the message was modified
  • 10. Mixing Collection types • Note that most methods, such as boolean containsAll(Collection c); are defined for any type of Collection, and take any type of Collection as an argument • This makes it very easy to work with different types of Collections
  • 11. singleton • Collections.singleton(e) returns an immutable set containing only the element e • c.removeAll(Collections.singleton(e)); will remove all occurrences of e from the Collection c
  • 12. Collection: Array operations • Object[ ] toArray( ); – creates a new array of Objects • Object[ ] toArray(Object a[ ]); – Allows the caller to provide the array • Examples: Object[ ] a = c.toArray( ); String[ ] a; a = (String[ ]) c.toArray(new String[0]);
  • 13. The List interface • A List is ordered and may have duplicates • Operations are exactly those for Collections int size( ); boolean isEmpty( ); boolean contains(Object e); boolean add(Object e); boolean remove(Object e); Iterator iterator( ); boolean containsAll(Collection c); boolean addAll(Collection c); boolean removeAll(Collection c); boolean retainAll(Collection c); void clear( ); Object[ ] toArray( ); Object[ ] toArray(Object a[ ]);
  • 14. List implementations • List is an interface; you can’t say new List ( ) • There are two implementations: – LinkedList gives faster insertions and deletions – ArrayList gives faster random access • It’s poor style to expose the implementation, so: • Good: List list = new LinkedList ( ); Bad: LinkedList list = new LinkedList ( );
  • 15. Inherited List methods • list.remove(e) removes the first e • add and addAll add to the end of the list • To append one list to another: list1.addAll(list2); • To append two lists into a new list: List list3 = new ArrayList(list1); list3.addAll(list2); • Again, it's good style to hide the implementation
  • 16. List: Positional access Object get(int index); // Required -- // the rest are optional Object set(int index, Object element); void add(int index, Object element); Object remove(int index); abstract boolean addAll(int index, Collection c); • These operations are more efficient with the ArrayList implementation
  • 17. List: Searching int indexOf(Object o); int lastIndexOf(Object o); • equals and hashCode work even if implementations are different
  • 18. Interface List: Iteration • Iterators specific to Lists: ListIterator listIterator( ); ListIterator listIterator(int index); – starts at the position indicated (0 is first element) • Inherited methods: boolean hasNext( ); Object next( ); void remove( ); • Additional methods: boolean hasPrevious() Object previous()
  • 19. List: Iterating backwards boolean hasPrevious( ); Object previous( ); int nextIndex( ); int previousIndex( ); • Think of the iterator as “between” elements • Hence, next followed by previous gives you the same element each time
  • 20. List: More operations • void add(Object o); – Inserts an object at the cursor position • Object set(Object o); // Optional – Replace the current element; return the old one • Object remove(int index); // Optional – Remove and return the element at that position
  • 21. List: Range-view • List subList(int from, int to); allows you to manipulate part of a list • A sublist may be used just like any other list