2006年02月17日
下棋的时候,只能一步一步地走,想连续走两步是不可能的,除非你欺骗你的对手,瞒着他下手.这当然是相当不光彩的做法,并且既然你的对手没有看见,他也会知道,既然你的对手不知道,旁观者也会知道,裁判也会知道,所以如果你想赢,你只能一步一步地走,深思熟虑,比你的对手更聪明些.
说起来当然容易,就如足球评论员一样,在任何一场球中他都可以讲得头头是道,但你不能对他说:既然你洞察能力这么强,你下场去踢吧!任何不在场的旁观者,不管他有意还是无意,他都会产生对进入他感知事物的判断,你无法阻止它的发生,你只能阻止观旁者发表他的看法.这就是言论自由与否的问题了,不谈.
而细节化到朋友\同事之间的事情,则体现关相互之间的关系以及个人修养事情了.有人喜欢搬弄是非,有人喜欢守口如瓶,各自的处理风格不同,你不能说谁就是对的,谁就是不对的,你只能说谁就是好的,谁就是不好的,因为这也是你自己的判断与风格.
近来房子换室友的事情颇让人费神,我还为我的java费神呢,还为自己未能洞察的前途费神呢,这许多的因素混杂在一起,它仍然处于棋局之中,任谁有再大的本领,也不能超脱.想要生活得更好,只有深思熟虑,付出自己辛勤劳动与汗水,比你的对手更聪明一些,才更加有可能,能更快让生活如你的意.

//: c09:Statistics.java
// From ‘Thinking in Java, 2nd ed.’ by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Simple demonstration of HashMap.
import java.util.*;

// Modify class Counter, make it to be Comparable
class Counter implements Comparable {
  public int i = 1;
  public int key = 0;
  public Counter(int key) {
   this.key = key;
  }
  public int compareTo(Object o) {
   return i > ((Counter)o).i ? 1 : (i == ((Counter)o).i ? 0 : -1);
  }
  public String toString() {
    return "key:" + Integer.toString(key) + " value: " + Integer.toString(i) + "\n";
  }
}

class Statistics {
  public static void main(Map tm) {
    //TreeMap tm = new TreeMap();
    for(int i = 0; i < 10000; i++) {
      // Produce a number between 0 and 20:
      Integer r =
        new Integer((int)(Math.random() * 20));
      if(tm.containsKey(r))
        ((Counter)tm.get(r)).i++;
      else
        tm.put(r, new Counter(r.intValue()));
    }
  }
} ///:~

public class P25 {
 public static void main(String[] args) {
  TreeMap tm = new TreeMap();
  int reps = 5;
  if(args.length > 0)
   reps = Integer.parseInt(args[0]);
  // run test:
  for(int i = 0; i < reps; i++)
   Statistics.main(tm);
  // find out the biggest:
  Collection c = tm.values();
  ArrayList al = new ArrayList();
  Iterator it = c.iterator();
  while(it.hasNext())
   al.add(it.next());
  Collections.sort(al);
  System.out.println("It appears to be:\n" + al);
 }
}

2006年02月16日
出租住宅信息
户 型:
3 室 1 厅 1 卫 1 厨
类 型:
多层住宅
户型补充说明:
保留
房屋座向:
保留
楼层状况:
第6层 共9层
建筑年份:
9x
建筑面积:
使用面积:
100 平方米
租金价格:
380 元/月 (人民币)
单 价:
 
支付方式:
每月支付
房屋装修:
简单
住宅所在区:
海珠
小区名:
中山大学肿瘤医院宿舍
住宅地址:
新港东路76号[磨碟沙地铁口东100米处]
交通状况:
地铁[磨碟沙站]/76-139-203-298-229等多路公车
信息发布时间:
2006/02/16 08:50:52
有效时间:
2006/03/03 以前
联系人:
林剑锋
email:
asiabaa@163.com
联系电话:
13138642845
传 真:
腾讯QQ:602721
联系地址:
广州新港东路76号东梯603
邮 编:
510603
传呼机:
详细说明:
1.本住宅三房一厅.出租其中一房.
2.分均分摊房租/水/电/垃圾费,除电费外,平均每户380/元.
3.环境优美,安全系统高[派出所就在隔壁,保安24小时巡逻].
信息来源:
 个人

来源网址:http://house.gznet.com/house/chouse_succ.php?MARK=look&HouseID=1750454316

2006年02月15日

//: chapter 9 practice 24
import java.util.*;
import com.bruceeckel.util.*;

public class P24 {
 public static void main(String[] args) {
  HashSet hs = new HashSet();
  TreeSet ts = new TreeSet();
  Collections2.fill(hs, Collections2.countries, 10);
  Collections2.fill(hs, Collections2.countries.reset(), 10);
  Collections2.fill(hs, Collections2.countries.reset(), 10);
  System.out.println("hs = " + hs);
  Collections2.fill(ts, Collections2.countries, 15);
  Collections2.fill(ts, Collections2.countries.reset(), 15);
  Collections2.fill(ts, Collections2.countries.reset(), 15);
  System.out.println("ts = " + ts);
 }
}

//: chapter 9 practice 23
// Generate a map and a set, fill with all the countries that
// start with character ‘A’.
import java.util.*;
import com.bruceeckel.util.*;

// Modified from Collections2.StringGenerator
class AGenerator implements Generator {
 private String[][] d;
 private int position;
 private int index = -1;
 public AGenerator(String[][] data, int pos) {
  d = data;
  position = pos;
 }
 public Object next() {
  index = (index + 1) % d.length;
  // return a Country name if it start with ‘A’
  // else run next() again:
  char[] chars = d[index][position].toCharArray();
  if(chars[0] == ‘A’)
   return d[index][position];
  else
   return next();
 }
 public AGenerator reset() {
  index = -1;
  return this;
 }
}

// Modified from Collections2.StringPairGenerator
class APairGenerator implements MapGenerator {
 private int index = -1;
 private String[][] d;
 public APairGenerator(String[][] data) {
  d = data;
 }
 public Pair next() {
  index = (index + 1) % d.length;
  // return a Pair Object which contains a country name
  // start with ‘A’; else run next() again:
  char[] chars = d[index][0].toCharArray();
  if(chars[0] == ‘A’)
   return new Pair(d[index][0], d[index][1]);
  else
   return next();
 }
}

public class P23 {
 // return true if a string is start with ‘A’:
 public static boolean isX(String s, char X) {
  return (s.toCharArray())[0] == X;
 }
 // Create a two-dimensional String array
 static String[][] cc = new String[CountryCapitals.pairs.length][CountryCapitals.pairs.length];
 
 public static void main(String[] args) {
  // solution 1–
  // using arraycopy to initilize cc by CountryCapitals.pairs:
  System.arraycopy(CountryCapitals.pairs,0,cc,0,CountryCapitals.pairs.length);

  Map m = new HashMap();
  Set s = new HashSet();
  
  for(int i = 0; i < cc.length; i++) {
   if(isX(cc[i][0],’A'))
    s.add(cc[i][0]);
  }
  
  for(int i = 0; i < cc.length; i++) {
   if(isX(cc[i][0],’A'))
    m.put(cc[i][0], cc[i][1]);
  }
  System.out.println("solution 1–");
  System.out.println("s = " + s); 
  System.out.println("m = " + m);
  
  // solution 2–
  // using the tow new Generator above:
  HashSet hs = new HashSet();
  HashMap hm = new HashMap();
  
  Collections2.fill(hs, new AGenerator(CountryCapitals.pairs, 0),CountryCapitals.pairs.length);
  Collections2.fill(hm, new APairGenerator(CountryCapitals.pairs), CountryCapitals.pairs.length);
  System.out.println("solution 2–");
  System.out.println("hs = " + hs);
  System.out.println("hm = " + hm);
 }
}

2006年02月14日

//: chapter 9 practice 22
import java.util.*;

// class from Statistics.java
class Counter {
  int i = 1;
  public String toString() {
    return Integer.toString(i);
  }
}

public class P22 {
 private abstract static class Tester {
  String name;
  Tester(String name) { this.name = name; }
  abstract void test(Map m, int size, int reps);
 }
 private static Tester[] tests = {
  // test from Statistics.java
  new Tester("Statistics") {
   void test(Map m, int size, int reps) {
      for(int i = 0; i < reps; i++) {
        // Produce a number between 0 and 20:
        Integer r =
          new Integer((int)(Math.random() * size));
        if(m.containsKey(r))
          ((Counter)m.get(r)).i++;
        else
          m.put(r, new Counter());
      }
   }
  },
 };
 public static void test(Map m, int size, int reps) {
  System.out.println("Testing " + m.getClass().getName() + " size " + size);
  for(int i = 0; i < tests.length; i++) {
  System.out.print(tests[i].name);
  long t1 = System.currentTimeMillis();
  tests[i].test(m, size, reps);
  long t2 = System.currentTimeMillis();
  System.out.println(": " + ((double)(t2 – t1)/(double)size));
  }
 }
 public static void main(String[] args) {
  int reps = 50000;
  if(args.length > 0)
   reps = Integer.parseInt(args[0]);
  // Small:
  test(new TreeMap(), 10, reps);
  test(new HashMap(), 10, reps);
  // Midium:
  test(new TreeMap(), 100, reps);
  test(new HashMap(), 100, reps);
  // Large
  test(new TreeMap(), 1000, reps);
  test(new HashMap(), 1000, reps);
 }
}

2006年02月13日

//: chapter 9 practice 21 : Deque
import java.util.*;

class Deque {
 private LinkedList list = new LinkedList();
 public void putFirst(Object v) { list.addFirst(v); }
 public void putLast(Object v) { list.addLast(v); }
 public Object getFirst() {
  return list.removeFirst();
 }
 public Object getLast() {
  return list.removeLast();
 }
 public boolean isEmpty() {
  return list.isEmpty();
 }
}

public class P21 {
 public static void main(String[] args) {
  Deque deque = new Deque();
  for(int i = 0; i < 10; i++)
   deque.putFirst(Integer.toString(i*i));
  System.out.println(deque.getLast());
  System.out.println(deque.getFirst());
  while(!deque.isEmpty())
   System.out.println(deque.getLast());
 }
}

//: c07/P6.java : polymorphism
import java.util.*;

class Rodent { //啮齿目动物
 void bite(){}
 void burrow(){}
 void bray(){}
}
class Mouse extends Rodent { //家鼠
 void bite() { System.out.println("Mouse.bite()"); }
 void burrow() { System.out.println("Mouse.burrow()"); }
 void bray() { System.out.println("Mouse.bray()"); }
}
class Gerbil extends Rodent { //沙鼠
 void bite() { System.out.println("Gerbil.bite()"); }
 void burrow() { System.out.println("Gerbil.burrow()"); }
 void bray() { System.out.println("Gerbil.bray()"); } 
}
class Hamster extends Rodent { //仓鼠
 void bite() { System.out.println("Hamster.bite()"); }
 void burrow() { System.out.println("Hamster.burrow()"); }
 void bray() { System.out.println("Hamster.bray()"); } 
}
public class P6 {
 private ArrayList r = new ArrayList();
 P6() {
   System.out.println("P6 constructor");
   for(int i = 0; i < 10; i++)
    r.add(randRodent());
   }
 public static void act(Rodent r) {
  r.bite();
 }
 public static void actAll(ArrayList r) {
  //for(int i = 0; i < r.length; i++)
  Iterator it = r.iterator();
  while(it.hasNext())
   act((Rodent)it.next());
 }
 public static Rodent randRodent() { //返回随机类型的老鼠
  switch((int)(Math.random() * 3)) {
   default:
   case 0 : return new Mouse();
   case 1 : return new Gerbil();
   case 2 : return new Hamster();
  }
 }
 //Rodent[] r = new Rodent[10];
 public static void main(String[] args) {
  P6 p = new P6();
  p.actAll(p.r);
 }
}

//: chapter 9 practice 19
import java.util.*;

public class P19{
 public static void main(String[] args) {
  P19[] ps = new P19[10];
  for(int j = 0; j < ps.length; j++)
   ps[j] = new P19();
  ArrayList al = new ArrayList(Arrays.asList(ps));
  // List al2 = new ArrayList(al.subList(al.size()/4,al.size()/2));
  // If use the al2 upstair, then we can use "@" method to remove al2:
  List al2 = al.subList(al.size()/4,al.size()/2);
  System.out.println("al= " + al);
  System.out.println("al2= " + al2);
  // because al2 is just a part of reference of al,
  // so we just need to remove ifself.
  al2.removeAll(al2);
  //@ al.removeAll(al2);
  System.out.println("al – al2 = " + al);
 }
}

//: c09:InfiniteRecursion.java
// From ‘Thinking in Java, 2nd ed.’ by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Accidental recursion.
import java.util.*;

public class InfiniteRecursion {
  public String toString() {
    return " InfiniteRecursion address: "
    // just replace "this" to "super.toString()" will be OK
      + super.toString() + "\n";
  }
  public static void main(String[] args) {
    ArrayList v = new ArrayList();
    for(int i = 0; i < 10; i++)
      v.add(new InfiniteRecursion());
    System.out.println(v);
  }
} ///:~