CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Announcements Exam 3 on Wednesday 11/10 –covers material from last exam up to and including Friday 11/05 Review on Monday 11/08
Agenda Collections Inheritance – our last relationship!
Collections We have seen that a variable can hold one object reference at a time. How do we effectively deal with multiple references? –arbitrarily many –what does addActionListener do?
Collections interface: java.util.Collection (some) classes implementing interface: –java.util.HashSet –java.util.ArrayList E is the type of element contained in the collection – replace by an actual type
Use of a collection HashSet names = new HashSet (); names.add(“Amy”);names.remove(“Bob”); names.add(“Bob”); names.add(“Cindy”); names.add(“Dave”); names.add(“Emma”); …
for-each loop for (String name : names) { System.out.println(name); } This would print out: Amy Cindy Dave Emma
Code demo Consider types from last time: public interface Noisy { public void sound(); } public class Cat implements Noisy {...} public class Dog implements Noisy {...} This is clearly a toy example. We used this to illustrate the use of a collection, the foreach loop, and String concatenation.
String concatenation ‘+’ is the String concatenation operator If s1 and s2 are references to String objects, then s1+s2 is an expression whose value is a reference to a new String object, consisting of all the characters of s1, followed by all the characters of s2.