java编程思想第四版第十一章习题
- 第一题
package net.mindview.holding.test1; import java.util.ArrayList; import java.util.List; /** * 沙鼠 * @author samsung * */ public class Gerbil { static int counter; int gerbilNumber; public Gerbil(){ this.gerbilNumber = counter ++; } public String hop(){ return "沙鼠的号码:"+gerbilNumber + ",正在跳跃"; } public static void main(String[] args) { List<Gerbil> list = new ArrayList<Gerbil>(); list.add(new Gerbil()); list.add(new Gerbil()); list.add(new Gerbil()); list.add(new Gerbil()); for(Gerbil g: list){ System.out.println(g.hop()); } } }
运行结果
沙鼠的号码:0,正在跳跃 沙鼠的号码:1,正在跳跃 沙鼠的号码:2,正在跳跃 沙鼠的号码:3,正在跳跃
- 第二题
package net.mindview.holding.test2; import java.util.HashSet; import java.util.Set; public class SimpleCollection { public static void main(String[] args) { Set<Integer> c = new HashSet<Integer>(); for(int i = 0; i < 10; i++){ c.add(i); } for(Integer i: c){ System.out.print(i + ","); } } }
- 第三题
package net.mindview.holding.test3; import java.util.ArrayList; import java.util.List; /** * 选择器 */ interface Selector { //是否结束 boolean end(); //当前 Object current(); //下一个 void next(); } /** * 顺序 */ public class Sequence { //数组是有限制的,而集合时无限制的. //private Object[] items; private List<Object> items; private int next = 0; //定义数组的个数 public Sequence(){ items = new ArrayList<Object>(); } //添加 public void add(Object x){ items.add(x); } //内部类--顺序选择器 private class SequenceSelector implements Selector { //选择器最开始指向第一个元素 private int i = 0; @Override public boolean end() { return i == (items.size()); } /** * 新增方法: 在内部类中引用外部类 */ public Sequence outer(){ return Sequence.this; } @Override public Object current() { return items.get(i); } @Override public void next() { if(i<items.size()){ i++; } } } public Selector selector() { return new SequenceSelector(); } public static void main(String[] args) { Sequence s = new Sequence(); for(int i=0; i<10; i++){ s.add("对象"+i); } Selector selector = s.selector(); while(!selector.end()){ System.out.print(selector.current()); selector.next(); } } }
结果
对象0对象1对象2对象3对象4对象5对象6对象7对象8对象9
分析: 这个案例的重点是, 数组瘦受限制的, 集合是没有元素个数限制的。 因此,可以任意添加元素的意思就是:将数组替代为集合。
- 第四题
package net.mindview.holding.test4; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.TreeSet; class MoiveNameGenerator { String[] moives = {"Snow White", "Star Wars", "White Princple", "Dream","My Mother", "The Forest", "My Grandpa"}; int next; public String next(){ String moive = moives[next]; //电影名用完之后, 循环到这个字符列表的开始处 next = (next+1) % moives.length; return moive; } } public class MoiveGenerator { MoiveNameGenerator moiveNameGenerator = new MoiveNameGenerator(); public String[] fill(String[] arr){ for(int i=0; i<arr.length; i++){ arr[i] = moiveNameGenerator.next(); } return arr; } public Collection fill(Collection col){ for(int i=0; i<5; i++){ col.add(moiveNameGenerator.next()); } return col; } public static void main(String[] args) { MoiveGenerator moiveGenerator = new MoiveGenerator(); System.out.println(Arrays.toString(moiveGenerator.fill(new String[5]))); System.out.println(moiveGenerator.fill(new ArrayList<String>())); System.out.println(moiveGenerator.fill(new LinkedList<String>())); System.out.println(moiveGenerator.fill(new HashSet<String>())); System.out.println(moiveGenerator.fill(new LinkedHashSet<String>())); System.out.println(moiveGenerator.fill(new TreeSet<String>())); } }
运行结果:
[Snow White, Star Wars, White Princple, Dream, My Mother] [The Forest, My Grandpa, Snow White, Star Wars, White Princple] [Dream, My Mother, The Forest, My Grandpa, Snow White] [My Mother, Star Wars, The Forest, Dream, White Princple] [My Grandpa, Snow White, Star Wars, White Princple, Dream] [My Grandpa, My Mother, Snow White, Star Wars, The Forest]
- dff
- af
- a
- fa
- fda
- sf
- af
- as
- fa
- fa
-
栈在编程语言中,经常用来对表达式求值。请使用net.mindview.util.Stack对下面的表达式求值。 其中“+”表示将后面的字母压入栈,“-”表示将栈顶的字母押出栈。并打印他们。
package net.mindview.holding; import java.util.LinkedList; /** * 模拟栈 */ public class Stack<T> { private LinkedList<T> storage = new LinkedList<T>(); //推入 public void push(T v){ storage.addFirst(v); } public T peek(){ return storage.getFirst(); } //取出 public T pop(){ return storage.removeFirst(); } public boolean empty(){ return storage.isEmpty(); } public String toString(){ return storage.toString(); } }
package net.mindview.holding.test15; import java.util.Stack; /** * 栈在编程语言中,经常用来对表达式求值。请使用net.mindview.util.Stack对下面的表达式求值。 * 其中“+”表示将后面的字母压入栈,“-”表示将栈顶的字母押出栈。并打印他们。 * @author samsung * */ public class Test15 { Stack<Character> stac = new Stack<Character>(); //计算 public void eval(String str){ char[] chars = str.toCharArray(); for(int i=0; i<chars.length; i++){ char c = chars[i++]; //遇到+号把字符入栈 switch (c){ case '+': System.out.println("入栈:"+stac.push(chars[i])); break; case '-': System.out.println("出栈:"+stac.pop()); break; } } } public static void main(String[] args) { Test15 t = new Test15(); String str = "+U+n+c---+e+r+t---+a-+i-+n+t+y---+ -+r+u--+l+e+s--"; t.eval(str); } }
- fda
- fa
- fa
- dfa
- fa
- df
- asfd
- afa
- fa
- fa
- fa
- 二十七题
package net.mindview.holding.test27; import java.util.LinkedList; import java.util.Queue; //消息 class Command { String str; public Command(String str){ this.str = str; } public void operation(){ System.out.println(str); } } //生产者 class Producter{ //生产者生成消息 public Queue<Command> produce(Queue<Command> qc){ qc.offer(new Command("A")); qc.offer(new Command("AB")); qc.offer(new Command("ABC")); qc.offer(new Command("VFA")); qc.offer(new Command("SDA")); return qc; } } //消费者 class Customer { //消费者消费消息 public void custom(Queue<Command> queue){ while(queue.peek() != null){ queue.remove().operation(); } } } public class Test27 { public static void main(String[] args) { Queue<Command> qc = new LinkedList<Command>(); Producter producter = new Producter(); Queue<Command> qcc = producter.produce(qc); new Customer().custom(qcc); } }
运行结果:
A AB ABC VFA SDA
- fa
- 第三十一题
package net.mindview.holding.test31; import java.util.Iterator; import java.util.Random; //形状 class Shape { public void draw() { } public void erase() { } } // 圆形 class Cycle1 extends Shape { @Override public void draw() { System.out.println("draw cycle"); } @Override public void erase() { System.out.println("erase cycle"); } } class Square extends Shape { @Override public void draw() { System.out.println("draw Square"); } @Override public void erase() { System.out.println("draw Square"); } } // 矩形 class Triangle extends Shape { @Override public void draw() { System.out.println("draw Triangle"); } @Override public void erase() { System.out.println("draw Triangle"); } } // 梯形 class Trapezoid extends Shape { @Override public void draw() { System.out.println("draw Trapezoid"); } @Override public void erase() { System.out.println("draw Trapezoid"); } } class RandomShapeFactory implements Iterable<Shape> { Random random = new Random(47); private final int quantity; public RandomShapeFactory(int quantity) { this.quantity= quantity; } // 下面画什么形状呢 public Shape nextShape() { switch (random.nextInt(4)) { default: case 0: return new Cycle1(); case 1: return new Square(); case 2: return new Triangle(); case 3: return new Trapezoid(); } } @Override public Iterator<Shape> iterator() { return new Iterator<Shape>() { private int count = 0; @Override public boolean hasNext() { return count < quantity; } @Override public Shape next() { ++count; return nextShape(); } @Override public void remove() { } }; } } public class Shapes { public static void main(String[] args) { RandomShapeFactory rd = new RandomShapeFactory(9); for (Shape s : rd) { System.out.println(s.getClass().getSimpleName()); } } }
运行结果
Triangle Square Triangle Cycle1 Cycle1 Triangle Cycle1 Square Triangle
- f
- afda
![](https://img2020.cnblogs.com/blog/1187916/202106/1187916-20210627082053046-596566365.jpg)