Your SlideShare is downloading. ×
0
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Lists (Java Collections)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Lists (Java Collections)

636

Published on

Introduction to the List data structure and its implementation in the Java Collections Framework. …

Introduction to the List data structure and its implementation in the Java Collections Framework.


Teaching material for the course of "Tecniche di Programmazione" at Politecnico di Torino in year 2012/2013. More information: http://bit.ly/tecn-progr

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
636
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
49
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. ListsArrays reloaded
  • 2. ProblemA.A. 2012/2013Tecniche di programmazione2Store 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();}
  • 3. Solution 1 (Array)A.A. 2012/2013Tecniche di programmazione3Array of StringCheck whether a word is already present in the arraybefore inserting itShift the array after deleting an element
  • 4. Data and constructorA.A. 2012/2013Tecniche di programmazione4final static int A_BIG_NUMBER = 9999;String[] words;int numWords;public WordSet() {numWords = 0;words = new String[A_BIG_NUMBER];}
  • 5. dump() methodA.A. 2012/2013Tecniche di programmazione5public void dump() {System.out.println("WORDS");for(int t=0; t<numWords; ++t) {System.out.printf("%d) %sn", t+1, words[t]);}}
  • 6. add() methodA.A. 2012/2013Tecniche di programmazione6public 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;}
  • 7. remove() methodA.A. 2012/2013Tecniche di programmazione7public 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;}
  • 8. remove() methodA.A. 2012/2013Tecniche di programmazione8public 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;}
  • 9. Collection Family TreeA.A. 2012/2013Tecniche di programmazione9
  • 10. Lists == Arrays “Reloaded”A.A. 2012/2013Tecniche di programmazione10Lists are (probably) the most widely used Java collectionsLike arraysfull visibility and control over the ordering of its elementsmay contain duplicatesUnlike arraysresize smoothly
  • 11. List interfaceA.A. 2012/2013Tecniche di programmazione11Add/remove elementsboolean add(element)boolean remove(object)Positional Accesselement get(index)element set(index, element)void add(index, element)element remove(index)Searchboolean contains(object)int indexOf(object)
  • 12. remove() methodA.A. 2012/2013Tecniche di programmazione12public void remove(String str) {words.remove(str);}
  • 13. dump() methodA.A. 2012/2013Tecniche di programmazione13public void dump() {System.out.println("WORDS");Iterator<String> i = words.iterator();while(i.hasNext()) {System.out.println(i.next());}}
  • 14. add() methodA.A. 2012/2013Tecniche di programmazione14public Boolean add(String str) {if(!words.contains(str)) {words.add(str);return true;} else {return false;}}
  • 15. Collection Family TreeA.A. 2012/2013Tecniche di programmazione15
  • 16. Data and constructorA.A. 2012/2013Tecniche di programmazione16ArrayListLinkedListList<String> words;public WordSet() {words = new ArrayList<String>();}List<String> words;public WordSet() {words = new LinkedList<String>();}
  • 17. ArrayListA.A. 2012/2013Tecniche di programmazione17FooBazBar
  • 18. ArrayList – DeleteA.A. 2012/2013Tecniche di programmazione18BazBar
  • 19. TimingA.A. 2012/2013Tecniche di programmazione19Class System – current timestatic long currentTimeMillis(); // in millisecondsstatic long nanoTime(); // in nanosecondscurrent value of themost preciseavailable systemtimer
  • 20. Random stringA.A. 2012/2013Tecniche di programmazione20Not quite random…String val = “tag_” + num;
  • 21. Universally unique identifierA.A. 2012/2013Tecniche di programmazione21Open Software Foundation (OSF) standardPart of the Distributed Computing Environment (DCE)Five versionsVersion 4 (completely random)xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxxx is any hexadecimal digity is one of 8, 9, a, or b.E.g.,f47ac10b-58cc-4372-a567-0e02b2c3d479
  • 22. Random stringA.A. 2012/2013Tecniche di programmazione22import java.util.UUID;String val = UUID.randomUUID().toString());
  • 23. Data and constructorA.A. 2012/2013Tecniche di programmazione23ArrayListLinkedListList<String> words;public WordSet() {words = new ArrayList<String>();}List<String> words;public WordSet() {words = new LinkedList<String>();}
  • 24. LinkedListA.A. 2012/2013Tecniche di programmazione24
  • 25. LinkedList – DeleteA.A. 2012/2013Tecniche di programmazione25
  • 26. ArrayList vs. LinkedListA.A. 2012/2013Tecniche di programmazione26ArrayList LinkedListadd(element)remove(object)get(index)set(index, element)add(index, element)remove(index)contains(object)indexOf(object)
  • 27. ArrayList vs. LinkedListA.A. 2012/2013Tecniche di programmazione27ArrayList LinkedListadd(element) IMMEDIATE IMMEDIATEremove(object)get(index)set(index, element)add(index, element)remove(index)contains(object)indexOf(object)
  • 28. ArrayList vs. LinkedListA.A. 2012/2013Tecniche di programmazione28ArrayList LinkedListadd(element) IMMEDIATE IMMEDIATEremove(object) SLUGGISH IMMEDIATEget(index)set(index, element)add(index, element)remove(index)contains(object)indexOf(object)
  • 29. ArrayList vs. LinkedListA.A. 2012/2013Tecniche di programmazione29ArrayList LinkedListadd(element) IMMEDIATE IMMEDIATEremove(object) SLUGGISH IMMEDIATEget(index) IMMEDIATE SLUGGISHset(index, element) IMMEDIATE SLUGGISHadd(index, element)remove(index)contains(object)indexOf(object)
  • 30. ArrayList vs. LinkedListA.A. 2012/2013Tecniche di programmazione30ArrayList LinkedListadd(element) IMMEDIATE IMMEDIATEremove(object) SLUGGISH IMMEDIATEget(index) IMMEDIATE SLUGGISHset(index, element) IMMEDIATE SLUGGISHadd(index, element) SLUGGISH SLUGGISHremove(index) SLUGGISH SLUGGISHcontains(object)indexOf(object)
  • 31. ArrayList vs. LinkedListA.A. 2012/2013Tecniche di programmazione31ArrayList LinkedListadd(element) IMMEDIATE IMMEDIATEremove(object) SLUGGISH IMMEDIATEget(index) IMMEDIATE SLUGGISHset(index, element) IMMEDIATE SLUGGISHadd(index, element) SLUGGISH SLUGGISHremove(index) SLUGGISH SLUGGISHcontains(object) SLUGGISH SLUGGISHindexOf(object) SLUGGISH SLUGGISH
  • 32. ArrayList vs. LinkedListA.A. 2012/2013Tecniche di programmazione32ArrayList LinkedListadd(element) IMMEDIATE IMMEDIATEremove(object) SLUGGISH IMMEDIATEget(index) IMMEDIATE SLUGGISHset(index, element) IMMEDIATE SLUGGISHadd(index, element) SLUGGISH SLUGGISHremove(index) SLUGGISH SLUGGISHcontains(object) SLUGGISH SLUGGISHindexOf(object) SLUGGISH SLUGGISHeverybody lies
  • 33. Licenza d’usoA.A. 2012/2013Tecniche di programmazione33Queste diapositive sono distribuite con licenza Creative Commons“Attribuzione - Non commerciale - Condividi allo stesso modo (CCBY-NC-SA)”Sei libero:di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,rappresentare, eseguire e recitare questoperadi modificare questoperaAlle seguenti condizioni:Attribuzione — Devi attribuire la paternità dellopera agli autorioriginali e in modo tale da non suggerire che essi avallino te o il modo incui tu usi lopera.Non commerciale — Non puoi usare questopera per finicommerciali.Condividi allo stesso modo — Se alteri o trasformi questopera, o sela usi per crearne unaltra, puoi distribuire lopera risultante solo con unalicenza identica o equivalente a questa.http://creativecommons.org/licenses/by-nc-sa/3.0/

×