INF210 Lec4 Data Structures
INF210 Lec4 Data Structures
INF 210
Fall 2022-2023
Lecture 4
The Arraylist
INSTRUCTOR
DR/ AYMAN SOLIMAN & DR/ OSAMA FAROUK
Ø Contents
1) Introduction
2) Structure of the ArrayList
3) Initializing an ArrayList
4) ArrayList Functionality
5) ArrayList Methods
6) ArrayList as a Data Structure
7) Comparing and Contrasting with Arrays
8) Conclusion
10/26/22 Dr/ Ayman Soliman 2
q The ArrayList - Introduction
Ø The arrayList, fundamentally, can be thought of as a growing array
Nums 4 8 24 6
Nums 4 8 24 6
Nums 4 8 24 6
Nums 4 8 24 6 9
Array
Behind the Scenes
ArrayList Similar Functionality
Python Lists
ArrayLists
Object-Oriented
Class Hierarchy
Programming
Initialization of Arraylist
Name of Arraylist
Size = 50
Size = 10
Size = 3
Size = 3
o Beefier أﺿﺨﻢ
o More Functionality
o More Powerful
o Beefier
o More Functionality
o More Powerful
Add
Access
Change Delete
Java
C#
Remember, all of these are preprogrammed for you and so you don’t have to make
them all by hand, all you must do is call them on a premade Arraylist.
10/26/22 Dr/ Ayman Soliman 27
q The ArrayList - ArrayList Methods
Ø Because of the variability surrounding the arrayList, we will only be covering
6 common methods
ArrayList
ArrayList exampleAList
Index 0 1 2 3
ExampleAList
Index 0 1 2 3
ExampleAList
Add(Object)
Index 0 1 2 3
ExampleAList
Add(Object)
Index 0 1 2 3
ExampleAList
Add(Object)
Index 0 1 2 3
ExampleAList
Add(Object)
Index 0 1 2 3
ExampleAList
Add(Object)
Index 0 1 2 3
ExampleAList
Add(Object) Autoboxing
Appends the element you pass in as an
argument to the end of the arrayList exampleAList.add(2);
Index 0 1 2 3
ExampleAList
Add(Object)
Index 0 1 2 3
ExampleAList
Add(Object)
Index 0 1 2 3
ExampleAList 2
Add(Object)
Index 0 1 2 3
ExampleAList 2
Add(Object)
Index 0 1 2 3
ExampleAList 2
Add(Object)
Index 0 1 2 3
ExampleAList 2 5
Add(Object)
Index 0 1 2 3
ExampleAList 2 5
Index 0 1 2 3
ExampleAList 2 5
Add(Object, Index)
● Appends the element you pass
in as an argument at the index
you pass in
Index 0 1 2 3
ExampleAList 2 5
Add(Object, Index)
● Appends the element you pass
exampleAList.add(1,0); in as an argument at the index
you pass in
Index 0 1 2 3
ExampleAList 2 5
Add(Object, Index)
● Appends the element you pass
exampleAList.add(1,0); in as an argument at the index
you pass in
Index 0 1 2 3
ExampleAList 2 5
Add(Object, Index)
● Appends the element you pass
exampleAList.add(1,0); in as an argument at the index
you pass in
Index 0 1 2 3
ExampleAList 1 2 5
Add(Object, Index)
● Appends the element you pass
exampleAList.add(3,2); in as an argument at the index
you pass in
Index 0 1 2 3
ExampleAList 1 2 5
Add(Object, Index)
● Appends the element you pass
exampleAList.add(3,2); in as an argument at the index
you pass in
Index 0 1 2 3
ExampleAList 1 2 5
Add(Object, Index)
● Appends the element you pass
exampleAList.add(3,2); in as an argument at the index
you pass in
Index 0 1 2 3
ExampleAList 1 2 3 5
Add(Object, Index)
Integer
● Appends the element you pass
exampleAList.add(3,2); in as an argument at the index
you pass in
Index 0 1 2 3
ExampleAList 1 2 3 5
Add(Object, Index)
Integer
● Appends the element you pass
exampleAList.add(3,2); in as an argument at the index
you pass in
Index
Index 0 1 2 3
ExampleAList 1 2 3 5
Index 0 1 2 3
ExampleAList 1 2 3 5
Remove(Index) Remove(Object)
Removes the Object at the index Removes the first instance of the
you provide object passed into the arrayList
Index 0 1 2 3
ExampleAList 1 2 3 5
Remove(Index) Remove(Object)
Removes the Object at the index Removes the first instance of the
you provide object passed into the arrayList
If we want to remove the number five from our Arraylist, there are two methods
Index 0 1 2 3
ExampleAList 1 2 3 5
Remove(Index) Remove(Object)
Removes the Object at the index Removes the first instance of the
you provide object passed into the arrayList
Index 0 1 2 3
ExampleAList 1 2 3 5
Remove(Index)
Index 0 1 2 3
ExampleAList 1 2 3 5
Remove(Index)
Index 0 1 2 3
ExampleAList 1 2 3 5
Remove(Index)
Index 0 1 2 3
ExampleAList 1 2 3 5
Remove(Index)
Index 0 1 2 3
ExampleAList 1 2 3
Remove(Index)
Index 0 1 2 3
ExampleAList 1 2 3
Index 0 1 2 3
ExampleAList 1 2 3 5
Remove(Object)
Index 0 1 2 3
ExampleAList 1 2 3 5
Remove(Object)
Means that we want to remove the first instance of the number five in Arraylist
Index 0 1 2 3
ExampleAList 1 2 3 5
Remove(Object)
Index 0 1 2 3
ExampleAList 1 2 3 5
Remove(Object)
Index 0 1 2 3
ExampleAList 1 2 3 5
Remove(Object)
When running this will return true and remove the integer from our list
True
Index 0 1 2 3
ExampleAList 1 2 3
Index 0 1 2 3
ExampleAList 1 2 3
Remove(Object)
Index 0 1 2 3
ExampleAList 1 2 3 5
Index 0 1 2 3
ExampleAList 1 2 3 5
Get(Index)
Index 0 1 2 3
ExampleAList 1 2 3 5
Get(Index)
Index 0 1 2 3
ExampleAList 1 2 3 5
Get(Index)
Index 0 1 2 3
ExampleAList 1 2 3 5
Get(Index)
Index 0 1 2 3
ExampleAList 1 2 3 5
Get(Index)
Index 0 1 2 3
ExampleAList 1 2 3 5
Set(Index, Object)
Index 0 1 2 3
ExampleAList 1 2 3 5
Set(Index, Object)
Index 0 1 2 3
ExampleAList 1 2 3 5
Set(Index, Object)
Index 0 1 2 3
ExampleAList 1 2 3 5
Set(Index, Object)
Index 0 1 2 3
ExampleAList 1 2 3 5
Set(Index, Object)
Index 0 1 2 3
ExampleAList 1 2 3 5
Set(Index, Object)
Index 0 1 2 3
ExampleAList 1 2 3 5
Set(Index, Object)
Index 0 1 2 3
ExampleAList 1 2 3 5
Set(Index, Object)
Index 0 1 2 3
ExampleAList 1 2 3 5
Set(Index, Object)
Index 0 1 2 3
ExampleAList 1 2 3 4
Set(Index, Object)
Index 0 1 2 3
ExampleAList 1 2 3 4
Index 0 1 2 3
ExampleAList 1 2 3 4
Clear()
Index 0 1 2 3
ExampleAList 1 2 3 4
Clear()
Index 0 1 2 3
ExampleAList 1 2 3 4
Clear()
Index 0 1 2 3
ExampleAList
Index 0 1 2 3
ExampleAList 1 2 3 4
toArray()
Index 0 1 2 3
ExampleAList 1 2 3 4
toArray()
Index 0 1 2 3
ExampleAList 1 2 3 4
toArray()
Index 0 1 2 3
ExampleAList 1 2 3 4
O(1)
O(1)
O(1)
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
Memory
Index 0 1 2 3
Example 87 91 100 42
O(1)
O(1) O(n)
ArrayLists
A dynamically increasing array