0% found this document useful (0 votes)
13 views127 pages

INF210 Lec4 Data Structures

The document discusses the ArrayList data structure. It provides an introduction to ArrayLists, describing how they are similar to but more dynamic than arrays. It covers initializing and defining the size of an ArrayList, and compares the functionality of ArrayLists to arrays, noting ArrayLists can grow and support adding, accessing, changing and deleting elements.

Uploaded by

Nader Yasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views127 pages

INF210 Lec4 Data Structures

The document discusses the ArrayList data structure. It provides an introduction to ArrayLists, describing how they are similar to but more dynamic than arrays. It covers initializing and defining the size of an ArrayList, and compares the functionality of ArrayLists to arrays, noting ArrayLists can grow and support adding, accessing, changing and deleting elements.

Uploaded by

Nader Yasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 127

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

Array Size is Final

10/26/22 Dr/ Ayman Soliman 3


q The ArrayList - Introduction
Ø The arrayList, fundamentally, can be thought of as a growing array

Nums 4 8 24 6

Arraylist Size is Dynamic

10/26/22 Dr/ Ayman Soliman 4


q The ArrayList - Introduction
Ø The arrayList, fundamentally, can be thought of as a growing array

Nums 4 8 24 6

Arraylist Size is Dynamic

Nums 4 8 24 6 9

10/26/22 Dr/ Ayman Soliman 5


q The ArrayList - Introduction
e
Ø The arrayList, fundamentally, can be thought of as a growing array
s
S u
A Y
8 LW 24s? 6
Nums 4
t A i st
n o yL
y
h a rra
WArray Size is Dynamic
Nums 4 8 24 6 9

10/26/22 Dr/ Ayman Soliman 6


q The ArrayList - Structure of the ArrayList
Ø An arrayList is backed by an array

o This makes the arrayList have a lot of similar functionality to an array

Array
Behind the Scenes
ArrayList Similar Functionality

10/26/22 Dr/ Ayman Soliman 7


q The ArrayList - Initializing an ArrayList
Ø Initializing an arrayList varies based on the language

o Shown below in Java and C#

ArrayList<Integer> arrayList = new ArrayList<Integer>();

ArrayList arrayList = new ArrayList();

10/26/22 Dr/ Ayman Soliman 8


q The ArrayList - Initializing an ArrayList
Ø Arrays and arrayLists are frankensteined together into a single data structure
called Lists

o Take some functionality from both data structures

Python Lists

ArrayLists

10/26/22 Dr/ Ayman Soliman 9


q The ArrayList - Initializing an ArrayList
Ø Initializing an arrayList varies based on the language

o Shown below in Java and C#

Object-Oriented
Class Hierarchy
Programming

ArrayList<Integer> arrayList = new ArrayList<Integer>();

ArrayList arrayList = new ArrayList();

10/26/22 Dr/ Ayman Soliman 10


q The ArrayList - Initializing an ArrayList
Ø Initializing an arrayList varies based on the language

o Shown below in Java and C#

ArrayList<Integer> arrayList = new ArrayList<Integer>();

ArrayList arrayList = new ArrayList();

10/26/22 Dr/ Ayman Soliman 11


q The ArrayList - Initializing an ArrayList
Ø Initializing an arrayList varies based on the language

o Shown below in Java and C#

ArrayList<Integer> arrayList = new ArrayList<Integer>();

ArrayList arrayList = new ArrayList();

Initialization of Arraylist

10/26/22 Dr/ Ayman Soliman 12


q The ArrayList - Initializing an ArrayList
Ø Initializing an arrayList varies based on the language

o Shown below in Java and C#

ArrayList<Integer> arrayList = new ArrayList<Integer>();

ArrayList arrayList = new ArrayList();

10/26/22 Dr/ Ayman Soliman 13


q The ArrayList - Initializing an ArrayList
Ø Initializing an arrayList varies based on the language

o Shown below in Java and C#

ArrayList<Integer> arrayList = new ArrayList<Integer>();

ArrayList arrayList = new ArrayList();

Name of Arraylist

10/26/22 Dr/ Ayman Soliman 14


q The ArrayList - Initializing an ArrayList
Ø Initializing an arrayList varies based on the language

o Shown below in Java and C#

ArrayList<Integer> arrayList = new ArrayList<Integer>();

ArrayList arrayList = new ArrayList();

10/26/22 Dr/ Ayman Soliman 15


q The ArrayList - Initializing an ArrayList
Ø Initializing an arrayList varies based on the language

o Shown below in Java and C#

ArrayList<Integer> arrayList = new ArrayList<Integer>();

ArrayList arrayList = new ArrayList();

10/26/22 Dr/ Ayman Soliman 16


q The ArrayList - Initializing an ArrayList
Ø Initializing an arrayList varies based on the language

o Shown below in Java and C#

Size = 50

ArrayList<Integer> arrayList = new ArrayList<Integer>(50);

ArrayList arrayList = new ArrayList(50);

You can define the Arraylist size

10/26/22 Dr/ Ayman Soliman 17


q The ArrayList - Initializing an ArrayList
Ø Initializing an arrayList varies based on the language

o Shown below in Java and C#

Size = 10

ArrayList<Integer> arrayList = new ArrayList<Integer>();

ArrayList arrayList = new ArrayList();

Leaving the parentheses blank, will automatically define size of 10

10/26/22 Dr/ Ayman Soliman 18


q The ArrayList - Initializing an ArrayList
Ø Initializing an arrayList varies based on the language

o Shown below in Java and C#

Size = 3

ArrayList<Integer> arrayList = new ArrayList<Integer>(1,2,3);

ArrayList arrayList = new ArrayList(1,2,3);

Arraylist don’t support this type of declaration


10/26/22 Dr/ Ayman Soliman 19
q The ArrayList - Initializing an ArrayList
Ø Initializing an arrayList varies based on the language

o Shown below in Java and C#

Size = 3

ArrayList<Integer> arrayList = new ArrayList<Integer>(1,2,3);

ArrayList arrayList = new ArrayList(1,2,3);

10/26/22 Dr/ Ayman Soliman 20


q The ArrayList - ArrayList Functionality
Ø An arrayList can be thought of as an evolved array (‫)ﻣﺼﻔﻮﻓﮫ ﻣﺘﻄﻮره‬

o Beefier ‫أﺿﺨﻢ‬

o More Functionality

o More Powerful

10/26/22 Dr/ Ayman Soliman 21


q The ArrayList - ArrayList Functionality
Ø An arrayList can be thought of as an evolved array

o Beefier

o More Functionality

o More Powerful

“Who are you?” “I’m you, but Stronger”

10/26/22 Dr/ Ayman Soliman 22


q The ArrayList - ArrayList Functionality
Ø This is mainly because it belongs to the pre-built arrayList “class”

o Means it has pre-built functions that are at our disposal

Add
Access
Change Delete

10/26/22 Dr/ Ayman Soliman 23


q The ArrayList - ArrayList Functionality
Ø The type of functionality you’re going to get is going to vary based on
language

Java

10/26/22 Dr/ Ayman Soliman 24


q The ArrayList - ArrayList Functionality
Ø The type of functionality you’re going to get is going to vary based on
language

C#

10/26/22 Dr/ Ayman Soliman 25


q The ArrayList - ArrayList Methods
Ø Because of the variability surrounding the arrayList, we will only be covering
6 common methods

Add Method Set Method

Remove Method Clear Method

Get Method toArray Method

10/26/22 Dr/ Ayman Soliman 26


q The ArrayList - ArrayList Methods
Ø Because of the variability surrounding the arrayList, we will only be covering
6 common methods

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

Add Method Set Method

Remove Method Clear Method

Get Method toArray Method

10/26/22 Dr/ Ayman Soliman 28


q The ArrayList - ArrayList Methods

ArrayList

10/26/22 Dr/ Ayman Soliman 29


q The ArrayList - ArrayList Methods

ArrayList exampleAList

10/26/22 Dr/ Ayman Soliman 30


q The ArrayList - ArrayList Methods

ArrayList exampleAList = new ArrayList();

10/26/22 Dr/ Ayman Soliman 31


q The ArrayList - ArrayList Methods

ArrayList exampleAList = new ArrayList(4);

10/26/22 Dr/ Ayman Soliman 32


q The ArrayList - ArrayList Methods

ArrayList exampleAList = new ArrayList(4);

Index 0 1 2 3
ExampleAList

10/26/22 Dr/ Ayman Soliman 33


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object) Add(Object, Index)

Index 0 1 2 3
ExampleAList

10/26/22 Dr/ Ayman Soliman 34


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object)

Appends the element you pass in as an


argument to the end of the arrayList exampleAList.add(2);

Index 0 1 2 3
ExampleAList

10/26/22 Dr/ Ayman Soliman 35


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object)

Appends the element you pass in as an


argument to the end of the arrayList exampleAList.add(2);

Index 0 1 2 3
ExampleAList

10/26/22 Dr/ Ayman Soliman 36


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object)

Appends the element you pass in as an


argument to the end of the arrayList exampleAList.add(2);

Index 0 1 2 3
ExampleAList

10/26/22 Dr/ Ayman Soliman 37


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object)

Appends the element you pass in as an


argument to the end of the arrayList exampleAList.add(2);

Index 0 1 2 3
ExampleAList

10/26/22 Dr/ Ayman Soliman 38


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object)

Appends the element you pass in as an


argument to the end of the arrayList exampleAList.add(2);

2 Integer Object (2)

Index 0 1 2 3
ExampleAList

10/26/22 Dr/ Ayman Soliman 39


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object) Autoboxing
Appends the element you pass in as an
argument to the end of the arrayList exampleAList.add(2);

2 Integer Object (2)

Index 0 1 2 3
ExampleAList

10/26/22 Dr/ Ayman Soliman 40


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object)

Appends the element you pass in as an


argument to the end of the arrayList exampleAList.add(2);

Index 0 1 2 3
ExampleAList

10/26/22 Dr/ Ayman Soliman 41


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object)

Appends the element you pass in as an


argument to the end of the arrayList exampleAList.add(2);

Index 0 1 2 3
ExampleAList 2

10/26/22 Dr/ Ayman Soliman 42


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object)

Appends the element you pass in as an


argument to the end of the arrayList exampleAList.add(2);

Index 0 1 2 3
ExampleAList 2

10/26/22 Dr/ Ayman Soliman 43


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object)

Appends the element you pass in as an


argument to the end of the arrayList exampleAList.add(5);

Index 0 1 2 3
ExampleAList 2

10/26/22 Dr/ Ayman Soliman 44


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object)

Appends the element you pass in as an


argument to the end of the arrayList exampleAList.add(5);

Index 0 1 2 3
ExampleAList 2 5

10/26/22 Dr/ Ayman Soliman 45


q The ArrayList - Add Method
Ø The add method has 2 different types...

Add(Object)

Appends the element you pass in as an


argument to the end of the arrayList exampleAList.add(5);

Index 0 1 2 3
ExampleAList 2 5

10/26/22 Dr/ Ayman Soliman 46


q The ArrayList - Add Method
Ø The add method has 2 different types...

Index 0 1 2 3
ExampleAList 2 5

10/26/22 Dr/ Ayman Soliman 47


q The ArrayList - Add Method
Ø The add method has 2 different types...

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

10/26/22 Dr/ Ayman Soliman 48


q The ArrayList - Add Method
Ø The add method has 2 different types...

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

10/26/22 Dr/ Ayman Soliman 49


q The ArrayList - Add Method
Ø The add method has 2 different types...

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

10/26/22 Dr/ Ayman Soliman 50


q The ArrayList - Add Method
Ø The add method has 2 different types...

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

10/26/22 Dr/ Ayman Soliman 51


q The ArrayList - Add Method
Ø The add method has 2 different types...

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

10/26/22 Dr/ Ayman Soliman 52


q The ArrayList - Add Method
Ø The add method has 2 different types...

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

10/26/22 Dr/ Ayman Soliman 53


q The ArrayList - Add Method
Ø The add method has 2 different types...

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

10/26/22 Dr/ Ayman Soliman 54


q The ArrayList - Add Method
Ø The add method has 2 different types...

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

10/26/22 Dr/ Ayman Soliman 55


q The ArrayList - Add Method
Ø The add method has 2 different types...

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

10/26/22 Dr/ Ayman Soliman 56


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 57


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

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

10/26/22 Dr/ Ayman Soliman 58


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

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

10/26/22 Dr/ Ayman Soliman 59


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

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

10/26/22 Dr/ Ayman Soliman 60


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Remove(Index)

Removes the Object at the index


you provide

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 61


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Remove(Index)

Removes the Object at the index


you provide exampleAList.remove(3);

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 62


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Remove(Index)

Removes the Object at the index


you provide exampleAList.remove(3);

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 63


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Remove(Index)

Removes the Object at the index


you provide exampleAList.remove(3);

Index 0 1 2 3
ExampleAList 1 2 3

10/26/22 Dr/ Ayman Soliman 64


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Remove(Index)

Removes the Object at the index


you provide exampleAList.remove(3);

Index 0 1 2 3
ExampleAList 1 2 3

10/26/22 Dr/ Ayman Soliman 65


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 66


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Remove(Object)

Removes the first instance of the


object passed into the arrayList

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 67


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Remove(Object)

Removes the first instance of the


exampleAList.remove(new Integer(5)); object passed into the arrayList

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

10/26/22 Dr/ Ayman Soliman 68


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Remove(Object)

Removes the first instance of the


exampleAList.remove(new Integer(5)); object passed into the arrayList

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 69


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Remove(Object)

Removes the first instance of the


exampleAList.remove(new Integer(5)); object passed into the arrayList

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 70


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Remove(Object)

Removes the first instance of the


exampleAList.remove(new Integer(5)); object passed into the arrayList

When running this will return true and remove the integer from our list
True

Index 0 1 2 3
ExampleAList 1 2 3

10/26/22 Dr/ Ayman Soliman 71


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

If the number, you want to delete not found Remove(Object)

Removes the first instance of the


exampleAList.remove(new Integer(5)); object passed into the arrayList

When running this will return false


False

Index 0 1 2 3
ExampleAList 1 2 3

10/26/22 Dr/ Ayman Soliman 72


q The ArrayList - Remove Method
Ø The Remove method has 2 different types...

Remove(Object)

Removes the first instance of the


exampleAList.remove(new Integer(5)); object passed into the arrayList

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 73


q The ArrayList - Get Method
Ø The get method is the same as referencing an index for an array

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 74


q The ArrayList - Get Method
Ø The get method is the same as referencing an index for an array

Get(Index)

Returns the object contained at the


given index

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 75


q The ArrayList - Get Method
Ø The get method is the same as referencing an index for an array

Get(Index)

Returns the object contained at the


given index exampleAList.get(0);

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 76


q The ArrayList - Get Method
Ø The get method is the same as referencing an index for an array

Get(Index)

Returns the object contained at the


given index exampleAList.get(0);

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 77


q The ArrayList - Get Method
Ø The get method is the same as referencing an index for an array

Get(Index)

Returns the object contained at the


given index exampleAList.get(2);

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 78


q The ArrayList - Get Method
Ø The get method is the same as referencing an index for an array

Get(Index)

Returns the object contained at the


given index exampleAList.get(2);

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 79


q The ArrayList - Set Method
Ø The set method is how we replace elements within the arrayList

Set(Index, Object)

Sets the element at the index which you


passed in, to the object you also passed in

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 80


q The ArrayList - Set Method
Ø The set method is how we replace elements within the arrayList

Set(Index, Object)

Sets the element at the index which you


passed in, to the object you also passed in

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 81


q The ArrayList - Set Method
Ø The set method is how we replace elements within the arrayList

Set(Index, Object)

Sets the element at the index which you


passed in, to the object you also passed in

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 82


q The ArrayList - Set Method
Ø The set method is how we replace elements within the arrayList

Set(Index, Object)

Sets the element at the index which you


passed in, to the object you also passed in exampleAList.set

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 83


q The ArrayList - Set Method
Ø The set method is how we replace elements within the arrayList

Set(Index, Object)

Sets the element at the index which you


passed in, to the object you also passed in exampleAList.set(3,

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 84


q The ArrayList - Set Method
Ø The set method is how we replace elements within the arrayList

Set(Index, Object)

Sets the element at the index which you


passed in, to the object you also passed in exampleAList.set(3,4);

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 85


q The ArrayList - Set Method
Ø The set method is how we replace elements within the arrayList

Set(Index, Object)

Sets the element at the index which you


passed in, to the object you also passed in exampleAList.set(3,4);

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 86


q The ArrayList - Set Method
Ø The set method is how we replace elements within the arrayList

Set(Index, Object)

Sets the element at the index which you


passed in, to the object you also passed in exampleAList.set(3,4);

Index 0 1 2 3
ExampleAList 1 2 3 5

10/26/22 Dr/ Ayman Soliman 87


q The ArrayList - Set Method
Ø The set method is how we replace elements within the arrayList

Set(Index, Object)

Sets the element at the index which you


passed in, to the object you also passed in exampleAList.set(3,4);

Index 0 1 2 3
ExampleAList 1 2 3 4

10/26/22 Dr/ Ayman Soliman 88


q The ArrayList - Set Method
Ø The set method is how we replace elements within the arrayList

Set(Index, Object)

Sets the element at the index which you


passed in, to the object you also passed in exampleAList.set(3,4);

Index 0 1 2 3
ExampleAList 1 2 3 4

10/26/22 Dr/ Ayman Soliman 89


q The ArrayList - Clear Method
Ø For if you just HATE your arrayList

Index 0 1 2 3
ExampleAList 1 2 3 4

10/26/22 Dr/ Ayman Soliman 90


q The ArrayList - Clear Method
Ø For if you just HATE your arrayList

Clear()

Index 0 1 2 3
ExampleAList 1 2 3 4

10/26/22 Dr/ Ayman Soliman 91


q The ArrayList - Clear Method
Ø For if you just HATE your arrayList

Clear()

Clears the arrayList, deleting every


element entirely exampleAList.clear();

Index 0 1 2 3
ExampleAList 1 2 3 4

10/26/22 Dr/ Ayman Soliman 92


q The ArrayList - Clear Method
Ø For if you just HATE your arrayList

Clear()

Clears the arrayList, deleting every


element entirely exampleAList.clear();

Index 0 1 2 3
ExampleAList

10/26/22 Dr/ Ayman Soliman 93


q The ArrayList - toArray Method
Ø Used to convert an arrayList to an Array

Index 0 1 2 3
ExampleAList 1 2 3 4

10/26/22 Dr/ Ayman Soliman 94


q The ArrayList - toArray Method
Ø Used to convert an arrayList to an Array

toArray()

Converts the arrayList into an array.


Must be set equal to the creation of a Object convertedExample[] =
new array exampleAList.toArray();

Index 0 1 2 3
ExampleAList 1 2 3 4

10/26/22 Dr/ Ayman Soliman 95


q The ArrayList - toArray Method
Ø Used to convert an arrayList to an Array

toArray()

Converts the arrayList into an array.


Must be set equal to the creation of a Object convertedExample[] =
new array exampleAList.toArray();

Index 0 1 2 3
ExampleAList 1 2 3 4

10/26/22 Dr/ Ayman Soliman 96


q The ArrayList - toArray Method
Ø Used to convert an arrayList to an Array

toArray()

Converts the arrayList into an array.


Must be set equal to the creation of a Object convertedExample[] =
new array exampleAList.toArray();

Index 0 1 2 3
ExampleAList 1 2 3 4

10/26/22 Dr/ Ayman Soliman 97


q The ArrayList - ArrayList Methods
Ø You should be able to find these, or versions of these, in any language which
also supports arrayLists

Add Method Set Method

Remove Method Clear Method

Get Method toArray Method

10/26/22 Dr/ Ayman Soliman 98


q The ArrayList - ArrayList as a Data Structure

Accessing Searching Inserting Deleting

10/26/22 Dr/ Ayman Soliman 99


q The ArrayList - ArrayList as a Data Structure

Accessing Searching Inserting Deleting

O(1)

10/26/22 Dr/ Ayman Soliman 100


q The ArrayList - ArrayList as a Data Structure

Accessing Searching Inserting Deleting


Get
Method

O(1)

10/26/22 Dr/ Ayman Soliman 101


q The ArrayList - ArrayList as a Data Structure

Accessing Searching Inserting Deleting


Get
Method

O(1)

10/26/22 Dr/ Ayman Soliman 102


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 103


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Arraylist deals with references or pointers


Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 104


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 105


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 106


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 107


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 108


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 109


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 110


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 111


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 112


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 113


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 114


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 115


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 116


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 117


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 118


q The ArrayList - ArrayList as a Data Structure

Memory
Index 0 1 2 3
Example 87 91 100 42

Spot in Memory 42 ... 87 88 ... 91 ... 99 100

Stored Value 4 ... 1 “hey” ... 2 ... true 2

10/26/22 Dr/ Ayman Soliman 119


q The ArrayList - ArrayList as a Data Structure

Accessing Searching Inserting Deleting


Get
Method

O(1)

10/26/22 Dr/ Ayman Soliman 120


q The ArrayList - ArrayList as a Data Structure

Accessing Searching Inserting Deleting


Get
Method

O(1) O(n)

10/26/22 Dr/ Ayman Soliman 121


q The ArrayList - ArrayList as a Data Structure

Accessing Searching Inserting Deleting


Get Add(Object, Index) Add(Object)
Method

O(1) O(n) O(n)

10/26/22 Dr/ Ayman Soliman 122


q The ArrayList - ArrayList as a Data Structure

Accessing Searching Inserting Deleting


Get Add(Object, Index) Add(Object)
Remove( Remove(
Method Object) Index)

O(1) O(n) O(n) O(n)

10/26/22 Dr/ Ayman Soliman 123


q The ArrayList - ArrayList as a Data Structure
u se
A Y S
ALW ?
o t i s t s
y n ayL
W h r r
a Inserting
Accessing Searching Deleting
Get Add(Object, Index) Add(Object)
Remove( Remove(
Method Object) Index)

O(1) O(n) O(n) O(n)

10/26/22 Dr/ Ayman Soliman 124


q The ArrayList - Comparing and Contrasting with Arrays
Arrays ArrayLists

Fixed Size Dynamic Size

Can store all data types Can only store Objects

Methods need to be created Methods are created for you


Doesn’t require much memory use or
Requires more memory use and upkeep
upkeep
Smaller tasks, where you won’t be interacting More interactive programs where you’ll be
or changing the data that often modifying data quite a bit

10/26/22 Dr/ Ayman Soliman 125


q The ArrayList - Conclusion

ArrayLists
A dynamically increasing array

Add Method Set Method

Remove Method Clear Method

Get Method toArray Method

10/26/22 Dr/ Ayman Soliman 126


10/26/22 Dr/ Ayman Soliman 127

You might also like