Download presentation
Presentation is loading. Please wait.
1
Chapters 17-18 (ArrayList / Generic)
Big Java Chapters 17-18 (ArrayList / Generic)
2
Generic Programming The design and implementation of data structures and algorithms that work for multiple types.
3
Generic Class Uses a type variable to achieve genericity
public class ArrayList<E> { public ArrayList() { . . .} public void add(E element) { } } may have one or more Can instantiate type variable with class or interface type, not primitive (use wrapper)
4
Example ArrayList<BankAccount> accounts1 = new ArrayList<BankAccount>(); LinkedList accounts2 = new LinkedList(); // intended for BankAccounts accounts1.add(“my savings”); // compiler error – WHY ? accounts2.add(“my savings”); // not detected
5
Names for Type Variables
Often use short uppercase names, such as: Type Name Meaning E Element in collection K Key type in map V Value type in map T General type S,U Additional general types
6
Example – useful return type
public class Pair<T, S> { public Pair(T firstElement, S, second Element) first = firstElement; second = second Element } public T getFirst() { return first; } public S getSecond() { return second; } private T first; private S second;
7
Generic Methods Method with a type variable
Useful for methods that differ only by one or more types Can start with method that works on a specific types, then make generic
8
Generic Method Example
public static void print(String[] a) { for (String e : a) System.out.print(e + “ “); System.out.println(); } public static <E> void print(E[] a) for (E e : a) Rectangle[] rectangles = . . .; ArrayUtil.print(rectangles); Methods are not required to be static Method call based on parameters, doesn’t specify E explicitly Assume in ArrayUtil class
9
Constraining Type Variables
actually means extends or implements public static <E extends Comparable> E min(E[] a) { E smallest = a[0]; for (int i=0; i<a.length; i++) if (a[i].compareTo(smallest) < 0) smallest = a[i]; return smallest; } could also do: <E extends Comparable & Cloneable> if have more than one constraint
10
Genericity and Inheritance
If SavingsAccount is a subclass of BankAccount, is ArrayList<SavingsAccount> a subclass of ArrayList<BankAccount>? NO. Necessary for type checking. ArrayList<SavingsAccount> savingsAccounts = new ArrayList<SavingsAccount>(); ArrayList<BankAccount> bankAccounts = savingsAccounts; // Not legal, but if it were… BankAccount myChecking = new CheckingAccount(); BankAccounts.add(myChecking); // just added checking account to savings account list
11
Wildcard Types – Advanced Topic
A wildcard type is a type that can remain unknown. public void addAll(LinkedList<? extends E> other) { ListIterator<E> iter = other.listIterator(); while (iter.hasNext()) add(iter.next()); } Allows you to add any type that is a subtype of E. Read Advanced Topic 17.1 for more details
12
Raw Types The Java Virtual Machine (JVM) uses raw types in which type variables are replaced with ordinary types The compiler converts generic classes and type variables into regular classes/raw types for the JVM Type variables are replaced with Object, e.g., public Pair(Object firstElement, Object secondElement) { … } Type erasure enables generic classes to interoperate with legacy code (e.g., original ArrayList).
13
Limitations You cannot use type variables to define static fields, static methods or static inner classes You cannot create a new object of a type variable type (remember it is replaced with Object)
14
Graphical User Interfaces…
15
BorderLayout JPanel panel = new JPanel();
panel.setLayout(new BorderLayout()); panel.add(component, BorderLayout.NORTH); NORTH WEST CENTER EAST SOUTH
16
GridLayout JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 3)); buttonPanel.add(button7); buttonPanel.add(button8); … CE
17
Nesting Panels JPanel keypadPanel = new JPanel();
keypadPanel.setLayout(new BorderLayout()); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(4, 3)); buttonPanel.add(button7); buttonPanel.add(button8); … keypadPanel.add(buttonPanel, BorderLayout.CENTER); JTextField display = new JTextField(); keypadPanel.add(display, BorderLayout.NORTH); CE
18
Making Choices – Radios, Combos & Checks
19
Combo Box editable – can type in your own selection
20
Check Box & Border Many other border types available – see API
21
Radio Buttons Need ButtonGroup for grouping, only one
selected at a time
22
Control Panel for All Controls
It’s easy once the other panels are created! controls are going in the bottom
23
Controlling the Font access combo box for font name
access check boxes to determine bold/italic settings access radio buttons to determine size settings – sometimes separate field is used for this repaint causes changes to be visible
24
Putting it together Same listener for all components
25
Reading Assignment How To 18.1 Laying Out a User Interface, pages – summarizes how to achieve an interface
26
Menus
27
File Menus Creates menu item Creates menu item with listener
28
Menu and Item
29
Setting Font Note use of instance variables for size,
facename and style
30
Putting it all Together
Create a JMenuBar and add menus to it
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.