最近總是太過浮躁,學習的勁頭越來越弱,又太懶,無意間翻出兩年前寫過的筆記,就貼過來吧。有點亂,面試題會經常遇到吧...
public class ReadLine {
public static void main(String[] args){
byte [] buf = new byte[1024];
String strInfo = null;
int pos = 0;
int ch = 0;
while(true)
{
ch = System.in.read();
switch(ch)
{
case '\r':
break;
case '\n':
strInfo = new String(buf,0,pos);
if(strInfo.equals("bye"))
{
return;
}
else
{
System.out.println(strInfo);
pos = 0;
break;
}
default:
buf[pos] = (byte)ch;
}
}
}
}
//integer
public class TestInteger{
public static void main(String[] args){
int w = new Integer(args[0]).intValue();
int h = Integer.parseInt(args[1]);
for(int i=0;i<h;i++){
StringBuffer sb = new StringBuffer();
for(int j=0;j<w;j++){
sb.append("*");
}
System.out.println(sb.toString());
}
}
}
String sb = new String();
for(int j=0;j<w;j++)
{
sb = sb + "*";
}
//效率高點
StringBuffer sb = new StringBuffer();
for(int j=0;j<w;j++)
{
sb.append("*");
}
//集合類
Vector Enumeration ArrayList Collection Iterator
Set List
Vector 類與 Enumeration接口
public class TestVector{
public static void main(String[] args){
Vector v = new Vector();
System.out.println("please enter number:");
while(true)
{
int b = System.in.read();
if(b=='\r'||b=='\n')
break;
else
{
int num = b - '0';
v.addElement(new Integer(num));
}
}
int sum = 0;
Enumeration e = v.elements();
while(e.hasMoreElements())
{
Integer intObj = (Integer)e.nextElement();
sum += intObj.intValue();
}
System.out.println(sum);
}
}
//Collection接口與Iterator接口
public class TestCollection{
public static void main(String[] args){
Collection v = new ArrayList();
System.out.println("please enter number:");
while(true)
{
int b = System.in.read();
if(b=='\r'||b=='\n')
break;
else
{
int num = b - '0';
v.add(new Integer(num));
}
}
int sum = 0;
Iterator e = v.iterator();
while(e.hasNext())
{
Integer intObj = (Integer)e.next();
sum += intObj.intValue();
}
System.out.println(sum);
}
}
//Collection Set List的區別如下:
Collection是通用接口
Collection各元素對象之間沒有指定的順序,允許有重複的和
多個null元素對象
Set各元素對象之間沒有指定的順序,不允許有重複元素,最多
允許有一個null元素對象
List各元素對象對象之間有指定的順序,允許有重複元素和多個null
元素對象
public class TestSort{
public static void main(String[] args){
ArrayList a1 = new ArrayList();
al.add(new Integer(1));
al.add(new Integer(3));
al.add(new Integer(2));
System.out.println(al.toString());
Collections.sort(al);
System.out.println(al.toString());
}
}
//Hashtable類
Hashtable不僅可以像Vector一樣動態存儲一系列的
對象,而且對存儲的每一個對象(稱爲值)都要安排
另一個對象(稱爲關鍵字)與之相關聯。
Hashtable numbers = new Hashtable();
numbers.put("one",new Integer(1));
numbers.put("two",new Integer(2));
Integer n = numbers.get("two");
if(n != null)
{
}
用作關鍵字的類必須覆蓋Object.hashCode方法
和Object.equals方法
public class HashtableTest{
public static void main(){
Hashtable numbers = new Hashtable();
numbers.put(new MyKey("zhangsan",18),new Integer(1));
numbers.put(new MyKey("lisi",15),new Integer(2));
numbers.put(new MyKey("wangwu",30),new Integer(3));
Enumration e = numbers.keys();
while(e.hasMoreElements())
{
MyKey key = (MyKey)e.nextElement();
System.out.print(key + "=");
System.out.println(numbers.get(key));
}
//numbers.get(new MyKey("zhangsan",18));
}
}
public class MyKey{
private String name = null;
private int age = 0;
public MyKey(String name,int age)
{
this.name = name;
this.age = age;
}
public boolean equals(Object obj){
if(obj instanceof MyKey)
{
MyKey objTemp = (MyKey)obj;
if(name.equals(objTemp.name)&&age ==objTemp.age)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public int hashCode(){
return name.hashCode() + age;
}
public String toString(){
return name + age;
}
}
//Properties類
Properties類是Hashtable的子類
Properties.store();
//打印程序運行次數
public class PropertiesFile{
public static void main(String[] args){
Properties settings = new Properties();
try{
settings.load(new FileInputStream("count.txt"));
}
catch(Exception e)
{
settings.setProperty("count",String.valueOf(0));
}
//settings.get("count");
int c = Integer.parseInt(settings.getProperty("count")) + 1;
System.out.println("這是第" + c + "次運行");
//settings.put("count",new Integer(c).toString());
settings.setProperty("count",new Integer(c).toString());
try{
settings.store(new FileOutputStream("count.txt"),"Program is used:");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//System Runtime類
System類
-exit方法 終止虛擬機運行
-currentTimeMillis方法
-Java虛擬機的系統屬性
-getProperties setProperties
System.currentTimeMillis();
//打印虛擬機的系統屬性
public class TestProperties{
public static void main(String[] args){
Properties sp = System.getProperties();
Enumeration e = sp.propertyNames();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
System.out.println(key + "=" + sp.getProperty(key));
}
Process p = null;
try{
p = Runtime.getRuntime().exec("notepad.exe TestProperties.java");
Thread.sleep(5000);
p.destroy();
}catch(Exception e1)
{
}
}
}
System.setProperties();
Runtime類
-Runtime.getRuntime靜態方法
//日期和時間有關的類
最常用的幾個類:Date/DateFormat Calendar
Calendar
- add()
- get()
- set()
- getInstance() static
- GregorianCalendar child
public class TestCalendar{
public static void main(String[] args){
Calendar cl = Calendar.getInstance();
System.out.println(cl.get(Calendar.YEAR) + "年" + cl.get(cl.MONTH) + "月");
cl.add(cl.DAY_OF_YEAR,315);//315天后
}
}
Date類
java.text.DateFormat java.text.SimpleDateFormat
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
try{
Date d = sdf1.parse("2003-03-15");
sdf2.format(d)
}catch(Exception e)
{
}
Timer TimerTask類
schedule方法
schedule(TimerTask task,long delay)
schedule(TimerTask task,Date time)
schedule(TimeTask task,long delay,long period)
schedule(TimerTask task,Date firstTime,long period)
TimerTask類實現了Runnable接口,要執行的任務由它裏面是啥
的run方法來完成
new Timer().schedule(
new TimerTask()
{
public void run()
{
try
{
Runtime.getRuntime().exec("calc.exe");
}
catch(Exception e)
{
e.printStackTrace();
}
//結束任務線程的代碼
//Timer.cancel();
//TimerTask.cancel();
}
}
,30000);
class MyTimerTask extend TimerTask
{
private tm = null;
public MyTimerTask(Timer tm)
{
this.tm = tm;
}
public void run()
{
try
{
Runtime.getRuntime().exec("calc.exe");
}
catch(Exception e)
{
e.printStackTrace();
}
//結束任務線程的代碼
//Timer.cancel();
//TimerTask.cancel();
}
}
Timer tm = new Timer();
tm.schedule(new MyTimerTask(tm)
,30000);
//new Thread(new MyTimerTask()).start();
Math Random
//file
import java.io.*;
public class FileTest{
public static void main(String[] args){
File f = new File("1.txt");
if(f.exists())
{
f.delete();
}
else
{
f.createNewFile();
}
System.out.println("File name:" + f.getName());
System.out.println("File path:" + f.getPath());
System.out.println("File abs path:" + f.getAbsolutePath());
System.out.println("File Parent:" + f.getParent());
System.out.println(f.exists()?"exist":"not exist");
}
}
AWT的基礎知識
GUI
java.awt.Component java.awt.Container
GUI部分由AWT線程管理
public static void main(String[] args){
Frame f = new Frame("www.");
f.add(new Button("ok"));
f.setSize(300,300);
f.setVisable();
try{
Thread.sleep(5000);
}
catch(Exception e){}
f.dispose();
}
AWT事件處理
MouseEvent
WindowEvent
ActionEvent
public class MyWindowListener implements WindowListener{
public void windowClosing(WindowEvent parm1)
{
parm1.getWindow().setVisible(false);
//parm1.getSource();
java.awt.Component parm1.getComponent();
getEngine Engine
getDongFengEngine DongFengEngine;
}
public static void main(String[] args)
{
}
}