package com;
import java.util.*;
import java.text.*;
public class JavaTraining {
public static void main(String[] arguments)
{
System.out.println("-------------java.util.*----------------");
System.out.println("-------------Bitset----------------");
luckynumber call = new luckynumber();
if (arguments.length > 0) {
try {
int whichDay = Integer.parseInt(arguments[0]);
if (call.isHoliday(whichDay)) {
System.out.println("The number " + whichDay +
" is my lucky number.");
} else {
System.out.println("Day number " + whichDay +
" is not my lucky number.");
}
} catch (NumberFormatException nfe) {
System.out.println("Error: " + nfe.getMessage());
}
}
System.out.println("-------------Array----------------");
double array[] = new double[10];
for(int n = 0; n < 10; n++)
{
array[n] = .94F * n;
}
for(int j = 0; j < array.length; j++)
{
System.out.println(array[j] + " ");
}
Fruit apple = new Fruit("apple", 5, .81F);
Fruit orange = new Fruit("orange", 4, .93F);
Fruit banana = new Fruit("banana", 3, .66F);
System.out.println("-------------Vector----------------");
Vector cart = new Vector();
cart.add(apple);
cart.add(orange);
cart.add(banana);
Iterator it = cart.iterator();
while(it.hasNext())
{
Fruit fr = (Fruit)it.next();
System.out.println(fr.name + "," + (fr.quantity*fr.price));
}
System.out.println("-------------ArrayList----------------");
ArrayList myAL = new ArrayList();
myAL.add(apple);
myAL.add(orange);
myAL.add(banana);
Iterator IT = cart.iterator();
while(IT.hasNext())
{
Fruit fk = (Fruit)IT.next();
System.out.println(fk.name + "," +(fk.quantity*fk.price));
}
System.out.println("-------------Hashtable----------------");
Hashtable quality = new Hashtable();
Float price0 = new Float(5.00F);
quality.put("pristine mint", price0);
Float price1 = new Float(3.00F);
quality.put("mint", price1);
Float price2 = new Float(2.00F);
quality.put("near mint", price2);
Float price3 = new Float(1.50F);
quality.put("very fine", price3);
Float price4 = new Float(1.00F);
quality.put("fine", price4);
Float price5 = new Float(0.50F);
quality.put("good", price5);
Float price6 = new Float(0.25F);
quality.put("poor", price6);
Float price7 = new Float(0.10F);
quality.put("coverless", price7);
// set up collection
Comic[] comix = new Comic[5];
comix[0] = new Comic("Amazing Spider-Man", "1A", "very fine",
5400.00F);
comix[0].setPrice( (Float)quality.get(comix[0].condition) );
comix[1] = new Comic("Incredible Hulk", "181", "near mint",
770.00F);
comix[1].setPrice( (Float)quality.get(comix[1].condition) );
comix[2] = new Comic("Cerebus", "1A", "good", 260.00F);
comix[2].setPrice( (Float)quality.get(comix[2].condition) );
comix[3] = new Comic("Prez", "1", "pristine mint", 30.00F);
comix[3].setPrice( (Float)quality.get(comix[3].condition) );
comix[4] = new Comic("Night Nurse", "1", "coverless", 22.00F);
comix[4].setPrice( (Float)quality.get(comix[4].condition) );
for (int i = 0; i < comix.length; i++) {
System.out.println("Title: " + comix[i].title);
System.out.println("Issue: " + comix[i].issueNumber);
System.out.println("Condition: " + comix[i].condition);
System.out.println("Price: $" + comix[i].price + "\n");
}
// HashMap
System.out.println("-------------HashMap----------------");
Map m = new HashMap();
m.put("Jeddy Hawk", new Double(20.31));
m.put("John Doe", new Double(3654.23));
m.put("Martin Luther King", new Double(123.43));
m.put("Jessica York", new Double(1243.32));
m.put("Naron Mask", new Double(-99.32));
Set s = m.entrySet(); //use public Set entrySet() function
Iterator iter = s.iterator();
while(iter.hasNext()){
Map.Entry me = (Map.Entry)iter.next();
System.out.print(me.getKey() + " : ");
System.out.println(me.getValue());
}
System.out.println();
double balance = ((Double)m.get("John Doe")).doubleValue();
Object ob = m.put("John Doe", new Double(balance + 1000));
//put() will return the original object as follows
System.out.println(ob);
System.out.println("John Doe's new balance is : " + m.get("John Doe"));
System.out.println("-------------StringTokenizer----------------");
StringTokenizerDemo demo = new StringTokenizerDemo();
demo.StringTokenizerDemo1();
demo.StringTokenizerDemo2();
demo.StringTokenizerDemo3();
System.out.println("-------------Calendar----------------");
Calendar cal = Calendar.getInstance();
cal.set(2007,6,12);
int i = 0;
int j=0;
int k=1;
int lastd =0;
int row =0 ;
int w = 0;
int y = cal.get(Calendar.YEAR);
int M = cal.get(Calendar.MONTH);
int d = cal.get(Calendar.DATE);
// cal.set(y,m,d);
w = cal.get(Calendar.DAY_OF_WEEK)-1;
cal.set(y,M,1);
lastd = cal.getActualMaximum(Calendar.DATE);
System.out.println(y+"年"+(M+1)+"月最後一天"+lastd);
cal.set(y,M,d);
int lastd1 = cal.getActualMaximum(Calendar.DATE);
System.out.println(y+"年"+(M+1)+"月"+d+"號");
int a=0;
if((lastd+w)%7 == 0){ row = (lastd+w)/7; }else{row = (lastd+w)/7+1;}
System.out.println((M+1)+"月有"+row+"周");
Calendar[][] date = new Calendar[row][7];
//set
if(w==0){
for(int c=0;c<row;c++){
for(int g=0;g<7;g++){
if(k<=lastd) {date[c][g] = Calendar.getInstance();date[c][g].set(y,M,k++);}else{date[c][g] = Calendar.getInstance();date[c][g].set(y,M+1,g);} ;
}
}
}else{
for(a=0 ;a<w;a++){//第一列空格
date[0][a] = Calendar.getInstance();
date[0][a].set(y,M-1,lastd1-w+1);//lastd1-w ;
lastd1++;
}
for(int b=w;b<7;++b){
date[0][b] = Calendar.getInstance();
date[0][b].set(y,M,k++);
}
int f =0;
for(int c=1;c<row;c++){
for(int g=0;g<7;g++){
if(k<=lastd) {
date[c][g] = Calendar.getInstance();
date[c][g].set(y,M,k++);f=g;}else{date[c][g] = Calendar.getInstance();date[c][g].set(y,M+1,g-f);} ;
}
}
}
System.out.println(" ");
for(i=0 ;i<row;i++){
for(j=0;j<7;j++){
System.out.print(" "+(date [i][j].get(Calendar.MONTH)+1)+"/"+date [i][j].get(Calendar.DATE)+" ");
}
System.out.println("");
}
System.out.println("-------------Random----------------");
int max = 1000;
int min = 500;
int num = (int)((max-min)*Math.random()+min);
System.out.println("The number is from 500 to 1000 : " + num);
Random rand = new Random( );
int[ ] trio = new int[ 3 ];
System.out.println( "Three random integers in the range of [67, 295):" );
for( int p = 0; p < 3; ++p ) {
trio[ p ] = rand.nextInt( 295 - 67 ) + 67;
System.out.println( trio[ p ] );
}
Arrays.sort( trio );
System.out.println( "\nMedian:\n" + trio[ 1 ] );
System.out.println("-------------java.text.*----------------");
/*
* Format 是 DateFormat, MessageFormat, NumberFormat 的父类
*/
System.out.println("-------------DateFormat----------------");
Date dd = new Date();
String ss;
/** Date类的格式: Sat Apr 16 13:17:29 CST 2005 */
System.out.println(dd);
/** getDateInstance() */
/** 输出格式: 2005-4-16 */
ss = DateFormat.getDateInstance().format(dd);
System.out.println(ss);
/** 输出格式: 2005-4-16 */
ss = DateFormat.getDateInstance(DateFormat.DEFAULT).format(dd);
System.out.println(ss);
/** 输出格式: 2005年4月16日 星期六 */
ss = DateFormat.getDateInstance(DateFormat.FULL).format(dd);
System.out.println(ss);
/** 输出格式: 2005-4-16 */
ss = DateFormat.getDateInstance(DateFormat.MEDIUM).format(dd);
System.out.println(ss);
/** 输出格式: 05-4-16 */
ss = DateFormat.getDateInstance(DateFormat.SHORT).format(dd);
System.out.println(ss);
System.out.println("-------------NumberFormat----------------");
int amount = 54321;
NumberFormat myformat = NumberFormat.getInstance();//system format
System.out.println(myformat.format(amount));
NumberFormat usformat = NumberFormat.getIntegerInstance(Locale.US);//usa format
System.out.println(usformat.format(amount));
NumberFormat germanformat = NumberFormat.getInstance(Locale.CHINA);//china format
System.out.println(germanformat.format(amount));
System.out.println("-------------MessageFormat----------------");
/*
* MessageFormat类 用来辅助信息的格式化,MessageFormat接受一个字符串模式指定
* 对于文字信息中可能变动的部分,可以使用参数索引先占住文字位置,参数索引是{0}~{9}的非负整数,
* 之后在使用MessageFormat的静态方法format()时,可以提供真正的参数来填充参数索引处的信息.
*/
MessageFormat format = new MessageFormat("欢迎各位{0}来参加{1},{2}欢迎您");
String result = format.format(new Object[]{"朋友","奥运会","北京"});
System.out.println(result);
System.out.println("-------------SimpleDateFormat----------------");
/*
* SimpleDateFormat 允许格式化 (date -> text)、语法分析 (text -> date)和标准化。
* SimpleDateFormat 允许以为日期-时间格式化选择任何用户指定的方式启动。
* 但是,希望用 DateFormat 中的 getTimeInstance、 getDateInstance 或 getDateTimeInstance
* 创建一个日期-时间格式化程序。 每个类方法返回一个以缺省格式化方式初始化的日期/时间格式化程序。
* 可以根据需要用 applyPattern 方法修改格式化方式。
*/
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");
SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString()
SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
SimpleDateFormat myFmt4=new SimpleDateFormat(
"一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
Date now=new Date();
System.out.println(myFmt.format(now));
System.out.println(myFmt1.format(now));
System.out.println(myFmt2.format(now));
System.out.println(myFmt3.format(now));
System.out.println(myFmt4.format(now));
System.out.println(now.toGMTString());
System.out.println(now.toLocaleString());
System.out.println(now.toString());
System.out.println("-------------DecimalFormat----------------");
/*
* DecimalFormat是NumberFormat的一个子类,其实例被指定为特定的地区。
*/
double value=1234567.893d; // use double
long number = 32322355744L; //use long
DecimalFormat df = new DecimalFormat("###,###.##");
System.out.println(df.format(value));
System.out.println(df.format(number));
String data="1,234,567.89";
DecimalFormat form = new DecimalFormat("#,##0.0");
try
{
Number no = form.parse(data); //use number
System.out.println("result is "+no.doubleValue());
}
catch (ParseException ex)
{
ex.printStackTrace();
}
System.out.println("-------------java.lang.*----------------");
System.out.println("-------------byte,short,math----------------");
byte a1 = 10;
byte b1 = 20;
byte c1 = (byte)(a1 + b1);
System.out.println("this is byte :" + c1);
short a3 = 10;
short b3 = 20;
short c3 = (short)(a3 + b3);
System.out.println("this is short :" + c3);
int a4=10;
int b4=20;
int c4=Math.max(a4,b4);
System.out.println("this is math :" + c4);
System.out.println("-------------Runtime----------------");
Runtime rt = Runtime.getRuntime();
//Returns the total amount of memory in the Java virtual machine
System.out.println("total memory : "+rt.totalMemory());
//Returns the maximum amount of memory that the Java virtual machine will attempt to use.
System.out.println("used by jvm : "+rt.maxMemory());
// Returns the amount of free memory in the Java Virtual Machine.
System.out.println("free in jvm : "+rt.freeMemory());
System.out.println("-------------String and Stringbuffer----------------");
String tempstr = "abcdefghijklmnopqrstuvwxyz";
int times = 5000;
long lstart1=System.currentTimeMillis();
String str ="";
for(int i0=0;i0<times;i0++)
{
str+=tempstr;
}
long lend1=System.currentTimeMillis();
long time = (lend1-lstart1);
System.out.println("String have " + time + "times");
String tempstr1 = "abcdefghijklmnopqrstuvwxyz";
int times1 = 5000;
long lstart2=System.currentTimeMillis();
StringBuffer sb =new StringBuffer();
for(int i1=0;i1<times1;i1++)
{
sb.append(tempstr1);
}
long lend2=System.currentTimeMillis();
long time2 = (lend2-lstart2);
System.out.println("StringBuffer have " + time2 + "times");
System.out.println("StringBuffer 的速度几乎是String 上万倍.");
System.out.println("-------------Thread----------------");
ThreadA t1 = new ThreadA("first thread",50);
ThreadA t2 = new ThreadA("second thread ",100);
t1.start();
t2.start();
System.out.println("-------------Runnable----------------");
RunnableA r1 = new RunnableA("first Runnable",50);
RunnableA r2 = new RunnableA("second Runnable ",100);
Thread t11 = new Thread(r1);
Thread t22 = new Thread(r2);
t11.start();
t22.start();
}
}
class Fruit
{
String name; //use string
int quantity; //use int
float price; //use float
Fruit (String inName, int inQuantity, float inPrice)
{
name = inName;
quantity = inQuantity;
price = inPrice;
}
}
class Comic {
String title;
String issueNumber;
String condition;
float basePrice;
float price;
Comic(String inTitle, String inIssueNumber, String inCondition,
float inBasePrice) {
title = inTitle;
issueNumber = inIssueNumber;
condition = inCondition;
basePrice = inBasePrice;
}
void setPrice(Float factor) {
float multiplier = factor.floatValue();
price = basePrice * multiplier;
}
}
class StringTokenizerDemo {
private final String source1 = "name|number,Intrenet ID;email.address";
private final String source3 = "This file \"Java Training.java\" is in" +
" dir \"D:\" of \"JAVA\"";
private final String delim1 = ";,.|"; // 包含分号、逗号、句点、竖址分隔符
private final String delim2 = " \""; // 包含空格、引号
private final String delim3 = "\""; // 仅包含引号--------------------------------------------------------------------------------
private void printSeparater(String title) {
System.out.println("--------- " + title + " ---------");
}
/**
* 演示 StringTokenizer 最常用的用法,使用 StringTokenizer(String, String)
*/
public void StringTokenizerDemo1() {
this.printSeparater("StringTokenizerDemo1");
StringTokenizer tokenizer;
tokenizer = new StringTokenizer(source1, delim1);
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
}
/**
* 演示 StringTokenizer 的 nextToken(String) 用法
*/
public void StringTokenizerDemo2() {
this.printSeparater("StringTokenizerDemo2");
StringTokenizer tokenizer = new StringTokenizer(source1);
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken(delim1));
}
}
/**
* 一个较综合的示例,它拆分一句话,将每个单词拆分出来,其中,
* 被双引号包含的若干单词被处理为一个单词输出。
*/
public void StringTokenizerDemo3() {
this.printSeparater("StringTokenizerDemo3");
String delim = delim2;
String word;
boolean begin = false; // use boolean
StringTokenizer tokenizer = new StringTokenizer(source3, delim, true);
while (tokenizer.hasMoreTokens()) {
word = tokenizer.nextToken(delim);
if (word.equals("\"")) {
if (begin) {
delim = delim2;
begin = false;
} else {
delim = delim3;
begin = true;
}
} else if (!word.equals(" ")) {
System.out.println(word);
}
}
}
}
class luckynumber {
BitSet sked;
public luckynumber() {
sked = new BitSet(1000);
int[] luckynumber = { 1, 6, 8, 58, 777, 888, 999};
for (int i = 0; i < luckynumber.length; i++) {
addLuckynumber(luckynumber[i]);
}
}
public void addLuckynumber(int numberToAdd) {
sked.set(numberToAdd);
}
public boolean isHoliday(int numberToCheck) {
boolean result = sked.get(numberToCheck);
return result;
}
}
class ThreadA extends Thread {
int delay;
ThreadA(String s, int d) {
super(s);
delay = d;
}
public void run() {
try {
for (int i=0 ; i < 5 ; i++) {
sleep(delay);
System.out.println(getName()
+"第"+i+"圈");
}
} catch (InterruptedException e) {
System.err.println("异常 : "+e);
}
}
}
class RunnableA implements Runnable {
String name;
int delay;
RunnableA(String s, int d) {
name = s;
delay = d;
}
public void run() {
try {
for (int i=0 ; i < 5 ; i++) {
Thread.sleep(delay);
System.out.println(name
+"第"+i+"圈");
}
} catch (InterruptedException e) {
System.err.println("异常 : "+e);
}
}
}