SlideShare a Scribd company logo
OOP(Object-Oriented Programming)
Lecture 8 (ArrayList)
• An ArrayList class is a resizable array implementation in Java
provided by the java.util package. Unlike arrays, which have a
fixed size, an ArrayList can grow and shrink dynamically as
elements are added or removed.
• Key Characteristics:
- Dynamic sizing: Automatically resizes when capacity is exceeded.
- Indexed access: Elements can be accessed using an index, just
like arrays.
- Allows duplicates: Elements can be repeated.
- Preserves insertion order.
ArrayList
• When to Use ArrayList?
- when you need fast access by index.
- when you don't know the number of elements in advance.
- when you want to frequently add/remove elements at the end.
• How does ArrayList work internally?
ArrayList uses an internal array of type Object[] to store data.
When elements are added and the capacity is exceeded, it
automatically creates a larger array and copies the old data into it.
ArrayList
• How ArrayList creates a larger array?
- It use the Bitwise right shift operators >>
- ArrayList has tow variable size(number of elements) and
capacity(length of ArrayList).
- If there is an ArrayList with size=10, and we want to add the
element number11, then:
- The size=11
- The oldcapacity=oldsize=10
- The newcapacity= oldcapacity/2
- The capacity=oldcapacity + newcapacity
ArrayList
• List is :
- An interface in Java; you cannot instantiate it
directly.
- Provides a contract for operations such as
add, remove, get, size.
- Implemented by two common classes:
ArrayList and LinkedList.
ArrayList
ThehierarchyofArrayListclass
• Elements in ArrayList are actually objects.
• ArrayList can not be used for primitive types, like int, float,
double, char, etc.
• To use primitive types you must specify an equivalent wrapper
class, as Integer, Float, Double, Boolean, Character, etc.
• ArrayList is generic class with a type parameter.
• Declaration of ArrayList Objects:
ArrayList list = new ArrayList();
ArrayList<Car> list = new ArrayList();  Car is class
ArrayList<Integer> list = new ArrayList<>();
ArrayList<String> list = new ArrayList<String>();
ArrayList
• We use generics with ArrayList to achieve type safety.
• This prevents runtime errors and makes the code easier to
understand and maintain.
• Without generic:
ArrayList list = new ArrayList();
list.add("Hello");
list.add(123); // Logical error, but no compile-time warning
String str = (String) list.get(1); // Leads to ClassCastException at runtime
• With generic:
ArrayList<String> list = new ArrayList<>();
list.add("Hello");
list.add(123); // Compile-time error
String str = list.get(0); // No need for casting
ArrayList
• Benefit of generics in ArrayList:
- Prevents inserting incompatible types.
- No need for casting when retrieving elements.
- Errors are caught at compile time instead of runtime, making the
program more stable.
• Note:
Main drawback of ArrayList: Inserting or deleting from the
beginning or middle is slower because elements need to be shifted.
ArrayList

More Related Content

Similar to Lecture 8_٠٨٣٣٣٦taiz unvercity object oreinted programming.pdf (20)

PPTX
ArrayList in JAVA
SAGARDAVE29
 
PPT
12_-_Collections_Framework
Krishna Sujeer
 
PPT
A2003822018_21789_17_2018_09. ArrayList.ppt
RithwikRanjan
 
PPTX
VTUOOPMCA5THMODULECollection OverV .pptx
VeenaNaik23
 
PPTX
mca5thCollection OverViCollection O.pptx
VeenaNaik23
 
PPTX
VTUOOPMCA5THMODULEvCollection OverV.pptx
VeenaNaik23
 
PPTX
VTUOOPMCA5THMODULECollection OverVi.pptx
VeenaNaik23
 
PPT
Java10 Collections and Information
SoftNutx
 
PDF
5 collection framework
Minal Maniar
 
PPTX
arraylistinjava.pptx
dintakurthigayathri9
 
PPT
STRINGS IN JAVA
LOVELY PROFESSIONAL UNIVERSITY
 
PPT
Java collection
Arati Gadgil
 
PPSX
Collections - Array List
Hitesh-Java
 
PPTX
ArrayList class and useful methods.pptx
Abid523408
 
PPTX
Session 15 - Collections - Array List
PawanMM
 
PPTX
List interface in collections framework
Ravi Chythanya
 
PDF
java unit 4 pdf - about java collections
aapalaks
 
DOCX
ArrayList.docx
veerendranath12
 
PDF
Java ArrayList Tutorial | Edureka
Edureka!
 
DOC
Advanced core java
Rajeev Uppala
 
ArrayList in JAVA
SAGARDAVE29
 
12_-_Collections_Framework
Krishna Sujeer
 
A2003822018_21789_17_2018_09. ArrayList.ppt
RithwikRanjan
 
VTUOOPMCA5THMODULECollection OverV .pptx
VeenaNaik23
 
mca5thCollection OverViCollection O.pptx
VeenaNaik23
 
VTUOOPMCA5THMODULEvCollection OverV.pptx
VeenaNaik23
 
VTUOOPMCA5THMODULECollection OverVi.pptx
VeenaNaik23
 
Java10 Collections and Information
SoftNutx
 
5 collection framework
Minal Maniar
 
arraylistinjava.pptx
dintakurthigayathri9
 
Java collection
Arati Gadgil
 
Collections - Array List
Hitesh-Java
 
ArrayList class and useful methods.pptx
Abid523408
 
Session 15 - Collections - Array List
PawanMM
 
List interface in collections framework
Ravi Chythanya
 
java unit 4 pdf - about java collections
aapalaks
 
ArrayList.docx
veerendranath12
 
Java ArrayList Tutorial | Edureka
Edureka!
 
Advanced core java
Rajeev Uppala
 

More from nabeehmohammedtaher (8)

PPTX
7 (1) the motor was very good Lock and complete.pptx
nabeehmohammedtaher
 
PPT
Lcdf3_chap_09 has been the subject for.ppt
nabeehmohammedtaher
 
PPT
10-Circuit-Packet and completed the .ppt
nabeehmohammedtaher
 
PDF
oop lecture5_٠٤٤٦٥٣.enArab you have r.pdf
nabeehmohammedtaher
 
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
PDF
‏‏‏‏‏‏oop lecture 6_١٢٥٩٤٧taiz univercity.pdf
nabeehmohammedtaher
 
PDF
‏‏oop lecture 2 taiz univercity object programming.pdf
nabeehmohammedtaher
 
PDF
lecture.in ooop object orented programmeg.pdf
nabeehmohammedtaher
 
7 (1) the motor was very good Lock and complete.pptx
nabeehmohammedtaher
 
Lcdf3_chap_09 has been the subject for.ppt
nabeehmohammedtaher
 
10-Circuit-Packet and completed the .ppt
nabeehmohammedtaher
 
oop lecture5_٠٤٤٦٥٣.enArab you have r.pdf
nabeehmohammedtaher
 
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
‏‏‏‏‏‏oop lecture 6_١٢٥٩٤٧taiz univercity.pdf
nabeehmohammedtaher
 
‏‏oop lecture 2 taiz univercity object programming.pdf
nabeehmohammedtaher
 
lecture.in ooop object orented programmeg.pdf
nabeehmohammedtaher
 
Ad

Recently uploaded (20)

PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Ad

Lecture 8_٠٨٣٣٣٦taiz unvercity object oreinted programming.pdf

  • 2. • An ArrayList class is a resizable array implementation in Java provided by the java.util package. Unlike arrays, which have a fixed size, an ArrayList can grow and shrink dynamically as elements are added or removed. • Key Characteristics: - Dynamic sizing: Automatically resizes when capacity is exceeded. - Indexed access: Elements can be accessed using an index, just like arrays. - Allows duplicates: Elements can be repeated. - Preserves insertion order. ArrayList
  • 3. • When to Use ArrayList? - when you need fast access by index. - when you don't know the number of elements in advance. - when you want to frequently add/remove elements at the end. • How does ArrayList work internally? ArrayList uses an internal array of type Object[] to store data. When elements are added and the capacity is exceeded, it automatically creates a larger array and copies the old data into it. ArrayList
  • 4. • How ArrayList creates a larger array? - It use the Bitwise right shift operators >> - ArrayList has tow variable size(number of elements) and capacity(length of ArrayList). - If there is an ArrayList with size=10, and we want to add the element number11, then: - The size=11 - The oldcapacity=oldsize=10 - The newcapacity= oldcapacity/2 - The capacity=oldcapacity + newcapacity ArrayList
  • 5. • List is : - An interface in Java; you cannot instantiate it directly. - Provides a contract for operations such as add, remove, get, size. - Implemented by two common classes: ArrayList and LinkedList. ArrayList ThehierarchyofArrayListclass
  • 6. • Elements in ArrayList are actually objects. • ArrayList can not be used for primitive types, like int, float, double, char, etc. • To use primitive types you must specify an equivalent wrapper class, as Integer, Float, Double, Boolean, Character, etc. • ArrayList is generic class with a type parameter. • Declaration of ArrayList Objects: ArrayList list = new ArrayList(); ArrayList<Car> list = new ArrayList(); Car is class ArrayList<Integer> list = new ArrayList<>(); ArrayList<String> list = new ArrayList<String>(); ArrayList
  • 7. • We use generics with ArrayList to achieve type safety. • This prevents runtime errors and makes the code easier to understand and maintain. • Without generic: ArrayList list = new ArrayList(); list.add("Hello"); list.add(123); // Logical error, but no compile-time warning String str = (String) list.get(1); // Leads to ClassCastException at runtime • With generic: ArrayList<String> list = new ArrayList<>(); list.add("Hello"); list.add(123); // Compile-time error String str = list.get(0); // No need for casting ArrayList
  • 8. • Benefit of generics in ArrayList: - Prevents inserting incompatible types. - No need for casting when retrieving elements. - Errors are caught at compile time instead of runtime, making the program more stable. • Note: Main drawback of ArrayList: Inserting or deleting from the beginning or middle is slower because elements need to be shifted. ArrayList