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

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Grouping objects Iterators. Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator has three methods:
Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps.
Grouping Objects 3 Iterators, collections and the while loop.
Grouping Objects 2 Collections and the for-each loop Collections and the while loop.
Grouping Objects 1 Introduction to Collections.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
ARRAYLIST Collections of Data. ArrayLists Array lists can grow and shrink as needed ArrayList is a generic class (similar to C++ template) ArrayList has.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Arrays and ArrayLists.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Arrays…JavaCPython have fixed lengthyes*yesno are initialized to default values yesno? track their own lengthyesnoyes trying to access “out of bounds”
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
CSE 1201 Object Oriented Programming ArrayList 1.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
The ArrayList Data Structure Standard Arrays at High Speed!
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting.
CMSC 202 ArrayList Aug 9, 2007.
Objects First with Java CITS1001 week 4
Java Arrays and ArrayLists COMP T1 #5
Comp1004: Loops and Arrays II
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Intro to Collections.
Objects First with Java CITS1001
Array List Pepper.
COS 260 DAY 9 Tony Gauvin.
TCSS 143, Autumn 2004 Lecture Notes
Can store many of the same kind of data together
Arrays versus ArrayList
CMSC 202 ArrayList Aug 9, 2007.
Java Programming Arrays
Can store many of the same kind of data together
Object Oriented Programming in java
Exercise 1 Declare a constant of type int called SIZE and initialize it to value 10 Declare two int arrays of size “SIZE” Assume that those int arrays.
CMSC 202 ArrayList Aug 9, 2007.
Collections and iterators
Can store many of the same kind of data together
Creating and Modifying Text part 3
Review: libraries and packages
Collections and iterators
Why not just use Arrays? Java ArrayLists.
Presentation transcript:

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

Lecture 8 über-Arrays OO

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

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

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

So what is ArrayList An object Like an array OO

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

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?

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

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...

many useful methods like....? Check out the api: – Application Programming Interface type ‘java api’ into google visit

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

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

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..

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

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

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

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?!?

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)); }

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

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

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:

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.

BlueJ example BlueJ has a notebook example that uses an ArrayList

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

Adding a third note

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

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

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

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()); }

????! 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:

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

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.

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