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(); } }}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
int delay = 1000; //milliseconds ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { //...Perform a task... } }; new Timer(delay, taskPerformer).start();