1 / 32

Comp1004: Loops and Arrays II

Comp1004: Loops and Arrays II. Collections and Iterators. Coming Up. Arrays vs. ArrayLists Declaration Insertion Access Removal A Brief Introduction to Generics Autoboxing and unboxing Iterator objects. Arrays vs. ArrayLists. Problems with Arrays. They don’t change size

joshua
Télécharger la présentation

Comp1004: Loops and Arrays II

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. Comp1004: Loops and Arrays II Collections and Iterators

  2. Coming Up • Arrays vs. ArrayLists • Declaration • Insertion • Access • Removal • A Brief Introduction to Generics • Autoboxing and unboxing • Iterator objects

  3. Arrays vs. ArrayLists

  4. Problems with Arrays • They don’t change size • It’s a pain adding new elements if you don’t know how many are there already • You have to use indexes • ArrayIndexOutOfBoundsException

  5. ArrayList to the rescue! • Arrays are built into the Java language (a bit like primitives) • But Java also have a library of helpful classes you can use for free • These are not part of the language, but are included with every JVM • ArrayList is one of these library classes

  6. Array vs ArrayList

  7. Array vs ArrayList

  8. Array vs ArrayList

  9. Array vs ArrayList

  10. Array vs ArrayList

  11. Array vs ArrayList Declaration Insertion Access Removal Cat[] catArray; catArray= new Cat[10]; catArray[0] = moggy1; catArray[1] = moggy2; callMethodOn(catArray[1]); catArray[0] = null; ArrayListcatAList; catAList = new ArrayList(); catAList.add(moggy1); catAList.add(moggy2); callMethodOn(catAList.get(1)); catAList.remove(moggy1);

  12. ArrayList Advantages • Arrays are useful for simple small tasks • ArrayLists are better for more complex tasks • They grow and shrink when you add and remove things – arrays are fixed size • They have many useful methods... • Check out the API: • Application Programming Interface • http://download.oracle.com/javase/6/docs/api/ • type ‘java api’ into google

  13. Here’s a bit of the method summary for ArrayList...

  14. Brief Introduction to Generics

  15. Spot the Problem… ArrayListkennel = new ArrayList(); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); kennel.add(new Cat(“Mr Tiddles”)); for(inti = 0; i < kennel.size(); i++) { kennel.get(i).bark(); }

  16. Spot the Problem… ArrayLists store objects of any type ArrayListkennel = new ArrayList(); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); kennel.add(new Cat(“Mr Tiddles”)); for(inti = 0; i < kennel.size(); i++) { kennel.get(i).bark(); } Which means we can mix up the types of objects in the ArrayList Which may cause problems later if we make assumptions about what is in there!

  17. Spot the Problem… ArrayLists store objects of any type ArrayListkennel = new ArrayList(); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); kennel.add(new Cat(“Mr Tiddles”)); for(inti = 0; i < kennel.size(); i++) { kennel.get(i).bark(); } Which means we can mix up the types of objects in the ArrayList Which may cause problems later if we make assumptions about what is in there! In fact this code will not compile, because Java does not know what is in the ArrayList, and therefore will not let you call barkon it

  18. Solving the Problem… ArrayListkennel = new ArrayList(); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); kennel.add(new Cat(“Mr Tiddles”)); for(inti = 0; i < kennel.size(); i++) { kennel.get(i).bark(); }

  19. Solving the Problem… ArrayListkennel = new ArrayList(); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); kennel.add(new Cat(“Mr Tiddles”)); for(inti = 0; i < kennel.size(); i++) { if(kennel.get(i) instanceof Dog) { Dog d = (Dog)kennel.get(i); d.bark(); } } One option is to test what is in there using instanceof, and if it’s a Dog we can tell the compiler. This is called typecasting

  20. Solving the Problem… ArrayListkennel = new ArrayList(); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); kennel.add(new Cat(“Mr Tiddles”)); for(inti = 0; i < kennel.size(); i++) { if(kennel.get(i) instanceof Dog) { Dog d = (Dog)kennel.get(i); d.bark(); } } One option is to test what is in there using instanceof, and if it’s a Dog we can tell the compiler. This is called typecasting Makes my inner software engineer cringe! instanceof is a tool of last resort. If you’ve had to use it it probably means you’re program is not designed particularly well.

  21. Solving the Problem… ArrayListkennel = new ArrayList(); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); kennel.add(new Cat(“Mr Tiddles”)); for(inti = 0; i < kennel.size(); i++) { kennel.get(i).bark(); } It would be better if we could ensure that the ArrayListonly contained Dogs in the first place

  22. Solving the Problem… ArrayList<Dog> kennel = new ArrayList<Dog>(); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); kennel.add(new Cat(“Mr Tiddles”)); for(inti = 0; i < kennel.size(); i++) { kennel.get(i).bark(); } It would be better if we could ensure that the ArrayListonly contained Dogs in the first place This is easily done because ArrayList uses a mechanism called generics. We can specify the type allowed when we create the ArrayList. Now Java will only allow us to add things of type Dog. So this line will force a compile time error

  23. A Note About Primitives ArrayLists (and other collections in the API) can only store objects. This means that when you want to store primitives you need to use wrapper objects. This is a pain :-( ArrayList<Integer> numStore; numStore = new ArrayList<Integer>(); numStore.add(new Integer(3)); numStore.add(new Integer(5)); numStore.add(new Integer(2)); int total = 0; for(inti = 0; i < numStore.size(); i++) { total = total + numStore.get(i).intValue(); } System.out.println(“Total is ” + total);

  24. A Note About Primitives ArrayLists (and other collections in the API) can only store objects. This means that when you want to store primitives you need to use wrapper objects. This is a pain :-( ArrayList<Integer> numStore; numStore = new ArrayList<Integer>(); numStore.add(3); numStore.add(5); numStore.add(2); int total = 0; for(inti = 0; i < numStore.size(); i++) { total = total + numStore.get(i); } System.out.println(“Total is ” + total); Java 5 introduced autoboxing, a process where primitives are automatically cast to a wrapper where necessary. And unboxing, where they can be cast back again too

  25. Iterators

  26. Design Patterns • In Programming a neat and elegant way of solving a problem is sometimes called a design pattern • The Java API uses a number of well-known design patterns • Including the use of iterators to help you iterate over a collection

  27. Back at the Kennel… In our kennel example we used a for loop to iterate over the array ArrayList<Dog> kennel = new ArrayList<Dog>(); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); for(inti = 0; i < kennel.size(); i++) { kennel.get(i).bark(); }

  28. Back at the Kennel… In our kennel example we used a for loop to iterate over the array ArrayList<Dog> kennel = new ArrayList<Dog>(); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); for(inti = 0; i < kennel.size(); i++) { kennel.get(i).bark(); } Iterator<Dog> it = kennel.iterator(); while(it.hasNext()) { it.next().bark(); } We could instead use an iterator object. Iterators are generic classes (like the ArrayList) and track our progress through a collection. We can use hasNext to see if there are more elements And next to get the next element (the iterator will automatically move to the next element).

  29. Why are Iterators a useful pattern? ArrayList<Dog> kennel = new ArrayList<Dog>(); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); for(inti = 0; i < kennel.size(); i++) { kennel.get(i).bark(); } Iterator<Dog> it = kennel.iterator(); while(it.hasNext()) { it.next().bark(); }

  30. Why are Iterators a useful pattern? ArrayList<Dog> kennel = new ArrayList<Dog>(); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); for(inti = 0; i < kennel.size(); i++) { kennel.get(i).bark(); } Iterator<Dog> it = kennel.iterator(); while(it.hasNext()) { it.next().bark(); } 1) They are neater, and neat code is easier to read and understand 2) They decouple the loop from the collection (notice that in the loop we do not reference the Arraylist at all) This means we could pass the iterator to a method – and that method does not even need to know what the collection is!

  31. Why are Iterators a useful pattern? public void makeThemBark(Iterator<Dog> it) { while(it.hasNext()) { it.next().bark(); } } 1) They are neater, and neat code is easier to read and understand 2) They decouple the loop from the collection (notice that in the loop we do not reference the Arraylist at all) This means we could pass the iterator to a method – and that method does not even need to know what the collection is!

  32. Summary • Arrays vs. ArrayLists • Declaration • Insertion • Access • Removal • A Brief Introduction to Generics • Autoboxing and unboxing • Iterator objects

More Related