introduzione al corso · 2020-03-14 · lists == arrays “reloaded” 3 tecniche di programmazione...

22
Lists Arrays reloaded

Upload: others

Post on 17-Apr-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

Lists

Arrays reloaded

Page 2: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

Collection Family Tree

A.A. 2019/20Tecniche di programmazione2

Page 3: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

Lists == Arrays “Reloaded”

A.A. 2019/20Tecniche di programmazione3

Lists are (probably) the most widely used Java collections

Like arrays

full visibility and control over the ordering of its elements

may contain duplicates

Unlike arrays

resize smoothly

Page 4: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

List interface

A.A. 2019/20Tecniche di programmazione4

Add/remove elements

boolean add(element)

boolean remove(object)

Positional Access

element get(index)

element set(index, element)

void add(index, element)

element remove(index)

Search

boolean contains(object)

int indexOf(object)

Page 5: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

Collection Family Tree

A.A. 2019/20Tecniche di programmazione5

Page 6: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

Data and constructor

A.A. 2019/20Tecniche di programmazione6

ArrayList

List<String> words;

public WordSet() {

words = new ArrayList<String>();

}

Page 7: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

ArrayList

A.A. 2019/20Tecniche di programmazione7

Qui

QuaQuo

Page 8: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

ArrayList – Delete

A.A. 2019/20Tecniche di programmazione8

QuaQuo myList.remove(0) ;

Page 9: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

Data and constructor

A.A. 2019/20Tecniche di programmazione9

LinkedList

List<String> words;

public WordSet() {

words = new LinkedList<String>();

}

Page 10: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

LinkedList

A.A. 2019/20Tecniche di programmazione10

Page 11: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

LinkedList – Delete

A.A. 2019/20Tecniche di programmazione11

Page 12: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

ArrayList vs. LinkedList

A.A. 2019/20Tecniche di programmazione12

ArrayList LinkedList

add(element)

remove(object)

get(index)

set(index, element)

add(index, element)

remove(index)

contains(object)

indexOf(object)

Page 13: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

ArrayList vs. LinkedList

A.A. 2019/20Tecniche di programmazione13

ArrayList LinkedList

add(element) IMMEDIATE IMMEDIATE

remove(object)

get(index)

set(index, element)

add(index, element)

remove(index)

contains(object)

indexOf(object)

Page 14: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

ArrayList vs. LinkedList

A.A. 2019/20Tecniche di programmazione14

ArrayList LinkedList

add(element) IMMEDIATE IMMEDIATE

remove(object) SLUGGISH LESS SLUGGHISH

get(index)

set(index, element)

add(index, element)

remove(index)

contains(object)

indexOf(object)

Page 15: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

ArrayList vs. LinkedList

A.A. 2019/20Tecniche di programmazione15

ArrayList LinkedList

add(element) IMMEDIATE IMMEDIATE

remove(object) SLUGGISH LESS SLUGGHISH

get(index) IMMEDIATE SLUGGISH

set(index, element) IMMEDIATE SLUGGISH

add(index, element)

remove(index)

contains(object)

indexOf(object)

Page 16: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

ArrayList vs. LinkedList

A.A. 2019/20Tecniche di programmazione16

ArrayList LinkedList

add(element) IMMEDIATE IMMEDIATE

remove(object) SLUGGISH LESS SLUGGHISH

get(index) IMMEDIATE SLUGGISH

set(index, element) IMMEDIATE SLUGGISH

add(index, element) SLUGGISH SLUGGISH

remove(index) SLUGGISH SLUGGISH

contains(object)

indexOf(object)

Page 17: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

ArrayList vs. LinkedList

A.A. 2019/20Tecniche di programmazione17

ArrayList LinkedList

add(element) IMMEDIATE IMMEDIATE

remove(object) SLUGGISH LESS SLUGGHISH

get(index) IMMEDIATE SLUGGISH

set(index, element) IMMEDIATE SLUGGISH

add(index, element) SLUGGISH SLUGGISH

remove(index) SLUGGISH SLUGGISH

contains(object) SLUGGISH SLUGGISH

indexOf(object) SLUGGISH SLUGGISH

Page 18: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

ArrayList vs. LinkedList

A.A. 2019/20Tecniche di programmazione18

*source: http://www.programcreek.com/2013/03/arraylist-vs-linkedlist-vs-vector/

10,000 add(Element e)

10,000 get(int index)

10,000 remove(int index)

Page 19: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

Big O notation

A.A. 2019/20Tecniche di programmazione19

O(n)

Used to compare different implementation of a Collection

O(n) is used to note that the time required for the execution

of an algorithm rises like n

n is usually intended as the dimension of the data.

Examples

O(n^2) takes a time that is quadratic-dependent by n

O(n) takes a time that is linear-dependent by n

O(log n) takes a time that is dependent from the log n

O(C) or O(1) is a constant-time operation

Page 20: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

ArrayList vs. LinkedList

A.A. 2019/20Tecniche di programmazione20

ArrayList LinkedList

add(element) O(1) O(1)

remove(object) O(n) + O(n) O(n) + O(1)

get(index) O(1) O(n)

set(index, elem) O(1) O(n) + O(1)

add(index, elem) O(1) + O(n) O(n) + O(1)

remove(index) O(n) O(n) + O(1)

contains(object) O(n) O(n)

indexOf(object) O(n) O(n)

it.add() O(n) O(1)

it.remove() O(n) O(1)

Page 21: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

ArrayList vs. LinkedList

A.A. 2019/20Tecniche di programmazione21

ArrayList

get(index) and set(index, element) are O(1)

adding or removing an element in last position are O(1)

add(element) with resize could cost O(n)

LinkedList

iterator.remove() and listIterator.add() are O(1)

adding or removing an element in first position are O(1)

Memory footprint

LinkedList uses more memory than an ArrayList

Page 22: Introduzione al corso · 2020-03-14 · Lists == Arrays “Reloaded” 3 Tecniche di programmazione A.A. 2019/20 Lists are (probably) the most widely used Java collections Like arrays

Licenza d’uso

A.A. 2019/20Tecniche di programmazione22

Queste diapositive sono distribuite con licenza Creative Commons“Attribuzione - Non commerciale - Condividi allo stesso modo (CC BY-NC-SA)”

Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,

rappresentare, eseguire e recitare quest'opera

di modificare quest'opera

Alle seguenti condizioni: Attribuzione — Devi attribuire la paternità dell'opera agli autori

originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera.

Non commerciale — Non puoi usare quest'opera per fini commerciali.

Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa.

http://creativecommons.org/licenses/by-nc-sa/3.0/