2006年02月09日

//: chapter 9 practice 17 : Iterator 2
import java.util.*;
import com.bruceeckel.util.*;

public class P17 {
 public static void writeHC(Collection c) {
  Iterator it = c.iterator();

  while(it.hasNext())
   System.out.println(it.next().hashCode());
  System.out.println("——————–");
 }
 public static void main(String[] args){
  ArrayList al = new ArrayList();
  LinkedList ll = new LinkedList();
  HashSet hs = new HashSet();
  TreeSet ts = new TreeSet();
  Generator gen = new Arrays2.RandStringGenerator(5);
  Collections2.fill(al, gen, 10);
  Collections2.fill(ll, gen, 10);
  Collections2.fill(hs, gen, 10);
  Collections2.fill(ts, gen, 10);
  writeHC(al);
  writeHC(ll);
  writeHC(hs);
  writeHC(ts);
 }
}

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

public class P16 {
 public static void main(String[] args) {
  ArrayList al = new ArrayList();
  LinkedList ll = new LinkedList();
  // fill:
  Collections2.fill(al, Collections2.capitals, 20);
  Collections2.fill(ll, Collections2.capitals, 20);
  System.out.println("——————– List one —————-");
  Iterator itc = al.iterator();
  while(itc.hasNext())
   System.out.println(itc.next());
  System.out.println("——————– List two —————-");
  Iterator itl = ll.iterator();
  while(itl.hasNext())
   System.out.println(itl.next());
  System.out.println("—————- After add together ———-");
  ListIterator lit = al.listIterator();
  // the very point you want to add in:
  int pos = 5;
  // get position from command line.
  if(args.length != 0)
   pos = Integer.parseInt(args[0]);
  if((pos < 0) || (pos > 20)){
   pos = 5;
   System.out.println("error position, use default value 5:");
  }
  while(lit.nextIndex() < pos){
   //System.out.println(lit.nextIndex());
   lit.next();
  }
   
  // normal iterator object cannot roll back,
  // you must reset it if you want to use it again:
  itl = ll.iterator();  
  // add the LinkedList to the ArrayList: 
  while(itl.hasNext())
   lit.add(itl.next());
  // the end of the first is at
  // pos + ll.size() , in the new al:
  while(lit.hasNext()){
   if(lit.nextIndex() == pos + ll.size())
    lit.add("the very point");
   lit.next();
  }
  // ListIterator can roll back:
  while(lit.hasPrevious())  
   lit.previous();
  while(lit.hasNext())
   System.out.println(lit.next());
 }
}

//: chapter 9 practice 15 : TreeSet and Alphabetic sorting.
import java.util.*;
import com.bruceeckel.util.*;

public class P15 {
 // TreeSet is a tactic Container, Comparator Must be
 // added by constructor.
 private static TreeSet ts = new TreeSet(new AlphabeticComparator());
 public static void main(String[] args){
  Collections2.fill(ts, new Arrays2.RandStringGenerator(8), 10);
  System.out.println(ts);
 }
}

//: chater 9 practice 13
import java.util.*;
import com.bruceeckel.util.*;

class P13a implements Comparable {
 String str1;
 String str2;
 P13a(Pair p) {
  str1 = p.key.toString();
  str2 = p.value.toString();
 }
 public int compareTo(Object o) {
  return str1.toLowerCase().compareTo(((P13a)o).str1.toLowerCase());
 }
 public String toString(){
  return str1 + " " + str2;
 }
}

class P13Comparator1 implements Comparator {
 public int compare(Object o1, Object o2) {
  String s1 = ((P13a)o1).str2;
  String s2 = ((P13a)o2).str2;
  // use toLowerCase method to sort abort Cases.
  return s1.toLowerCase().compareTo(s2.toLowerCase());
 }
}

public class P14 {
 private P13a[] a1 = new P13a[20];
 private ArrayList al = new ArrayList();
 public static void main(String[] args) {
  P14 p = new P14();
  Collections2.StringPairGenerator gen = Collections2.geography;
  for(int i = 0; i < p.a1.length; i++){
   Pair p1 = gen.next();
   p.a1[i] = new P13a(p1);
   p.al.add(new P13a(p1));
  }
  Arrays2.print(p.a1);
  System.out.println(p.al);
  Comparator cpr = new P13Comparator1();
  Arrays.sort(p.a1, cpr);
  Arrays2.print(p.a1);
  // need the very comparator which you used to sort,
  // otherwise you will got a wrong result when you
  // using binarySearch().
  System.out.println(Arrays.binarySearch(p.a1,p.a1[5], cpr));
  
  Collections.sort(p.al, new P13Comparator1());
  System.out.println(p.al);
  System.out.println(Collections.binarySearch(p.al,p.al.get(8), cpr));
 }
}

2006年02月08日

http://web.nhome.cn/

测试过,可以注册,可以使用web上传文件,免费空间10M,不过打开页面速有广告,IE可以拦截到,估计放简单的个人主页还是可以的,只要你不怎么在乎弹出广告.

据网站上的公告介绍,本来还提供ftp上传服务的,但由于有人上传"有害程序",所以封了.

//: chater 9 practice 13
import java.util.*;
import com.bruceeckel.util.*;

class P13a implements Comparable {
 String str1;
 String str2;
 P13a(Pair p) {
  str1 = p.key.toString();
  str2 = p.value.toString();
 }
 public int compareTo(Object o) {
  return str1.compareTo(((P13a)o).str1);
 }
 public String toString(){
  return str1 + str2;
 }
}

class P13Comparator1 implements Comparator {
 public int compare(Object o1, Object o2) {
  String s1 = ((P13a)o1).str2;
  String s2 = ((P13a)o2).str2;
  return s1.compareTo(s2);
 }
}

public class P13 {
 private P13a[] a1 = new P13a[10];
 private ArrayList al = new ArrayList();
 public static void main(String[] args) {
  P13 p = new P13();
  Collections2.StringPairGenerator gen = Collections2.geography;
  for(int i = 0; i < p.a1.length; i++){
   Pair p1 = gen.next();
   p.a1[i] = new P13a(p1);
   p.al.add(new P13a(p1));
  }
  Arrays2.print(p.a1);
  System.out.println(p.al);
  Comparator cpr = new P13Comparator1();
  Arrays.sort(p.a1, cpr);
  Arrays2.print(p.a1);
  // need the very comparator which you used to sort,
  // otherwise you will got a wrong result when you
  // using binarySearch().
  System.out.println(Arrays.binarySearch(p.a1,p.a1[5], cpr));
  
  Collections.sort(p.al, new P13Comparator1());
  System.out.println(p.al);
  System.out.println(Collections.binarySearch(p.al,p.al.get(8), cpr));
 }
}

2006年02月07日

// chater 9 practice 12 : Crate a generator of movie roles.
import java.util.*;
import com.bruceeckel.util.*;

public class P12 {
 private String[] a = new String[10];
 private ArrayList al = new ArrayList();
 private LinkedList ll = new LinkedList();
 private HashSet hs = new HashSet();
 private TreeSet ts = new TreeSet();
 public static final String[] roles = {
  "俞秀莲", "李慕白", "玉娇龙", "碧眼狐狸",
 };
 public static class RoleGenerator implements Generator {
  private String[] d;
  private int index = -1;
  RoleGenerator(String[] data) {
   d = data;
  }
  public Object next() {
   index = (index + 1) % d.length;
   return d[index];
  }
  public RoleGenerator reset() {
   index = -1;
   return this;
  }
 }
 public static void main(String[] args) {
  P12 p = new P12();
  RoleGenerator gen = new P12.RoleGenerator(P12.roles);
  Arrays2.fill(p.a, gen);
  Arrays2.print(p.a);
  Collections2.fill(p.al, gen.reset(), 10);
  System.out.println(p.al);
  Collections2.fill(p.ll, gen.reset(), 10);
  System.out.println(p.ll);
  // Sets don’t accept repeated elements,
  // so it will hold 4 elemnts at last.
  Collections2.fill(p.hs, gen.reset(), 10);
  System.out.println(p.hs);
  Collections2.fill(p.ts, gen.reset(), 10);
  System.out.println(p.ts);
 }
}

//: chapter 9 practice 11 : create arrays of primitives
// and String, and fill them and print them.
import java.util.*;
import com.bruceeckel.util.*;

public class P11 {
 static int size = 10;
 static char[] cs = new char[size];
 static byte[] bs = new byte[size];
 static boolean[] os = new boolean[size];
 static short[] shs = new short[size];
 static int[] is = new int[size];
 static long[] ls = new long[size];
 static double[] ds = new double[size];
 static float[] fs = new float[size];
 static String[] ss = new String[size];
 public static void main(String[] args) {
  Arrays2.fill(cs, new Arrays2.RandCharGenerator());
  Arrays2.print(cs);
  Arrays2.fill(bs, new Arrays2.RandByteGenerator());
  Arrays2.print(bs);
  Arrays2.fill(os, new Arrays2.RandBooleanGenerator());
  Arrays2.print(os);
  Arrays2.fill(shs, new Arrays2.RandShortGenerator());
  Arrays2.print(shs);
  Arrays2.fill(is, new Arrays2.RandIntGenerator());
  Arrays2.print(is); 
  Arrays2.fill(ls, new Arrays2.RandLongGenerator());
  Arrays2.print(ls); 
  Arrays2.fill(ds, new Arrays2.RandDoubleGenerator());
  Arrays2.print(ds);
  Arrays2.fill(fs, new Arrays2.RandFloatGenerator());
  Arrays2.print(fs); 
  Arrays2.fill(ss, new Arrays2.RandStringGenerator(5));
  Arrays2.print(ss);      
 }
}

//: chapter 9 practice 10 : Create an int container and
// compare to ArrayList with Integer.
import java.util.*;

class intList {
 private double useRate = 0.75;
 private int useNum = 0;
 private int size = 100;
 private int[] sl1 = new int[size]; 
 public void add(int index, int string) {
  // 如果位置超出,则重建array
  if(index > size) {
   size = index * 2;
   sl1 = reSize(sl1, size);   
  }
  sl1[index] = string;
  useNum = useNum + 1;
  if(useNum / size > useRate){
   size = useNum * 2;
   sl1 = reSize(sl1, size);
  }
 }
 public int add(int string) {
  int index = 0;
  for(int i = 0; i < sl1.length; i++) {
   if(sl1[i] == 0)
    sl1[i] = string;
    useNum = useNum + 1;
    if(useNum / size > useRate){
     size = useNum * 2;
     sl1 = reSize(sl1, size);
    }
    index = i;
  }
  return index;
 }
 public static int[] reSize(int[] a1, int size) {
  int[] a2 = new int[size];
  for(int i = 0; i < a1.length; i++)
   a2[i] = a1[i];
  a1 = new int[size];
  for(int j = 0; j < a2.length; j++)
   a1[j] = a2[j];
  return a1;
 }
 public int get(int index) {
  return sl1[index];
 }
 public int size(){
  return size;
 }
}

public class P10 {
 private abstract static class Tester {
  String name;
  Tester(String name) { this.name = name; }
  abstract void test(intList sl, int size, int reps);
  abstract void test(ArrayList al, int size, int reps);
 }
 private static Tester[] tests = {
  new Tester("add") {
   void test(intList sl, int size, int reps) {
    for(int i = 0; i < reps; i++)
     for(int j = 0; j < size; j++)
      sl.add(j, j*j);
   }
   void test(ArrayList al, int size, int reps) {
    for(int i = 0; i < reps; i++)
     for(int j = 0; j < size; j++)
      al.add(j, new Integer(j*j));
   }
  },
  new Tester("get") {
   void test(intList sl, int size, int reps) {
    for(int i = 0; i < reps; i++)
     for(int j = 0; j < size; j++)
      sl.get(j);
   }
   void test(ArrayList al, int size, int reps) {
    for(int i = 0; i < reps; i++)
     for(int j = 0; j < size; j++)
      al.get(j);
   }
  },
  // ‘Increamenting that’s reset the value of the very
  // position of the container.
  new Tester("self add") {
   void test(intList sl, int size, int reps) {
    for(int i = 0; i < reps; i++)
     for(int j = 0; j < size; j++)
      sl.add(j,sl.get(j)+1);
   }
   void test(ArrayList al, int size, int reps) {
    for(int i = 0; i < reps; i++)
     for(int j = 0; j < reps; j++)
      al.add(j, new Integer(((Integer)al.get(j)).intValue()+1));
   }
  }
 };

 
 public static void test(intList sl, int size, int reps) {
  System.out.println("Testing " + sl.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(sl,size,reps);
   long t2 = System.currentTimeMillis();
   System.out.println(": " + (double)(t2 – t1)/(double)size);
  }
 }

 public static void test(ArrayList al, int size, int reps) {
  System.out.println("Testing " + al.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(al,size,reps);
   long t2 = System.currentTimeMillis();
   System.out.println(": " + (double)(t2 – t1)/(double)size);
  }
 }
 
 public static void main(String[] args) {
  int reps = 1000;
  if(args.length > 0)
   reps = Integer.parseInt(args[0]);
  // small size:
  test(new intList(), 10, reps);
  test(new ArrayList(), 10, reps);
  
  // middle size:
  test(new intList(), 100, reps);
  test(new ArrayList(), 100, reps);

  // lardge size:
  test(new intList(), 1000, reps);
  test(new ArrayList(), 1000, reps);  
 }
}

2006年02月05日

撰写一个容器,把String array 封装于内,只且只能安插或取出Strings… 依题照目的思路,实测发现性能远胜于ArrayList容器,但不知道使用重载版本的测试程序有没有影响?源代码如下:

import java.util.*;

class StringList {
 private double useRate = 0.75;
 private int useNum = 0;
 private int size = 100;
 private String[] sl1 = new String[size]; 
 private String[] sl2;
 public void add(int index, String string) {
  // 如果位置超出,则重建array
  if(index > size) {
   size = index * 2;
   sl1 = reSize(sl1, size);   
  }
  sl1[index] = string;
  useNum = useNum + 1;
  if(useNum / size > useRate){
   size = useNum * 2;
   sl1 = reSize(sl1, size);
  }
 }
 public int add(String string) {
  int index = 0;
  for(int i = 0; i < sl1.length; i++) {
   if(sl1[i] == null)
    sl1[i] = string;
    useNum = useNum + 1;
    if(useNum / size > useRate){
     size = useNum * 2;
     sl1 = reSize(sl1, size);
    }
    index = i;
  }
  return index;
 }
 public static String[] reSize(String[] a1, int size) {
  String[] a2 = new String[size];
  for(int i = 0; i < a1.length; i++)
   a2[i] = a1[i];
  a1 = new String[size];
  for(int j = 0; j < a2.length; j++)
   a1[j] = a2[j];
  return a1;
 }
 public String get(int index) {
  return sl1[index];
 }
 public int size(){
  return size;
 }
}

public class P9 {
 private abstract static class Tester {
  String name;
  Tester(String name) { this.name = name; }
  abstract void test(StringList sl, int size, int reps);
  abstract void test(ArrayList al, int size, int reps);
 }
 private static Tester[] tests = {
  new Tester("add") {
   void test(StringList sl, int size, int reps) {
    for(int i = 0; i < reps; i++)
     for(int j = 0; j < size; j++)
      sl.add(j, Integer.toString(j));
   }
   void test(ArrayList al, int size, int reps) {
    for(int i = 0; i < reps; i++)
     for(int j = 0; j < size; j++)
      al.add(j, Integer.toString(j));
   }
  },
  new Tester("get") {
   void test(StringList sl, int size, int reps) {
    for(int i = 0; i < reps; i++)
     for(int j = 0; j < size; j++)
      sl.get(j);
   }
   void test(ArrayList al, int size, int reps) {
    for(int i = 0; i < reps; i++)
     for(int j = 0; j < size; j++)
      al.get(j);
   }
  },
 };

 
 public static void test(StringList sl, int size, int reps) {
  System.out.println("Testing " + sl.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(sl,size,reps);
   long t2 = System.currentTimeMillis();
   System.out.println(": " + (double)(t2 – t1)/(double)size);
  }
 }

 public static void test(ArrayList al, int size, int reps) {
  System.out.println("Testing " + al.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(al,size,reps);
   long t2 = System.currentTimeMillis();
   System.out.println(": " + (double)(t2 – t1)/(double)size);
  }
 }
 
 public static void main(String[] args) {
  int reps = 1000;
  if(args.length > 0)
   reps = Integer.parseInt(args[0]);
  // small size:
  test(new StringList(), 10, reps);
  test(new ArrayList(), 10, reps);
  
  // middle size:
  test(new StringList(), 100, reps);
  test(new ArrayList(), 100, reps);

  // lardge size:
  test(new StringList(), 1000, reps);
  test(new ArrayList(), 1000, reps);  
 }
}