1 / 85

SD2054 Software Development

SD2054 Software Development. The Limitation of Arrays. Once an array is created it must be sized, and this size is fixed !. The Limitation of Arrays. An array contains no useful pre-defined methods !. The Limitation of Arrays. Use the classes provided in the Java Collections Framework !.

garron
Download Presentation

SD2054 Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SD2054 Software Development

  2. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!

  3. The Limitation of Arrays An array contains no useful pre-defined methods!

  4. The Limitation of Arrays Use the classes provided in the Java Collections Framework!

  5. The Java Collections Framework By the end of this lecture you should be able to: • use the ArrayList class to store a list of objects; • use the HashSet class to store a set of objects • use the HashMap class to store objects in a map; • fix the type of elements within a collection using the generics mechanism; • use objects of your own classes in conjunction with Java's collection classes.

  6. The classes in the JCF are all found in the java.util package. • import java.util.*

  7. Three important interfaces from this group are: • List • Set • Map

  8. The List interface The List interface specifies the methods required to process an ordered list of objects. Such a list may contain duplicates.

  9. The List interface Two implementations provided : ArrayList& LinkedList

  10. Using an ArrayList to store a queue of jobs waiting for a printer

  11. The ArrayList constructor creates an empty list: ArrayList printQ = new ArrayList();

  12. The ArrayList constructor creates an empty list: ArrayList<String>printQ = new ArrayList<String>(); What’s that stuff in angled brackets?

  13. The ArrayList constructor creates an empty list: ArrayList<String>printQ = new ArrayList<String>(); It’s a new Java feature called generics!

  14. Use the interface type instead of the implementation type!

  15. List<String> printQ = new ArrayList<String>(); ArrayList<String> printQ = new ArrayList<String>(); A method that receives a printQ object, would now be declared as follows public void someMethod (List<String> printQIn) { // some code here }

  16. List<String> printQ = new ArrayList<String>(); A method that receives a printQ object, would now be declared as follows public void someMethod (List<String> printQIn) { // some code here }

  17. List<String> printQ = new HashList<String>(); A method that receives a printQ object, would now be declared as follows public void someMethod (List<String> printQIn) { // some code here }

  18. 0 1 2 3 List methods - add printQ.add("myLetter.doc"); printQ.add("myFoto.jpg"); printQ.add("results.xls"); printQ.add("chapter.doc"); myLetter.doc myFoto.jpg results.xls chapter.doc printQ

  19. List methods - toString All the Java collection types have a toString method defined System.out.println(printQ); Lists are displayed as follows. [myLetter.doc, myFoto.jpg, results.xls, chapter.doc]

  20. 2 3 4 1 0 0 1 2 3 myLetter.doc myFoto.jpg results.xls chapter.doc List methods – add revisited printQ.add(0, "importantMemo.doc"); importantMemo.doc printQ

  21. 0 1 2 3 4 importantMemo.doc myLetter.doc myFoto.jpg results.xls chapter.doc List methods – set printQ.set(4, "newChapter.doc"); newChapter.doc printQ

  22. 0 1 2 3 4 importantMemo.doc myLetter.doc myFoto.jpg results.xls chapter.doc List methods – size printQ.set(printQ.size()-1, "newChapter.doc"); newChapter.doc printQ

  23. List methods – indexOf Example: finding “myFoto.jpg” int index = printQ.indexOf("myFoto.jpg"); if (index != -1) { System.out.println("myFoto.jpg is at index position: " + index); } else { System.out.println("myFoto.jpg not in list"); }

  24. 0 1 3 2 3 4 importantMemo.doc myLetter.doc results.xls chapter.doc newChapter.doc List methods – remove Example : removing "myFoto.jpg" printQ.remove(2); printQ.remove("myFoto.jpg"); 2 myFoto.jpg printQ

  25. 0 1 2 3 4 importantMemo.doc myLetter.doc myFoto.jpg results.xls chapter.doc newChapter.doc List methods – get System.out.println("First job is " + printQ.get(0)); First job is importantMemo.doc printQ

  26. List methods – contains if (printQ.contains(“poem.doc”)) { System.out.println(“poem.doc is in the list”); } else { System.out.println(“poem.doc is not in the list”); }

  27. Using the enhanced ‘for’ loop with collection classes

  28. Example: Iterating through the printQ list to find and display those jobs that end with a “.doc” extension: for (Stringitem: printQ) { if (item.endsWith(".doc")) { System.out.println(item); } }

  29. The Set interface The Set interface defines the methods required to process a collection of objects in which there is no repetition, and ordering is unimportant.

  30. Which of these are sets? • a queue of people waiting to see a doctor: 

  31. Which of these are sets? • a list of number one records for each of the 52 weeks of a particular year : 

  32. AK346NY QC771OD AZ885JL Which of these are sets? • car registration numbers allocated parking permits. 

  33. There are 2 implementations provided for the Set interface in the JCF. They are HashSet and TreeSet.

  34. AK346NY QC771OD AZ885JL Using a HashSetto store a collection of vehicle registration numbers

  35. The constructor creates an empty set: Set<String> regNums = new HashSet<String>();

  36. The constructor creates an empty set: Set<String> regNums = new HashSet<String>(); Remember: use the interface type!

  37. The constructor creates an empty set: Set<String> regNums = new HashSet<String>(); Remember: use generics!

  38. Set methods - add The add method allows us to insert objects into the set regNums.add(“V53PLS”); regNums.add(“X85ADZ”); regNums.add(“L22SBG”); regNums.add(“W79TRV”);

  39. Set methods - toString We can display the entire set as follows: System.out.println(regNums); The set is displayed in the same format as a list: [W79TRV, X85ADZ, V53PLS, L22SBG]

  40. Set methods - size As with a list, the size method returns the number of items in the set System.out.println(“Number of items in set: ” + regNums.size() );

  41. Set methods - remove The remove method deletes an item from the set if it is present. regNums.remove(“X85ADZ”); If we now display the set, the given registration will have been removed: [W79TRV, V53PLS, L22SBG]

  42. Set interface also includes contains & isEmpty methods

  43. Using the enhanced ‘for’ loop to iterate through a set

  44. The following enhanced for loop will allow us to iterate through the registration numbers and display all registrations after ‘T’. for (Stringitem: regNums) { if (item.charAt(0)> 'T') { System.out.println(item); } } W79TRV V53PLS [W79TRV, V53PLS, L22SBG]

  45. Iterator objects

  46. An Iterator object allows the items in a collection to be retrieved by providing three methods defined in the Iterator interface:

  47. To obtain an Iterator object from a set, call the iteratormethod.. Iterator<String> elements = regNums.iterator();

More Related