读书人

java 核心技术(数组(杨辉三角)散

发布时间: 2012-11-07 09:56:10 作者: rapoo

java 核心技术(数组(杨辉三角),散列码,对象拷贝,枚举类型,定时器)
数组java中无多维数组,都是一维数组,多维数组可以看做是数组的数组。下面是一个利用数组打印类似杨辉三角中数字的例子

package ch3;/**   @version 1.20 2004-02-10   @author Cay Horstmann*///不规则数组,数组的列数不同,打印类似杨辉三角public class LotteryArray{   public static void main(String[] args)   {      final int NMAX = 10;      // allocate triangular array      int[][] odds = new int[NMAX + 1][];      for (int n = 0; n <= NMAX; n++)         odds[n] = new int[n + 1];      // fill triangular array      for (int n = 0; n < odds.length; n++)         for (int k = 0; k < odds[n].length; k++)         {            /*               compute binomial coefficient               n * (n - 1) * (n - 2) * . . . * (n - k + 1)               -------------------------------------------               1 * 2 * 3 * . . . * k            */            int lotteryOdds = 1;            for (int i = 1; i <= k; i++)               lotteryOdds = lotteryOdds * (n - i + 1) / i;            odds[n][k] = lotteryOdds;         }      // print triangular array      for (int[] row : odds)      {         for (int odd : row)            System.out.printf("%4d", odd);         System.out.println();      }   }}


输出:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
散列码:hashCode
java中的每个对象都继承了Object的hashCode方法,String类覆盖了Object的equals和以下是String类中的散列码方法:public int hashCode()返回此字符串的哈希码。String 对象的哈希码根据以下公式计算:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
使用 int 算法,这里 s[i] 是字符串的第 i 个字符,n 是字符串的长度,^ 表示求幂。(空字符串的哈希值为 0。) 覆盖:类 Object 中的 hashCode
使用Arrays的排序方法
package ch6;/**   @version 1.30 2004-02-27   @author Cay Horstmann*/import java.util.*;public class EmployeeSortTest{     public static void main(String[] args)   {        Employee[] staff = new Employee[3];      staff[0] = new Employee("Harry Hacker", 35000);      staff[1] = new Employee("Carl Cracker", 75000);      staff[2] = new Employee("Tony Tester", 38000);      Arrays.sort(staff); //Arrays的排序方法,排序对象必须实现Comparable接口      // print out information about all Employee objects      for (Employee e : staff)         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());   }}class Employee implements Comparable<Employee>{     public Employee(String n, double s)   {        name = n;      salary = s;   }   public String getName()   {        return name;   }   public double getSalary()   {        return salary;   }   public void raiseSalary(double byPercent)   {        double raise = salary * byPercent / 100;      salary += raise;   }   /**      Compares employees by salary      @param other another Employee object      @return a negative value if this employee has a lower      salary than otherObject, 0 if the salaries are the same,      a positive value otherwise   */         public int compareTo(Employee other) //如果实现的是Comparable<Employee>,就重写这个方法   {        if (salary < other.salary) return -1;      if (salary > other.salary) return 1;      return 0;   }   private String name;   private double salary;   //public int compareTo(Object arg0) {  //如果Employee类实现的是Comparable,就重写这个方法,二者只能取一//   Employee other = (Employee) arg0;//   if(this.salary<other.salary) return -1;//   if(this.salary>other.salary)return 1;//   return 0;//   //}}

枚举类型:
package ch5;/** @version 1.0 2004-05-24 @author Cay Horstmann */import java.util.*;enum Size {SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL"); //构造方法private Size(String abbreviation) {this.abbreviation = abbreviation;}public String getAbbreviation() {return abbreviation;}private String abbreviation;}public class EnumTest {public static void main(String[] args) {Scanner in = new Scanner(System.in);System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");String input = in.next().toUpperCase();Size size = Enum.valueOf(Size.class, input); // toString的逆方法System.out.println("size=" + size);System.out.println("abbreviation=" + size.getAbbreviation());if (size == Size.EXTRA_LARGE)System.out.println("Good job--you paid attention to the _.");Size[] values = Size.values(); // 得到枚举类型中的所有值for (Size z : values) {System.out.println(z.toString());}}}


对象拷贝:
package ch6;/**   @version 1.10 2002-07-01   @author Cay Horstmann*/import java.util.*;public class CloneTest{     public static void main(String[] args)   {        try      {         Employee original = new Employee("John Q. Public", 50000);          original.setHireDay(2000, 1, 1);         Employee copy = original.clone();         copy.raiseSalary(10);         copy.setHireDay(2002, 12, 31);         System.out.println("original=" + original);         System.out.println("copy=" + copy);      }      catch (CloneNotSupportedException e)       {          e.printStackTrace();       }   }}class Employee implements Cloneable  //需要克隆的对象,必须实现此接口,这是一个标记接口,无内容{     public Employee(String n, double s)   {        name = n;      salary = s;   }   public Employee clone() throws CloneNotSupportedException  //深拷贝   {      // call Object.clone()      Employee cloned = (Employee)super.clone();             // clone mutable fields      cloned.hireDay = (Date)hireDay.clone();            return cloned;   }   /**      Set the hire day to a given date      @param year the year of the hire day      @param month the month of the hire day      @param day the day of the hire day   */   public void setHireDay(int year, int month, int day)   {      hireDay = new GregorianCalendar(year, month - 1, day).getTime();   }   public void raiseSalary(double byPercent)   {        double raise = salary * byPercent / 100;      salary += raise;   }   public String toString()   {      return "Employee[name=" + name         + ",salary=" + salary         + ",hireDay=" + hireDay         + "]";   }   private String name;   private double salary;   private Date hireDay;}


定时器:
public class Timer extends Object implements Serializable

在指定时间间隔触发一个或多个 ActionEvent。一个示例用法是动画对象,它将 Timer 用作绘制其帧的触发器。

设置计时器的过程包括创建一个 Timer 对象,在该对象上注册一个或多个动作侦听器,以及使用 start 方法启动该计时器。例如,以下代码创建并启动一个每秒(该时间由 Timer 构造方法的第一个参数指定)触发一次动作事件的计时器。Timer 构造方法的第二个参数指定接收计时器动作事件的侦听器。

int delay = 1000; //milliseconds  ActionListener taskPerformer = new ActionListener() {      public void actionPerformed(ActionEvent evt) {          //...Perform a task...      }  };  new Timer(delay, taskPerformer).start();

构造 Timer 时要指定一个延迟参数和一个 ActionListener。延迟参数用于设置初始延迟和事件触发之间的延迟(以毫秒为单位)。启动了计时器后,它将在向已注册侦听器触发第一个 ActionEvent 之前等待初始延迟。第一个事件之后,每次超过事件间延迟时它都继续触发事件,直到被停止。
构造之后,可以单独更改初始延迟和事件间延迟,并且可以添加其他 ActionListener。
如果希望计时器只在第一次时触发然后停止,可以对计时器调用 setRepeats(false)。
在 1.3 版本中,向 Java 平台添加了另一个 Timer 类:java.util.Timer。该类和 javax.swing.Timer 的基本功能相同,但是 java.util.Timer 更常用,功能更多。javax.swing.Timer 有两个特征,它们可以让使用 GUI 更方便。首先,其事件处理程序都是 GUI 程序员所熟悉的,并且可以更简单地处理事件指派线程。第二,其自动线程共享意味着不必采取特殊步骤来避免生成过多线程。相反,计时器使用同一个线程让光标闪烁、使工具提示显示等等。

一个定时器可以定期执行多个action

读书人网 >编程

热点推荐