//: 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);
}
}
|
出租住宅信息
|
|||
|
户 型:
|
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
//: 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);
}
}
//: 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);
}
}
//: 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);
}
} ///:~