Presentation is loading. Please wait.

Presentation is loading. Please wait.

Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.

Similar presentations


Presentation on theme: "Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object."— Presentation transcript:

1 Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object

2 Lecture 8 über-Arrays OO

3 Coming up ArrayList ArrayList vs Array –D–Declaration –I–Insertion –A–Access –R–Removal Wrapper classes Iterator object

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 OO

5 ArrayList to the rescue! We can fix (most of) those problems Sun have a library of helpful classes you can use for free They are downloaded with the JVM etc ArrayList is one of these library classes

6 So what is ArrayList An object Like an array OO

7 Coming up ArrayList ArrayList vs Array –D–Declaration –I–Insertion –A–Access –R–Removal Wrapper classes Iterator object

8 Array vs ArrayList ArrayArrayList They don’t change sizeChanges size as you add elements It’s a pain adding new elements if you don’t know how many are there already ArrayList has an add() method and takes care of its size itself You have to use indexes You can use indexes if you want, but you don’t have to ArrayIndexOutOfBoundsException Still thrown by ArrayList. Hey, it’s a fact of life, okay?

9 Cat[] catArray; catArray = new Cat[10]; catArray[0] = moggy1; catArray[1] = moggy2; callMethodOn(catArray[1]); catArray[0] = null; ArrayList catAList; catAList = new ArrayList(); catAList.add(moggy1); catAList.add(moggy2); callMethodOn(catAList.get(1)); catAList.remove(moggy1); InsertionAccessRemovalDeclaration ArrayList can use indexes too

10 So ArrayLists are quite similar... Why would we need them? ArrayList Advantages: – They grow and shrink when you add and remove things – arrays are fixed size – They have many useful methods...

11 many useful methods like....? Check out the api: – Application Programming Interface http://java.sun.com/j2se/1.5.0/docs/api/ type ‘java api’ into google visit www.gotApi.com

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

13 Coming up ArrayList ArrayList vs Array –D–Declaration –I–Insertion –A–Access –R–Removal Wrapper classes Iterator object

14 ArrayLists and Primitives ArrayLists can’t store primitives So what if we want to store int s in an ArrayList? If ArrayList can only store objects, let’s make our primitives into objects..

15 Memory Wrappers Java has wrapper classes for each of the primitives myNumber int myNumber Integer

16 For every Primitive......there is a wrapper Watch out for the names, some of them are maybe a little different than you’d expect booleanBoolean byteByte charCharacter doubleDouble floatFloat intInteger longLong shortShort

17 How do I use one? int myInt; myInt = 5; Integer myInteger = new Integer(myInt); myArrayList.add(myInt);

18 What if I want the int back later? int getMyInt = myInteger.intValue(); Each Wrapper class has an intValue(), doubleValue(), longValue() etc method Do you mean to say that every time I want to put an int in an array, I have to wrap it when I put it in and then unwrap it when I get it out?!?

19 not quite So what happens if the following code is run? ArrayList arrl = new ArrayList(); arrl.add(1); arrl.add(2); for(int i=0; i<2; i++){ System.out.println(arrl.get(i)); }

20 It looks like it shouldn’t work...but it does Java recognises that storing ints and other primitives is useful So if you try and put a primitive in......it’ll box it up into an object for you

21 huh? In various (specified) situations, Java will autobox your primitives for you. Like putting ints into an ArrayList

22 A closer look at Collection Syntax Often you will see code written like: ArrayList This is: – the type of collection: ArrayList – the type of objects it will contain:

23 Why? This is a compiler thing It will check you code to make sure you don’t try to put an Integer into your ArrayList of Strings It’s called type checking, checking whether you are using the right data type.

24 BlueJ example BlueJ has a notebook example that uses an ArrayList

25 Object structures with collections Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

26 Adding a third note

27 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Removal may affect numbering

28 Coming up ArrayList ArrayList vs Array –D–Declaration –I–Insertion –A–Access –R–Removal Wrapper classes Iterator object

29 Collections design Collections are designed to store things So they are also designed to allow easy retrieval We can use loops to iterate though a collection But the java collections package comes with an iterator object My Collection My Collection My Collection Iterator My Collection Iterator

30 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using an Iterator object Iterator it = myCollection.iterator(); while(it.hasNext()) { call it.next() to get the next object do something with that object } java.util.Iterator returns an Iterator object public void listNotes() { Iterator it = notes.iterator(); while(it.hasNext()) { System.out.println(it.next()); }

31 ????! All collections have an iterator object that “comes with” them You call theCollectionImUsing.iterator(); to have this object returned to you. The iterator object has 3 methods:

32 These methods fit nicely in a while loop public void listNotes() { Iterator it=notes.iterator(); while(it.hasNext()) { System.out.println(it.next()); }

33 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Index versus Iterator Ways to iterate over a collection: – for-each loop. Use if we want to process every element. – while loop. Use if we might want to stop part way through. Use for repetition that doesn't involve a collection. – Iterator object. Use if we might want to stop part way through. Often used with collections where indexed access is not very efficient, or impossible. Iteration is an important programming pattern.

34 Covered this lecture ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object


Download ppt "Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object."

Similar presentations


Ads by Google