SlideShare une entreprise Scribd logo
1  sur  25
Télécharger pour lire hors ligne
Advanced Java Programming
ArrayList, LinkedList ,Vector and Stack
By
Ravi Kant Sahu
Asst. Professor, LPU
ArrAyList
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
ArrayList
• The ArrayList class extends AbstractList and implements the List
interface.
• Defined in java.util package.
• ArrayList supports dynamic arrays that can grow as needed.
• Array lists are created with an initial size.
• When this size is exceeded, the collection is automatically enlarged.
• When objects are removed, the array may be shrunk.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
ArrayList Constructors
• T he ArrayList class supports three constructors.
 ArrayList() : creates an empty array list with an initial capacity
sufficient to hold 10 elements.
 ArrayList(int capacity) : creates an array list that has the
specified initial capacity.
 ArrayList(Collection c) : creates an array list that is initialized
with the elements of the collection c.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of ArrayList
boolean add(Object o)
• Appends the specified element to the end of this list.
void add(int index, Object element)
• Inserts the specified element at the specified position index in
this list.
• Throws IndexOutOfBoundsException if the specified index is
out of range.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
boolean addAll(Collection c )
• Appends all of the elements in the specified collection to the end of
this list.
• Throws NullPointerException if the specified collection is null.
boolean addAll(int index, Collection c )
• Inserts all of the elements of the specified collection into this list,
starting at the specified position.
• Throws NullPointerException if the specified collection is null.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
void clear()
• Removes all of the elements from this list.
Object remove(int index)
• Removes the element at the specified position in this list.
Object clone()
• Returns a shallow copy of this ArrayList.
int size()
• Returns the number of elements in the list.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
boolean contains(Object o)
• Returns true if this list contains the specified element.
Object get(int index)
• Returns the element at the specified position in this list.
int indexOf(Object o)
• Returns the index in this list of the first occurrence of the
specified element, or -1 if the List does not contain the element.
int lastIndexOf(Object o)
• Returns the index in this list of the last occurrence of the
specified element, or -1.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
void ensureCapacity(int minCapacity)
• Increases the capacity of this ArrayList instance, if necessary,
to ensure that it can hold at least the number of elements
specified by the minimum capacity argument.
Object set(int index, Object element)
• Replaces the element at the specified position in this list with
the specified element.
• Throws IndexOutOfBoundsException if the specified index is
out of range (index < 0 || index >= size()).
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
void trimToSize()
• Trims the capacity of this ArrayList instance to be the list’s
current size.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
LinkedList
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
LinkedList
• LinkedList stores elements in a linked list.
• Insertion or deletion of elements at the beginning of the list.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
LinkedList Constructors
• T he LinkedList class supports two constructors.
 LinkedList() : creates an empty list with an initial capacity
sufficient to hold 10 elements.
 LinkedList(Collection c) : creates an array list that is
initialized with the elements of the collection c.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of LinkedList
• void addFirst(Object o)
• void addLast(Object o)
• Object getFirst()
• Object getLast()
• Object removeFirst()
• Object removeLast()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Difference b/w LinkedList and ArrayList
• The critical difference between them pertains to internal
implementation, which affects their performance.
• ArrayList is efficient for retrieving elements and LinkedList is
efficient for inserting and removing elements at the beginning
of the list.
• Both have the same performance for inserting and removing
elements in the middle or at the end of the list.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Vector
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Vector
• Vector is a subclass of AbstractList.
• Vector is the same as ArrayList, except that it contains
synchronized methods for accessing and modifying the vector.
• Synchronized methods can prevent data corruption when a
vector is accessed and modified by two or more threads
concurrently.
• Applications that do not require synchronization, using
ArrayList is more efficient than using Vector.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Vector Constructors
• Vector class supports following constructors.
 Vector()
 Vector(Collection c) :
 Vector(int capacity)
 Vector(int capacity, int increment): Creates a vector with the
specified initial capacity and increment.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of Vector
• void addElement (Object o)
• void elementAt(int index)
• Enumerations elements()
• void ensureCapacity(int capacity)
• Object firstElement()
• Object lastElement()
• void insertElementAt(Object o, int index)
• int capacity()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of Vector
• void copyInto(Object [])
• void removeAllElements()
• void removeElement(Object o)
• void removeElementAt(int index)
• setElementAt(Object o, int index)
• setSize(int size)
• void trimToSize()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Difference b/w Vector and ArrayList
• Vector is synchronized where as ArrayList is not.
• ArrayList supports Iterator and ListIterator whereas Vector
supports Enumeration and Iterator.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Stack
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Stack
• Vector and Stack are the legacy classes.
• There were introduced prior to Java 2.
• Stack is a subclass of Vector.
• Stack class defines the default constructor only:
Stack()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of Stack
• boolean empty()
• Object peek()
• Object pop()
• Object push(Object o)
• int search(Object o) : Returns the position of the specified
element in this stack.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
List classes

Contenu connexe

Tendances

Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structuresagorolabs
 
Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanZeeshan Khan
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and VariablesNilesh Dalvi
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objectsmaznabili
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to DatastructureNilesh Dalvi
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and IntefacesNilesh Dalvi
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Edureka!
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception HandlingNilesh Dalvi
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classesteach4uin
 

Tendances (20)

Java Collections
Java  Collections Java  Collections
Java Collections
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshan
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
 
8. String
8. String8. String
8. String
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to Datastructure
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Overview of Java
Overview of Java Overview of Java
Overview of Java
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
 
wrapper classes
wrapper classeswrapper classes
wrapper classes
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
 
Hemajava
HemajavaHemajava
Hemajava
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
 

En vedette (14)

Packages
PackagesPackages
Packages
 
Sms several papers
Sms several papersSms several papers
Sms several papers
 
Networking
NetworkingNetworking
Networking
 
Jun 2012(1)
Jun 2012(1)Jun 2012(1)
Jun 2012(1)
 
Event handling
Event handlingEvent handling
Event handling
 
Jdbc
JdbcJdbc
Jdbc
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Questions for Class I & II
Questions for Class I & IIQuestions for Class I & II
Questions for Class I & II
 
Servlets
ServletsServlets
Servlets
 
Basic IO
Basic IOBasic IO
Basic IO
 
Swing api
Swing apiSwing api
Swing api
 
Internationalization
InternationalizationInternationalization
Internationalization
 
Distributed Programming (RMI)
Distributed Programming (RMI)Distributed Programming (RMI)
Distributed Programming (RMI)
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 

Similaire à List classes

Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40bhawna sharma
 
collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptxSoniaKapoor56
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & setsRatnaJava
 
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
 
Java Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxJava Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxrangariprajwal4554
 
Array list (java platform se 8 )
Array list (java platform se 8 )Array list (java platform se 8 )
Array list (java platform se 8 )charan kumar
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructorsRavi_Kant_Sahu
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxeyemitra1
 

Similaire à List classes (20)

Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptx
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & sets
 
Java collections
Java collectionsJava collections
Java collections
 
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
 
L11 array list
L11 array listL11 array list
L11 array list
 
ArrayList class and useful methods.pptx
ArrayList class and useful methods.pptxArrayList class and useful methods.pptx
ArrayList class and useful methods.pptx
 
Collection Framework-1.pptx
Collection Framework-1.pptxCollection Framework-1.pptx
Collection Framework-1.pptx
 
Java Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxJava Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptx
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 
Advanced core java
Advanced core javaAdvanced core java
Advanced core java
 
Collections Training
Collections TrainingCollections Training
Collections Training
 
Java util
Java utilJava util
Java util
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Array list (java platform se 8 )
Array list (java platform se 8 )Array list (java platform se 8 )
Array list (java platform se 8 )
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
 

Plus de Ravi_Kant_Sahu

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaRavi_Kant_Sahu
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)Ravi_Kant_Sahu
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi_Kant_Sahu
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java Ravi_Kant_Sahu
 

Plus de Ravi_Kant_Sahu (10)

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
 
Gui programming (awt)
Gui programming (awt)Gui programming (awt)
Gui programming (awt)
 
Event handling
Event handlingEvent handling
Event handling
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Packages
PackagesPackages
Packages
 
Inheritance
InheritanceInheritance
Inheritance
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
 

Dernier

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
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
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Dernier (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
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
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
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...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

List classes

  • 1. Advanced Java Programming ArrayList, LinkedList ,Vector and Stack By Ravi Kant Sahu Asst. Professor, LPU
  • 2. ArrAyList Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 3. ArrayList • The ArrayList class extends AbstractList and implements the List interface. • Defined in java.util package. • ArrayList supports dynamic arrays that can grow as needed. • Array lists are created with an initial size. • When this size is exceeded, the collection is automatically enlarged. • When objects are removed, the array may be shrunk. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 4. ArrayList Constructors • T he ArrayList class supports three constructors.  ArrayList() : creates an empty array list with an initial capacity sufficient to hold 10 elements.  ArrayList(int capacity) : creates an array list that has the specified initial capacity.  ArrayList(Collection c) : creates an array list that is initialized with the elements of the collection c. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 5. Methods of ArrayList boolean add(Object o) • Appends the specified element to the end of this list. void add(int index, Object element) • Inserts the specified element at the specified position index in this list. • Throws IndexOutOfBoundsException if the specified index is out of range. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 6. boolean addAll(Collection c ) • Appends all of the elements in the specified collection to the end of this list. • Throws NullPointerException if the specified collection is null. boolean addAll(int index, Collection c ) • Inserts all of the elements of the specified collection into this list, starting at the specified position. • Throws NullPointerException if the specified collection is null. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 7. void clear() • Removes all of the elements from this list. Object remove(int index) • Removes the element at the specified position in this list. Object clone() • Returns a shallow copy of this ArrayList. int size() • Returns the number of elements in the list. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 8. boolean contains(Object o) • Returns true if this list contains the specified element. Object get(int index) • Returns the element at the specified position in this list. int indexOf(Object o) • Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain the element. int lastIndexOf(Object o) • Returns the index in this list of the last occurrence of the specified element, or -1. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 9. void ensureCapacity(int minCapacity) • Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Object set(int index, Object element) • Replaces the element at the specified position in this list with the specified element. • Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()). Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 10. void trimToSize() • Trims the capacity of this ArrayList instance to be the list’s current size. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 11. LinkedList Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 12. LinkedList • LinkedList stores elements in a linked list. • Insertion or deletion of elements at the beginning of the list. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 13. LinkedList Constructors • T he LinkedList class supports two constructors.  LinkedList() : creates an empty list with an initial capacity sufficient to hold 10 elements.  LinkedList(Collection c) : creates an array list that is initialized with the elements of the collection c. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 14. Methods of LinkedList • void addFirst(Object o) • void addLast(Object o) • Object getFirst() • Object getLast() • Object removeFirst() • Object removeLast() Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 15. Difference b/w LinkedList and ArrayList • The critical difference between them pertains to internal implementation, which affects their performance. • ArrayList is efficient for retrieving elements and LinkedList is efficient for inserting and removing elements at the beginning of the list. • Both have the same performance for inserting and removing elements in the middle or at the end of the list. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 16. Vector Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 17. Vector • Vector is a subclass of AbstractList. • Vector is the same as ArrayList, except that it contains synchronized methods for accessing and modifying the vector. • Synchronized methods can prevent data corruption when a vector is accessed and modified by two or more threads concurrently. • Applications that do not require synchronization, using ArrayList is more efficient than using Vector. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 18. Vector Constructors • Vector class supports following constructors.  Vector()  Vector(Collection c) :  Vector(int capacity)  Vector(int capacity, int increment): Creates a vector with the specified initial capacity and increment. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 19. Methods of Vector • void addElement (Object o) • void elementAt(int index) • Enumerations elements() • void ensureCapacity(int capacity) • Object firstElement() • Object lastElement() • void insertElementAt(Object o, int index) • int capacity() Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 20. Methods of Vector • void copyInto(Object []) • void removeAllElements() • void removeElement(Object o) • void removeElementAt(int index) • setElementAt(Object o, int index) • setSize(int size) • void trimToSize() Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 21. Difference b/w Vector and ArrayList • Vector is synchronized where as ArrayList is not. • ArrayList supports Iterator and ListIterator whereas Vector supports Enumeration and Iterator. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 22. Stack Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 23. Stack • Vector and Stack are the legacy classes. • There were introduced prior to Java 2. • Stack is a subclass of Vector. • Stack class defines the default constructor only: Stack() Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 24. Methods of Stack • boolean empty() • Object peek() • Object pop() • Object push(Object o) • int search(Object o) : Returns the position of the specified element in this stack. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)