








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









docsity.com
generics To use generic classes and interfaces
interfaces ^ To^ declare^ generic^ classes
and^ interfaces ^ To^ understand^ why^ generic
types^ can^ improve^ reliability
and readabilityreadability To^ declare^ and^ use^ generic^ methods
and^ bounded^ generic^ types ^ To^ use^ raw^ types^ for^ backward
compatibility ^ To know wildcard types and understand why they are necessary^ To^ know^ wildcard^ types^ and
understand^ why^ they^ are^ necessary ^ To^ convert^ legacy^ code^ using
JDK^ 1.5^ generics ^ To^ understand^ that^ generic
type^ information^ is^ erased^ by
the compiler^ and^ all^ instances^ of
a^ generic^ class^ share^ the^ same
runtime p^ g class^ file To know^ certain^ restrictions^ on^ generic
types^ caused^ by^ type^ erasure ^ To^ design^ and^ implement
generic^ matrix^ classes
2 docsity.com
^ You may be familiar with similar constructs from^ You^ may^ be^ familiarwith^ similar^ constructs
from other^ languages,^ most
notably^ C++^ templates.
List^ myIntList =^ new^ LinkedList();
//^1 y^
() myIntList.add(new^ Integer(0));
//^2 Integer^ x^ =^ (Integer)^ myIntList.iterator().next();
//^3 docsity.com
list.add("Java^ Programming");}} }^ To understand the compileTo
understand the compilewarning on this line, you need tolearn JDK 1.5 generics.
5 learn JDK 1.5 generics.
docsity.com
} }^ No compile warning on this line.
7 docsity.com
to^ parameterize^ types.
can^ define^ a^ class^ or
a method^ with^ generic
types^ that^ can^ be^ substituted^ b^ h^ l using^ concrete^ types
by^ the^ compiler. For example, you^ may^ define^ a^ generic^
stack^ class that stores the elements of a generic type Fromthat^ stores^ the^ elements
of^ a^ generic^ type.^ From this^ generic^ class,^ you^
may^ create^ a^ stack^ object for^ holding^ strings^ and
a^ stack^ object^ for^ holdingg g j^
g numbers.^ Here,^ strings
and^ numbers^ are^ concrete types^ that^ replace^ the
generic^ type.^8
docsity.com
package java.lang;
public interface Comaprable
Comparable
docsity.com
+ArrayList()
java.util.ArrayList
+ArrayList()+add(o: Object) : void+add(index: int, o: Object) : void+clear(): void
+ArrayList()+add(o: E) : void+add(index: int, o: E) : void+clear(): void +clear(): void+contains(o: Object): boolean+get(index: int) : Object+indexOf(o: Object) : int
+clear(): void+contains(o: Object): boolean+get(index: int) : E+indexOf(o: Object) : int +indexOf(o: Object) : int+isEmpty(): boolean+lastIndexOf(o: Object) : int+remove(o: Object): boolean
+indexOf(o: Object) : int+isEmpty(): boolean+lastIndexOf(o: Object) : int+remove(o: Object): boolean remove(o: Object): boolean+size(): int+remove(index: int) : boolean+set(index: int, o: Object) : Object
remove(o: Object): boolean+size(): int+remove(index: int) : boolean+set(index: int, o: E) : E^11 (^ ,^ j^ )^ j^
(^ ,^ ) (a) ArrayList before JDK 1.^
(b) ArrayList in JDK 1.5docsity.com
-list: java.util.ArrayList
An array list to store elements.Creates an empty stack. +getSize(): int+peek(): E+pop(): E
Returns the number of elements in this stack.Returns the top element in this stack.p p()Returns and removes the top element in this stack. +push(o: E): E+isEmpty(): boolean
p Adds a new element to the top of this stack.Returns true if the stack is empty.
13
docsity.com
print(E[]^ list)^ { for (int i = 0; i < list.length; i++)
<^ list.length;^ i^ ) System.out.print(list[i]^ +^ "^ ");System.out.println();}} public static void print(Object[] list) {public static void print(Object[]^ list)^ { for (int i = 0; i <^ list.length;^ i++) System.out.print(list[i]^ +^ "^ ");System.out.println();}
14 docsity.com