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

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

  • 1.
  • 2.
    • An ArrayListclass 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 toUse 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 ArrayListcreates 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 inArrayList 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 usegenerics 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 ofgenerics 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