Removing compile warnings
This commit is contained in:
parent
3e0098ae73
commit
3db17475cc
@ -2,6 +2,7 @@
|
||||
import java.util.*;
|
||||
|
||||
public class GenericVarargs {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> List<T> makeList(T... args) {
|
||||
List<T> result = new ArrayList<>();
|
||||
for(T item : args)
|
||||
|
@ -6,16 +6,16 @@ import java.util.*;
|
||||
public class CustomHandler {
|
||||
private static Logger logger =
|
||||
Logger.getLogger("CustomHandler");
|
||||
private static List strHolder = new ArrayList();
|
||||
private static List<String> trace = new ArrayList<>();
|
||||
public static void main(String[] args) {
|
||||
logger.addHandler(new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord logRecord) {
|
||||
strHolder.add(logRecord.getLevel() + ":");
|
||||
strHolder.add(logRecord.getSourceClassName()+":");
|
||||
strHolder.add(logRecord.getSourceMethodName()+":");
|
||||
strHolder.add("<" + logRecord.getMessage() + ">");
|
||||
strHolder.add("\n");
|
||||
trace.add(logRecord.getLevel() + ":");
|
||||
trace.add(logRecord.getSourceClassName()+":");
|
||||
trace.add(logRecord.getSourceMethodName()+":");
|
||||
trace.add("<" + logRecord.getMessage() + ">");
|
||||
trace.add("\n");
|
||||
}
|
||||
@Override
|
||||
public void flush() {}
|
||||
@ -24,6 +24,6 @@ public class CustomHandler {
|
||||
});
|
||||
logger.warning("Logging Warning");
|
||||
logger.info("Logging Info");
|
||||
System.out.print(strHolder);
|
||||
System.out.print(trace);
|
||||
}
|
||||
} ///:~
|
||||
|
@ -28,13 +28,14 @@ class IAm implements Command {
|
||||
|
||||
// A Command object that holds commands:
|
||||
class Macro implements Command {
|
||||
private ArrayList commands = new ArrayList();
|
||||
private ArrayList<Command> commands =
|
||||
new ArrayList<>();
|
||||
public void add(Command c) { commands.add(c); }
|
||||
@Override
|
||||
public void execute() {
|
||||
Iterator it = commands.iterator();
|
||||
Iterator<Command> it = commands.iterator();
|
||||
while(it.hasNext())
|
||||
((Command)it.next()).execute();
|
||||
it.next().execute();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,8 +7,8 @@ class Outcome {
|
||||
private int value;
|
||||
private Outcome(int val) { value = val; }
|
||||
public final static Outcome
|
||||
WIN = new Outcome(0),
|
||||
LOSE = new Outcome(1),
|
||||
WIN = new Outcome(0),
|
||||
LOSE = new Outcome(1),
|
||||
DRAW = new Outcome(2);
|
||||
@Override
|
||||
public String toString() {
|
||||
@ -119,13 +119,12 @@ class Compete {
|
||||
|
||||
public class PaperScissorsRock {
|
||||
public static void main(String args[]) {
|
||||
ArrayList items = new ArrayList();
|
||||
ArrayList<Item> items = new ArrayList<>();
|
||||
for(int i = 0; i < 40; i++)
|
||||
items.add(ItemFactory.newItem());
|
||||
for(int i = 0; i < items.size()/2; i++)
|
||||
System.out.println(
|
||||
Compete.match(
|
||||
(Item)items.get(i),
|
||||
(Item)items.get(i*2)));
|
||||
items.get(i), items.get(i*2)));
|
||||
}
|
||||
} ///:~
|
||||
|
@ -15,8 +15,9 @@ interface Shape {
|
||||
|
||||
abstract class ShapeFactory {
|
||||
protected abstract Shape create();
|
||||
static Map factories = new HashMap();
|
||||
static Shape createShape(String id)
|
||||
static Map<String, ShapeFactory> factories =
|
||||
new HashMap<>();
|
||||
static Shape createShape(String id)
|
||||
throws BadShapeCreation {
|
||||
if(!factories.containsKey(id)) {
|
||||
try {
|
||||
@ -28,23 +29,22 @@ abstract class ShapeFactory {
|
||||
if(!factories.containsKey(id))
|
||||
throw new BadShapeCreation(id);
|
||||
}
|
||||
return
|
||||
((ShapeFactory)factories.get(id)).create();
|
||||
return factories.get(id).create();
|
||||
}
|
||||
}
|
||||
|
||||
class Circle implements Shape {
|
||||
private Circle() {}
|
||||
public void draw() {
|
||||
System.out.println("Circle.draw");
|
||||
public void draw() {
|
||||
System.out.println("Circle.draw");
|
||||
}
|
||||
public void erase() {
|
||||
public void erase() {
|
||||
System.out.println("Circle.erase");
|
||||
}
|
||||
static class Factory extends ShapeFactory {
|
||||
@Override
|
||||
protected Shape create() {
|
||||
return new Circle();
|
||||
protected Shape create() {
|
||||
return new Circle();
|
||||
}
|
||||
}
|
||||
static {
|
||||
@ -54,17 +54,17 @@ class Circle implements Shape {
|
||||
}
|
||||
|
||||
class Square implements Shape {
|
||||
private Square() {}
|
||||
public void draw() {
|
||||
System.out.println("Square.draw");
|
||||
private Square() {}
|
||||
public void draw() {
|
||||
System.out.println("Square.draw");
|
||||
}
|
||||
public void erase() {
|
||||
System.out.println("Square.erase");
|
||||
public void erase() {
|
||||
System.out.println("Square.erase");
|
||||
}
|
||||
static class Factory extends ShapeFactory {
|
||||
@Override
|
||||
protected Shape create() {
|
||||
return new Square();
|
||||
protected Shape create() {
|
||||
return new Square();
|
||||
}
|
||||
}
|
||||
static {
|
||||
@ -75,9 +75,9 @@ class Square implements Shape {
|
||||
|
||||
public class ShapeFactory2 {
|
||||
public static void main(String args[]) {
|
||||
String shlist[] = { "Circle", "Square",
|
||||
String shlist[] = { "Circle", "Square",
|
||||
"Square", "Circle", "Circle", "Square" };
|
||||
ArrayList shapes = new ArrayList();
|
||||
ArrayList<Shape> shapes = new ArrayList<>();
|
||||
try {
|
||||
for (String shlist1 : shlist) {
|
||||
shapes.add(ShapeFactory.createShape(shlist1));
|
||||
@ -86,11 +86,11 @@ public class ShapeFactory2 {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
Iterator i = shapes.iterator();
|
||||
Iterator<Shape> i = shapes.iterator();
|
||||
while(i.hasNext()) {
|
||||
Shape s = (Shape)i.next();
|
||||
Shape s = i.next();
|
||||
s.draw();
|
||||
s.erase();
|
||||
}
|
||||
}
|
||||
}
|
||||
} ///:~
|
||||
|
@ -5,12 +5,12 @@ import patterns.trash.*;
|
||||
import java.util.*;
|
||||
|
||||
public abstract class TypedBin {
|
||||
ArrayList v = new ArrayList();
|
||||
ArrayList<Trash> v = new ArrayList<>();
|
||||
protected boolean addIt(Trash t) {
|
||||
v.add(t);
|
||||
return true;
|
||||
}
|
||||
public Iterator elements() {
|
||||
public Iterator<Trash> elements() {
|
||||
return v.iterator();
|
||||
}
|
||||
public boolean add(DDAluminum a) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
//: patterns/dynatrash/DynaTrash.java
|
||||
//: patterns/dynatrash/DynaTrash.java
|
||||
// Using a HashMap of ArrayLists and RTTI
|
||||
// to automatically sort trash into
|
||||
// vectors. This solution, despite the
|
||||
@ -8,43 +8,44 @@ import patterns.trash.*;
|
||||
import java.util.*;
|
||||
|
||||
// Generic TypeMap works in any situation:
|
||||
class TypeMap {
|
||||
private HashMap t = new HashMap();
|
||||
public void add(Object o) {
|
||||
class TypeMap<T> {
|
||||
private Map<Class,List<T>> t = new HashMap<>();
|
||||
public void add(T o) {
|
||||
Class type = o.getClass();
|
||||
if(t.containsKey(type))
|
||||
((ArrayList)t.get(type)).add(o);
|
||||
t.get(type).add(o);
|
||||
else {
|
||||
ArrayList v = new ArrayList();
|
||||
List<T> v = new ArrayList<>();
|
||||
v.add(o);
|
||||
t.put(type,v);
|
||||
}
|
||||
}
|
||||
public ArrayList get(Class type) {
|
||||
return (ArrayList)t.get(type);
|
||||
public List<T> get(Class type) {
|
||||
return t.get(type);
|
||||
}
|
||||
public Iterator keys() {
|
||||
return t.keySet().iterator();
|
||||
public Iterator<Class> keys() {
|
||||
return t.keySet().iterator();
|
||||
}
|
||||
}
|
||||
|
||||
// Adapter class to allow
|
||||
// callbacks from ParseTrash.fillBin():
|
||||
class TypeMapAdapter implements Fillable {
|
||||
TypeMap map;
|
||||
public TypeMapAdapter(TypeMap tm) { map = tm; }
|
||||
TypeMap<Trash> map;
|
||||
public TypeMapAdapter(TypeMap<Trash> tm) {
|
||||
map = tm;
|
||||
}
|
||||
@Override
|
||||
public void addTrash(Trash t) { map.add(t); }
|
||||
}
|
||||
|
||||
public class DynaTrash {
|
||||
public static void main(String[] args) {
|
||||
TypeMap bin = new TypeMap();
|
||||
ParseTrash.fillBin("Trash.dat",
|
||||
TypeMap<Trash> bin = new TypeMap<>();
|
||||
ParseTrash.fillBin("Trash.dat",
|
||||
new TypeMapAdapter(bin));
|
||||
Iterator keys = bin.keys();
|
||||
Iterator<Class> keys = bin.keys();
|
||||
while(keys.hasNext())
|
||||
Trash.sumValue(
|
||||
bin.get((Class)keys.next()));
|
||||
Trash.sumValue(bin.get(keys.next()));
|
||||
}
|
||||
} ///:~
|
||||
|
@ -12,7 +12,7 @@ class BadShapeCreation extends Exception {
|
||||
abstract class Shape {
|
||||
public abstract void draw();
|
||||
public abstract void erase();
|
||||
static Shape factory(String type)
|
||||
static Shape factory(String type)
|
||||
throws BadShapeCreation {
|
||||
if("Circle".equals(type)) return new Circle();
|
||||
if("Square".equals(type)) return new Square();
|
||||
@ -23,32 +23,32 @@ abstract class Shape {
|
||||
class Circle extends Shape {
|
||||
Circle() {} // Friendly constructor
|
||||
@Override
|
||||
public void draw() {
|
||||
System.out.println("Circle.draw");
|
||||
public void draw() {
|
||||
System.out.println("Circle.draw");
|
||||
}
|
||||
@Override
|
||||
public void erase() {
|
||||
System.out.println("Circle.erase");
|
||||
public void erase() {
|
||||
System.out.println("Circle.erase");
|
||||
}
|
||||
}
|
||||
|
||||
class Square extends Shape {
|
||||
Square() {} // Friendly constructor
|
||||
@Override
|
||||
public void draw() {
|
||||
System.out.println("Square.draw");
|
||||
public void draw() {
|
||||
System.out.println("Square.draw");
|
||||
}
|
||||
@Override
|
||||
public void erase() {
|
||||
System.out.println("Square.erase");
|
||||
public void erase() {
|
||||
System.out.println("Square.erase");
|
||||
}
|
||||
}
|
||||
|
||||
public class ShapeFactory1 {
|
||||
public static void main(String args[]) {
|
||||
String shlist[] = { "Circle", "Square",
|
||||
String shlist[] = { "Circle", "Square",
|
||||
"Square", "Circle", "Circle", "Square" };
|
||||
ArrayList shapes = new ArrayList();
|
||||
List<Shape> shapes = new ArrayList<>();
|
||||
try {
|
||||
for (String shlist1 : shlist) {
|
||||
shapes.add(Shape.factory(shlist1));
|
||||
@ -57,11 +57,11 @@ public class ShapeFactory1 {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
Iterator i = shapes.iterator();
|
||||
Iterator<Shape> i = shapes.iterator();
|
||||
while(i.hasNext()) {
|
||||
Shape s = (Shape)i.next();
|
||||
Shape s = i.next();
|
||||
s.draw();
|
||||
s.erase();
|
||||
}
|
||||
}
|
||||
}
|
||||
} ///:~
|
||||
|
@ -1,4 +1,4 @@
|
||||
//: patterns/recyclea/RecycleA.java
|
||||
//: patterns/recyclea/RecycleA.java
|
||||
// Recycling with RTTI.
|
||||
package patterns.recyclea;
|
||||
import java.util.*;
|
||||
@ -10,13 +10,13 @@ abstract class Trash {
|
||||
abstract double value();
|
||||
double weight() { return weight; }
|
||||
// Sums the value of Trash in a bin:
|
||||
static void sumValue(ArrayList bin) {
|
||||
Iterator e = bin.iterator();
|
||||
static void sumValue(List<Trash> bin) {
|
||||
Iterator<Trash> e = bin.iterator();
|
||||
double val = 0.0f;
|
||||
while(e.hasNext()) {
|
||||
// One kind of RTTI:
|
||||
// A dynamically-checked cast
|
||||
Trash t = (Trash)e.next();
|
||||
Trash t = e.next();
|
||||
// Polymorphism in action:
|
||||
val += t.weight() * t.value();
|
||||
System.out.println(
|
||||
@ -62,7 +62,7 @@ class Glass extends Trash {
|
||||
|
||||
public class RecycleA {
|
||||
public static void main(String[] args) {
|
||||
ArrayList bin = new ArrayList();
|
||||
List<Trash> bin = new ArrayList<>();
|
||||
// Fill up the Trash bin:
|
||||
for(int i = 0; i < 30; i++)
|
||||
switch((int)(Math.random() * 3)) {
|
||||
@ -78,14 +78,14 @@ public class RecycleA {
|
||||
bin.add(new
|
||||
Glass(Math.random() * 100));
|
||||
}
|
||||
ArrayList
|
||||
glassBin = new ArrayList(),
|
||||
paperBin = new ArrayList(),
|
||||
alBin = new ArrayList();
|
||||
Iterator sorter = bin.iterator();
|
||||
List<Trash>
|
||||
glassBin = new ArrayList<>(),
|
||||
paperBin = new ArrayList<>(),
|
||||
alBin = new ArrayList<>();
|
||||
Iterator<Trash> sorter = bin.iterator();
|
||||
// Sort the Trash:
|
||||
while(sorter.hasNext()) {
|
||||
Object t = sorter.next();
|
||||
Trash t = sorter.next();
|
||||
// RTTI to show class membership:
|
||||
if(t instanceof Aluminum)
|
||||
alBin.add(t);
|
||||
|
12
patterns/trash/FillableList.java
Normal file
12
patterns/trash/FillableList.java
Normal file
@ -0,0 +1,12 @@
|
||||
//: patterns/trash/FillableList.java
|
||||
// Adapter that makes an ArrayList Fillable.
|
||||
package patterns.trash;
|
||||
import java.util.*;
|
||||
|
||||
public class FillableList
|
||||
implements Fillable {
|
||||
private ArrayList v;
|
||||
public FillableList(ArrayList vv) { v = vv; }
|
||||
@Override
|
||||
public void addTrash(Trash t) { v.add(t); }
|
||||
} ///:~
|
@ -10,7 +10,7 @@ public class ParseTrash {
|
||||
fillBin(String filename, Fillable bin) {
|
||||
try {
|
||||
try (BufferedReader data = new BufferedReader(
|
||||
new FileReader(filename))) {
|
||||
new FileReader(filename))) {
|
||||
String buf;
|
||||
while((buf = data.readLine())!= null) {
|
||||
if(buf.trim().length() == 0)
|
||||
@ -20,9 +20,8 @@ public class ParseTrash {
|
||||
double weight = Double.valueOf(
|
||||
buf.substring(buf.indexOf(':') + 1)
|
||||
.trim());
|
||||
bin.addTrash(
|
||||
Trash.factory(
|
||||
new Trash.Info(type, weight)));
|
||||
bin.addTrash(Trash.factory(
|
||||
new Trash.Info(type, weight)));
|
||||
}
|
||||
}
|
||||
} catch(IOException e) {
|
||||
@ -36,6 +35,6 @@ public class ParseTrash {
|
||||
// Special case to handle ArrayList:
|
||||
public static void
|
||||
fillBin(String filename, ArrayList bin) {
|
||||
fillBin(filename, new FillableArrayList(bin));
|
||||
fillBin(filename, new FillableList(bin));
|
||||
}
|
||||
} ///:~
|
||||
|
@ -11,13 +11,13 @@ public abstract class Trash {
|
||||
public abstract double value();
|
||||
public double weight() { return weight; }
|
||||
// Sums the value of Trash in a bin:
|
||||
public static void sumValue(ArrayList bin) {
|
||||
Iterator e = bin.iterator();
|
||||
public static void sumValue(List<Trash> bin) {
|
||||
Iterator<Trash> e = bin.iterator();
|
||||
double val = 0.0f;
|
||||
while(e.hasNext()) {
|
||||
// One kind of RTTI:
|
||||
// A dynamically-checked cast
|
||||
Trash t = (Trash)e.next();
|
||||
Trash t = e.next();
|
||||
val += t.weight() * t.value();
|
||||
System.out.println(
|
||||
"weight of " +
|
||||
@ -34,15 +34,16 @@ public abstract class Trash {
|
||||
extends Exception {}
|
||||
public static class CannotCreateTrashException
|
||||
extends Exception {}
|
||||
private static ArrayList trashTypes =
|
||||
new ArrayList();
|
||||
public static Trash factory(Info info)
|
||||
throws PrototypeNotFoundException,
|
||||
private static List<Class> trashTypes =
|
||||
new ArrayList<>();
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Trash factory(Info info)
|
||||
throws PrototypeNotFoundException,
|
||||
CannotCreateTrashException {
|
||||
for (Object trashType : trashTypes) {
|
||||
for (Class trashType : trashTypes) {
|
||||
// Somehow determine the new type
|
||||
// to create, and create one:
|
||||
Class tc = (Class) trashType;
|
||||
Class tc = trashType;
|
||||
if (tc.getName().contains(info.id)) {
|
||||
try {
|
||||
// Get the dynamic constructor method
|
||||
@ -50,7 +51,7 @@ public abstract class Trash {
|
||||
Constructor ctor =
|
||||
tc.getConstructor(
|
||||
new Class[] {double.class});
|
||||
// Call the constructor to create a
|
||||
// Call the constructor to create a
|
||||
// new object:
|
||||
return (Trash)ctor.newInstance(
|
||||
new Object[]{info.data});
|
||||
@ -69,13 +70,12 @@ public abstract class Trash {
|
||||
// but it must be in your class path!
|
||||
try {
|
||||
System.out.println("Loading " + info.id);
|
||||
trashTypes.add(
|
||||
Class.forName(info.id));
|
||||
trashTypes.add(Class.forName(info.id));
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new PrototypeNotFoundException();
|
||||
}
|
||||
// Loaded successfully. Recursive call
|
||||
// Loaded successfully. Recursive call
|
||||
// should work this time:
|
||||
return factory(info);
|
||||
}
|
||||
|
@ -3,53 +3,53 @@
|
||||
package patterns.visitor;
|
||||
import java.util.*;
|
||||
|
||||
interface Visitor {
|
||||
interface Visitor {
|
||||
void visit(Gladiolus g);
|
||||
void visit(Renuculus r);
|
||||
void visit(Chrysanthemum c);
|
||||
}
|
||||
|
||||
// The Flower hierarchy cannot be changed:
|
||||
interface Flower {
|
||||
interface Flower {
|
||||
void accept(Visitor v);
|
||||
}
|
||||
|
||||
class Gladiolus implements Flower {
|
||||
class Gladiolus implements Flower {
|
||||
@Override
|
||||
public void accept(Visitor v) { v.visit(this);}
|
||||
}
|
||||
|
||||
class Renuculus implements Flower {
|
||||
class Renuculus implements Flower {
|
||||
@Override
|
||||
public void accept(Visitor v) { v.visit(this);}
|
||||
}
|
||||
|
||||
class Chrysanthemum implements Flower {
|
||||
class Chrysanthemum implements Flower {
|
||||
@Override
|
||||
public void accept(Visitor v) { v.visit(this);}
|
||||
}
|
||||
|
||||
// Add the ability to produce a string:
|
||||
class StringVal implements Visitor {
|
||||
String s;
|
||||
String s;
|
||||
@Override
|
||||
public String toString() { return s; }
|
||||
@Override
|
||||
public void visit(Gladiolus g) {
|
||||
s = "Gladiolus";
|
||||
public void visit(Gladiolus g) {
|
||||
s = "Gladiolus";
|
||||
}
|
||||
@Override
|
||||
public void visit(Renuculus r) {
|
||||
s = "Renuculus";
|
||||
public void visit(Renuculus r) {
|
||||
s = "Renuculus";
|
||||
}
|
||||
@Override
|
||||
public void visit(Chrysanthemum c) {
|
||||
s = "Chrysanthemum";
|
||||
public void visit(Chrysanthemum c) {
|
||||
s = "Chrysanthemum";
|
||||
}
|
||||
}
|
||||
|
||||
// Add the ability to do "Bee" activities:
|
||||
class Bee implements Visitor {
|
||||
class Bee implements Visitor {
|
||||
@Override
|
||||
public void visit(Gladiolus g) {
|
||||
System.out.println("Bee and Gladiolus");
|
||||
@ -77,21 +77,21 @@ class FlowerFactory {
|
||||
|
||||
public class BeeAndFlowers {
|
||||
public static void main(String args[]) {
|
||||
ArrayList flowers = new ArrayList();
|
||||
List<Flower> flowers = new ArrayList<>();
|
||||
for(int i = 0; i < 10; i++)
|
||||
flowers.add(FlowerFactory.newFlower());
|
||||
// It's almost as if I had added a function
|
||||
// to produce a Flower string representation:
|
||||
StringVal sval = new StringVal();
|
||||
Iterator it = flowers.iterator();
|
||||
Iterator<Flower> it = flowers.iterator();
|
||||
while(it.hasNext()) {
|
||||
((Flower)it.next()).accept(sval);
|
||||
it.next().accept(sval);
|
||||
System.out.println(sval);
|
||||
}
|
||||
// Perform "Bee" operation on all Flowers:
|
||||
Bee bee = new Bee();
|
||||
it = flowers.iterator();
|
||||
while(it.hasNext())
|
||||
((Flower)it.next()).accept(bee);
|
||||
it.next().accept(bee);
|
||||
}
|
||||
} ///:~
|
||||
|
@ -28,6 +28,7 @@ class Int3 extends Int2 {
|
||||
}
|
||||
|
||||
public class AddingClone {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void main(String[] args) {
|
||||
Int2 x = new Int2(10);
|
||||
Int2 x2 = (Int2)x.clone();
|
||||
|
@ -12,6 +12,7 @@ class Int {
|
||||
}
|
||||
|
||||
public class Cloning {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void main(String[] args) {
|
||||
ArrayList v = new ArrayList();
|
||||
for(int i = 0; i < 10; i++ )
|
||||
|
@ -115,6 +115,7 @@ public class CopyConstructor {
|
||||
System.out.println("In slice, f is a " +
|
||||
f.getClass().getName());
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void ripen2(Tomato t) {
|
||||
try {
|
||||
Class c = t.getClass();
|
||||
@ -128,10 +129,11 @@ public class CopyConstructor {
|
||||
InstantiationException |
|
||||
IllegalAccessException |
|
||||
IllegalArgumentException |
|
||||
InvocationTargetException e) {
|
||||
System.out.println(e);
|
||||
InvocationTargetException e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void slice2(Fruit f) {
|
||||
try {
|
||||
Class c = f.getClass();
|
||||
@ -139,13 +141,13 @@ public class CopyConstructor {
|
||||
Object obj = ct.newInstance(new Object[] { f });
|
||||
System.out.println("In slice2, f is a " +
|
||||
obj.getClass().getName());
|
||||
} catch(NoSuchMethodException |
|
||||
SecurityException |
|
||||
InstantiationException |
|
||||
IllegalAccessException |
|
||||
IllegalArgumentException |
|
||||
InvocationTargetException e) {
|
||||
System.out.println(e);
|
||||
} catch(NoSuchMethodException |
|
||||
SecurityException |
|
||||
InstantiationException |
|
||||
IllegalAccessException |
|
||||
IllegalArgumentException |
|
||||
InvocationTargetException e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
|
@ -4,7 +4,7 @@ import java.util.*;
|
||||
|
||||
public class ImmutableInteger {
|
||||
public static void main(String[] args) {
|
||||
List v = new ArrayList();
|
||||
List<Integer> v = new ArrayList<>();
|
||||
for(int i = 0; i < 10; i++)
|
||||
v.add(new Integer(i));
|
||||
// But how do you change the int inside the Integer?
|
||||
|
@ -14,12 +14,12 @@ class IntValue {
|
||||
|
||||
public class MutableInteger {
|
||||
public static void main(String[] args) {
|
||||
List v = new ArrayList();
|
||||
List<IntValue> v = new ArrayList<>();
|
||||
for(int i = 0; i < 10; i++)
|
||||
v.add(new IntValue(i));
|
||||
System.out.println(v);
|
||||
for(int i = 0; i < v.size(); i++)
|
||||
((IntValue)v.get(i)).increment();
|
||||
v.get(i).increment();
|
||||
System.out.println(v);
|
||||
}
|
||||
} ///:~
|
||||
|
@ -7,7 +7,7 @@ import org.junit.Assert.*;
|
||||
|
||||
// So we can see the list objects being created,
|
||||
// and keep track of when they are cleaned up:
|
||||
class CountedList extends ArrayList {
|
||||
class CountedList extends ArrayList<String> {
|
||||
private static int counter = 0;
|
||||
private int id = counter++;
|
||||
public CountedList() {
|
||||
@ -77,7 +77,7 @@ public class JUnitDemo {
|
||||
@Test
|
||||
public void testAddAll() {
|
||||
System.out.println("Running testAddAll()");
|
||||
list.addAll(Arrays.asList(new Object[] {
|
||||
list.addAll(Arrays.asList(new String[] {
|
||||
"An", "African", "Swallow"}));
|
||||
org.junit.Assert.assertEquals(list.size(), 6);
|
||||
compare(list, new String[] { "0", "1", "2",
|
||||
|
Loading…
x
Reference in New Issue
Block a user