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

PPTX
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
PPTX
arraylist in java a comparison of the array and arraylist
PPTX
Array vs array list
PDF
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
PPTX
collection framework.pptx
PPTX
ArrayList in JAVA
PPTX
ArrayList in JAVA
PPT
12_-_Collections_Framework
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
arraylist in java a comparison of the array and arraylist
Array vs array list
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
collection framework.pptx
ArrayList in JAVA
ArrayList in JAVA
12_-_Collections_Framework

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

PPTX
Java Collections Framework - Interfaces, Classes and Algorithms
PPTX
Collections framework in java
PPTX
Arrays.pptx
PPTX
Lecture 9
PPTX
VTUOOPMCA5THMODULECollection OverV .pptx
PPTX
mca5thCollection OverViCollection O.pptx
PPTX
VTUOOPMCA5THMODULECollection OverVi.pptx
PPTX
VTUOOPMCA5THMODULEvCollection OverV.pptx
PPTX
collectionsframework210616084411 (1).pptx
PPTX
Data structures in c#
PPTX
Collections - Lists & sets
PDF
5 collection framework
PPTX
Collections lecture 35 40
PDF
Collection framework (completenotes) zeeshan
PPTX
Array in C# 3.5
PPT
Md08 collection api
PPTX
ARRAYS.pptx
PPTX
Unit 1 array based implementation
PPTX
Advanced Java - UNIT 3.pptx
Java Collections Framework - Interfaces, Classes and Algorithms
Collections framework in java
Arrays.pptx
Lecture 9
VTUOOPMCA5THMODULECollection OverV .pptx
mca5thCollection OverViCollection O.pptx
VTUOOPMCA5THMODULECollection OverVi.pptx
VTUOOPMCA5THMODULEvCollection OverV.pptx
collectionsframework210616084411 (1).pptx
Data structures in c#
Collections - Lists & sets
5 collection framework
Collections lecture 35 40
Collection framework (completenotes) zeeshan
Array in C# 3.5
Md08 collection api
ARRAYS.pptx
Unit 1 array based implementation
Advanced Java - UNIT 3.pptx
Ad

More from nabeehmohammedtaher (8)

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

Recently uploaded (20)

PPTX
L1 - Introduction to python Backend.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Nekopoi APK 2025 free lastest update
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
ai tools demonstartion for schools and inter college
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
AI in Product Development-omnex systems
PPTX
CHAPTER 2 - PM Management and IT Context
L1 - Introduction to python Backend.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PTS Company Brochure 2025 (1).pdf.......
Odoo Companies in India – Driving Business Transformation.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Odoo POS Development Services by CandidRoot Solutions
Nekopoi APK 2025 free lastest update
Design an Analysis of Algorithms II-SECS-1021-03
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
ai tools demonstartion for schools and inter college
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Which alternative to Crystal Reports is best for small or large businesses.pdf
Understanding Forklifts - TECH EHS Solution
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
AI in Product Development-omnex systems
CHAPTER 2 - PM Management and IT Context

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