Chapter 4 Flashcards
Class Libraries
Java calls it “libraries”, packages.
Grouping objects is a recurring requirement
The java utility package contains classes for doing this
Requirement Group Objects
-The collection abstraction becomes a class
-The operations are methods of that class
-A particular collection like music collection would be an instance of that class
-Items stored in the instance are object
Collections
We specify:
-The type of collection: ArrayList
-The type of object it will contain:<String>
-Private
ArrayList<String>files;
we say "ArrayList of String"</String></String>
Importing
-How we gain access to lib classes
-Import statements must be placed before class distinctions
Creating an ArrayList Object
-In versions of Java prior to version 7: -files=new ArrayList<String>();</String>
-Java 7 introduced diamond notation:
-Files=new ArrayList<>();
The type of parameter can be inferred from the variable being assigned to a convenience
Features of the Collection
-Increases it capacity as necessary.
-Keeps a private count:
*size() accessor
-Keeps objects in order
-Details of how this is done is hidden
Using the Collection
public class MusicOrganizer & private ArrayList<String>files;
...</String>
public void add file (string filename) & files.add(filename):
*For adding a new file
…
public int setnumberofFiles() &
return files.sizes():
*For returning of files (Delegation)
Generic Classes
-Collections are known as parameterized or generic types
-ArrayLIst implements list functionality:
*add, set, size, etc
One type parameter says what we want a list of:
-ArrayList<Person>
-ArrayList<TicketMachine>
-Etc</TicketMachine></Person>
Index Number
-Position of an object in a collection = index
Starts at 0 & last size is -1
-Remove method can change the index value of remaining objects to fill in the gap
The General Utility of Indices
Using integers to index collections has a general utility
-‘next’ is: index +1
-‘previous’ is: index -1
-last’ is: list.size() -1
‘The first three’ are the items @ indices 0,1,2…
We could also think about accessing items in sequence:0,1,2….
Iteration Fundamentals
-We often want to perform some actions an arbitrary number of times
-Most programming languages include loop statement to make this possible
-Java has several sorts of loop statements
For- each loop pseudo code
I.e
for(ElementType element: collection) (loop body)
- for each element in collection, do the things in the loop body
- Type of loop variable must be the same as the declared element type of the collection we’re going to use
Critique of for-each loop
- only use for processing whole collection - definite iteration
- can’t stop part way
For- Each loop- search tasks are indefinite
- can’t predict, how many places we will have to look
- although there may be an absolute limit (like checking every possible location)
- infinite loops are also possible- through error or the nature of the task
While loop syntax
while(the keys are missing){
look in the next place;
}
or
while(not the keys have been found){
look in the next place;
}
While loop
- uses a boolean condition to decide rather or not to continue
For-each vs while loop
For-each:
- easier to write
- safer, guaranteed to stop
while:
- Don’t have to process whole collection
- doesn’t have to be used with a condition
- could be infinite
Searching a collection
- fundamental activity
- applicable beyond collections
- necessarily indefinite iteration
- must code for both success & failure- exhausted search
- each of the finishing criteria must make the loop’s condition false to stop loop
- collection might be empty
- for each loop inappropriate for searching
Phishing a search
- either there are no more files to check:
index >= files.size() - or the item has been found
found == true
found
! searching
Continuing a search
- with a while loop we need to state the condition for continuing
- so the loop’s condition will be opposite of that for finishing:
index < files.size() && : found
index < files.size() && searching - NB: ‘or’ becomes ‘and’ when inverting everything
Moving away from string
- our collection of string objects for music tracks is limited
- no separate ID of artist, title, etc
Index vs iteration
Ways to iterate over a collection
- for- each loop - use if we want to process every element
- while loop - if we want to stop partway through - and for repetition that doesn’t involve a collection
- iterator object - to stop partway through - often used with collections where indexed access is not very efficient or possible - use to remove from a collection
Iterator & iterator()
- collections have an iterator() method
- this returns an Iterator object
- Iterator <E> has 3 methods:
1. boolean hasNext()
2. E next()
3. void remove()</E> - remove method for Iterator :
it.remove();
null
- used with object types
- used to indicate, ‘no object’
- test to see if an object holds the null value:
if(highestBid == null)… - used to indicate ‘no bid yet’
anonymous objects
- objects are often created and handled on elsewhere immediately:
Lot furtherLot = new Lot(nextLotNumber, description);
lots.add(furtherLot);
we don’t really need furtherLot:
lots.add(new Lot(…));
Chaining method calls
- each object in the chain is called on the object returned from the previous method call in the chain
- methods often return objects
- we often immediately call a method on the returned object
Bid bid = lot.getHighestBid();
Person bidder = bid.getBidder(); - we can use the anonymous object concept and chain method calls
lot.getHighestBid().getBidder()