![Page 1: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/1.jpg)
Lists
Arrays reloaded
![Page 2: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/2.jpg)
Problem
A.A. 2016/17Tecniche di programmazione2
} Store a set of unique words (duplicates shall be ignored)} Class “interface”
public class WordSet {public Boolean add(String str);public void delete(String str);public void dump();
}
![Page 3: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/3.jpg)
Main (driver)
A.A. 2016/17Tecniche di programmazione3
public static void main(String[] args) {Scanner keyboard = new Scanner(System.in);String str;
do {str = keyboard.next();if(!str.equals("-")) {
if(!ws.add(str)) {System.out.println(“Yeuch");
}}
} while(!str.equals("-"));keyboard.close();ws.dump();
ws.remove("foo");ws.dump();
}
![Page 4: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/4.jpg)
Solution 1 (Array)
A.A. 2016/17Tecniche di programmazione4
} Array of String} Check whether a word is already present in the array
before inserting it} Shift the array after deleting an element
![Page 5: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/5.jpg)
Data and constructor
A.A. 2016/17Tecniche di programmazione5
final static int A_BIG_NUMBER = 9999;String[] words;int numWords;
public WordSet() {numWords = 0;words = new String[A_BIG_NUMBER];
}
![Page 6: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/6.jpg)
dump() method
A.A. 2016/17Tecniche di programmazione6
public void dump() {System.out.println("WORDS");for(int t=0; t<numWords; ++t) {
System.out.printf("%d) %s\n", t+1, words[t]);}
}
![Page 7: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/7.jpg)
add() method
A.A. 2016/17Tecniche di programmazione7
public Boolean add(String str) {Boolean newWord = true;for(int t=0; t<numWords; ++t) {
if(str.equals(words[t])) {newWord = false;
}}if(newWord) {
words[numWords++] = str;}return newWord;
}
![Page 8: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/8.jpg)
remove() method
A.A. 2016/17Tecniche di programmazione8
public void remove(String str) {int t;t=0; while(!str.equals(words[t]))
++t;for(++t; t<numWords; ++t) {
words[t-1] = words[t];}--numWords;
}
![Page 9: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/9.jpg)
remove() method
A.A. 2016/17Tecniche di programmazione9
public void remove(String str) {int t;t=0; while(!str.equals(words[t]))
++t;for(++t; t<numWords; ++t) {
words[t-1] = words[t];}--numWords;
}
![Page 10: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/10.jpg)
Collection Family Tree
A.A. 2016/17Tecniche di programmazione10
![Page 11: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/11.jpg)
Lists == Arrays “Reloaded”
A.A. 2016/17Tecniche di programmazione11
} 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 12: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/12.jpg)
List interface
A.A. 2016/17Tecniche di programmazione12
} 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 13: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/13.jpg)
remove() method
A.A. 2016/17Tecniche di programmazione13
public void remove(String str) {words.remove(str);
}
![Page 14: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/14.jpg)
dump() method
A.A. 2016/17Tecniche di programmazione14
public void dump() {System.out.println("WORDS");
Iterator<String> i = words.iterator();while(i.hasNext()) {
System.out.println(i.next());}
}
![Page 15: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/15.jpg)
add() method
A.A. 2016/17Tecniche di programmazione15
public Boolean add(String str) {if(!words.contains(str)) {
words.add(str);return true;
} else {return false;
}}
![Page 16: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/16.jpg)
Collection Family Tree
A.A. 2016/17Tecniche di programmazione16
![Page 17: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/17.jpg)
Data and constructor
A.A. 2016/17Tecniche di programmazione17
} ArrayListList<String> words;
public WordSet() {words = new ArrayList<String>();
}
![Page 18: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/18.jpg)
ArrayList
A.A. 2016/17Tecniche di programmazione18
Foo
BazBar
![Page 19: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/19.jpg)
ArrayList – Delete
A.A. 2016/17Tecniche di programmazione19
BazBar
![Page 20: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/20.jpg)
Collection Family Tree
A.A. 2016/17Tecniche di programmazione20
![Page 21: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/21.jpg)
Data and constructor
A.A. 2016/17Tecniche di programmazione21
} LinkedListList<String> words;
public WordSet() {words = new LinkedList<String>();
}
![Page 22: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/22.jpg)
LinkedList
A.A. 2016/17Tecniche di programmazione22
![Page 23: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/23.jpg)
LinkedList – Delete
A.A. 2016/17Tecniche di programmazione23
![Page 24: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/24.jpg)
ArrayList vs. LinkedList
A.A. 2016/17Tecniche di programmazione24
ArrayList LinkedList
add(element)
remove(object)
get(index)
set(index, element)
add(index, element)
remove(index)
contains(object)
indexOf(object)
![Page 25: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/25.jpg)
ArrayList vs. LinkedList
A.A. 2016/17Tecniche di programmazione25
ArrayList LinkedList
add(element) IMMEDIATE IMMEDIATE
remove(object)
get(index)
set(index, element)
add(index, element)
remove(index)
contains(object)
indexOf(object)
![Page 26: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/26.jpg)
ArrayList vs. LinkedList
A.A. 2016/17Tecniche di programmazione26
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 27: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/27.jpg)
ArrayList vs. LinkedList
A.A. 2016/17Tecniche di programmazione27
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 28: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/28.jpg)
ArrayList vs. LinkedList
A.A. 2016/17Tecniche di programmazione28
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 29: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/29.jpg)
ArrayList vs. LinkedList
A.A. 2016/17Tecniche di programmazione29
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 30: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/30.jpg)
Timing
A.A. 2016/17Tecniche di programmazione30
} Class System – current timestatic long currentTimeMillis(); // in millisecondsstatic long nanoTime(); // in nanoseconds
current value of the most precise
available system timer
![Page 31: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/31.jpg)
Random string
A.A. 2016/17Tecniche di programmazione31
} Not quite random…
String val = “tag_” + num;
![Page 32: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/32.jpg)
Universally unique identifier
A.A. 2016/17Tecniche di programmazione32
} Open Software Foundation (OSF) standard} Part of the Distributed Computing Environment (DCE)} Five versions} Version 4 (completely random)} xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
} x is any hexadecimal digit } y is one of 8, 9, a, or b.
} E.g.,} f47ac10b-58cc-4372-a567-0e02b2c3d479
![Page 33: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/33.jpg)
Random string
A.A. 2016/17Tecniche di programmazione33
import java.util.UUID;
String val = UUID.randomUUID().toString());
![Page 34: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/34.jpg)
List
ArrayList vs. LinkedList
![Page 35: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/35.jpg)
Collection Family Tree
A.A. 2016/2017Tecniche di programmazione35
![Page 36: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/36.jpg)
ArrayList vs. LinkedList
A.A. 2016/2017Tecniche di programmazione36
ArrayList LinkedList
add(element) IMMEDIATE IMMEDIATE
remove(object) SLUGGISH LESS SLUGGISH
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
it.add() SLUGGISH IMMEDIATE
it.remove() SLUGGISH IMMEDIATE
![Page 37: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/37.jpg)
Big O notation
A.A. 2016/2017Tecniche di programmazione37
} 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 38: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/38.jpg)
ArrayList vs. LinkedList
A.A. 2016/2017Tecniche di programmazione38
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 39: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/39.jpg)
ArrayList vs. LinkedList
A.A. 2016/2017Tecniche di programmazione39
*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 40: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/40.jpg)
ArrayList vs. LinkedList
A.A. 2016/2017Tecniche di programmazione40
} 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 41: Arrays reloaded - polito.it · Solution 1 (Array) 4 Tecniche di programmazione A.A. 2016/17} Array of String} Check whether a word is already present in the array before inserting](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed6e404df0eda5e752ae8cf/html5/thumbnails/41.jpg)
Licenza d’uso
A.A. 2016/17Tecniche di programmazione41
} 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/