SlideShare uma empresa Scribd logo
1 de 25
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Destaque (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
 

Semelhante a 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
 

Semelhante a 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
 

Mais 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
 

Mais 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
 

Último

Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
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
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
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
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
🐬 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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 

Último (20)

Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
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...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
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
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

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)