1.拆分字符串
遇到特殊字符,比如:對‘$’符號,就應該使用‘\\$’,后總結可以加個方括號如 "[.]"。
2.遍歷HASHMAP
Iterator itr = map.keySet().itrator();
while(itr.hasNext())
{
Object temp1 = itr.next();
Object temp2 = tab.get(temp1);
}
3.日歷操作
Calendar c = Canlendar.getInstance();
c.get(c.YEAR);//獲取年份,其他同理
c.add(c.MONTH,-1);//上個月的日期
4.隨機數
Random random = new Random();
int ran = random.nextInt(100);
注意:范圍[0,100)
5.讀取配置文件
對于ini 文件或者 properties文件,其實只要內容是 ds=dfjh或者 kd: ksadkf這種,就可以用
Properties pro = new Properties();
//pro.load (Main.class.getResourceAsStream("/test.properties"));或者
//pro.load (new FileInputStream ("test.properties"));
pro.getProperty ("test")
6.遍歷vector
兩種方式:
// for (Enumeration e = v.elements ();e.hasMoreElements ();)
// {
// System.out.println (e.nextElement ().toString ());
// }
Iterator item = v.iterator ();
while(item.hasNext ())
{
System.out.println (item.next ().toString ());
}
7.JAVA在WINDOWS下調用其他程序
try
{
Process p = Runtime.getRuntime().exec("mspaint");
p.waitFor();
}catch ...
8.獲取鍵盤輸入
BufferedReader input = new BufferedReader(new InputStream(System.in));
String s = input.readLine();
9.子類無參構造會隱式super(),若父類沒有聲明無參構造函數,而且有含參數構造函數,程序編譯不通過。
10.命令提示符下,編譯java文件 建議使用"javac -d . xxx.java" 能自動生成程序中的包。而運行只需要"java packname.mainclass".
11.使用 "pack200 x.gz y.jar"則將jar文件壓縮成gz文件,對class文件壓縮率極高,解壓縮使用"unpack200 x.gz y.jar".
12.代碼中'@'標記符號使用,例如@ Override 在方法前面,表示此方法是覆蓋父類方法,那么在編譯時會自動檢查父類中是否有該方法。
13.周期性事件:
private java.util.Timer timer;
timer.schedule(new java.util.TimerTask()
{
public void run()
{
//……要做的事
}
},0,5*60*1000);
timer本身是多線程同步的,不需要自己啟動線程。
14.介紹下JDK5.0 新特性
1.枚舉類型:
public class EnumDemo
{
enum MyColors
{
red,
black,
blue,
green,
yellow
};
public static void main(String args[])
{
MyColors color = MyColors.red;
//for 也是JDK 5中新特性
for(MyColors option : color.values())
{
System.out.println(option);
}
switch(color)
{
case red:
System.out.println("best color is "+color.red);
break;
default:
System.out.println("What");
break;
}
}
}
幾點注意:1. enum不能寫成局部變量。
2. switch()參數為枚舉常量。
3. case 后red實際是 color.red(由于其機制強制省略color)而其他地方是不能直接用red的.
15.正則表達式:(檢驗郵箱)
String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$" ;
Pattern regex = Pattern.compile (check) ;
Matcher matcher = regex.matcher (Emailname) ;
boolean isMatched = matcher.matches () ;
16.序列化
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(combo);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in =new ObjectInputStream(byteIn);
JComboBox comb2 = (JComboBox)in.readObject();
17.數據庫操作
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection m_objConnection = DriverManager.getConnection (jdbc:odbc:smstransmitDB;uid=sa;pwd=leslie);
/*
Statement objStatement = m_objConnection.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
*/
String m_objDelSql = "delete from " + m_objTable +
" where " + m_objFldTagId + "=?";
PreparedStatement objStatement = m_objConnection.prepareStatement (m_objDelSql);
objStatement.setInt (1, objSms.id);
objStatement.execute ();
/*executeQuery()返回ResultSet結果*/
18.JAVA 截取小數位數
float a = 1234.5678f;
java.text.DecimalFormat df =new java.text.DecimalFormat("#.00");
String a=df.format(a);
System.out.println(a); //1234.56
//寫#的是有值就寫值,沒值就不寫
//寫0的是有值就寫值,沒值就寫0
19.大小寫互換
public static void main (String[] args)
{
Scanner sc = new Scanner (System.in);
sc.useDelimiter ("\n");
String temp = sc.next ();
chang_two(temp);
sc.close ();
}
//大小寫 互換
public staic void chang_two (String text)
{
char tem [] = text.toCharArray ();
for (int i = 0; i <tem.length;i++)
{
if(tem[i]>=97&&tem[i]<=122)
{
tem[i]=(char)(tem[i]-32);
}else if (tem[i]>=65&&tem[i]<=90)
{
tem[i]=(char)(tem[i]+32);
}
}
System.out.println(new String (tem));
}
20.java中格式化輸出數字
在實際工作中,常常需要設定數字的輸出格式,如以百分比的形式輸出,或者設定小數位數等,現稍微總結如下。
主要使用的類:java.text.DecimalFormat
1。實例化對象,可以用如下兩種方法:
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
因為DecimalFormat繼承自NumberFormat。
2。設定小數位數
系統默認小數位數為3,如:
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
System.out.println(df.format(12.3456789));
輸出:12.346
現在可以通過如下方法把小數為設為兩位:
df.setMaximumFractionDigits(2);
System.out.println(df.format(12.3456789));
則輸出為:12.35
3。將數字轉化為百分比輸出,有如下兩種方法:
(1)
df.applyPattern("##.##%");
System.out.println(df.format(12.3456789));
System.out.println(df.format(1));
System.out.println(df.format(0.015));
輸出分別為:1234.57% 100% 1.5%
(2)
df.setMaximumFractionDigits(2);
System.out.println(df.format(12.3456789*100)+"%");
System.out.println(df.format(1*100)+"%");
System.out.println(df.format(0.015*100)+"%");
輸出分別為:
1,234.57% 100% 1.5%
4。設置分組大小
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
df1.setGroupingSize(2);
System.out.println(df1.format(123456789));
輸出:1,23,45,67,89
還可以通過df1.setGroupingUsed(false);來禁用分組設置,如:
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
df1.setGroupingSize(2);
df1.setGroupingUsed(false);
System.out.println(df1.format(123456789));
輸出:123456789
5。設置小數為必須為2位
DecimalFormat df2=(DecimalFormat) DecimalFormat.getInstance();
df2.applyPattern("0.00");
System.out.println(df2.format(1.2));
輸出:1.20
21.遍歷VECTOR
for(int i = 0; i<v.size ();i++)
{
System.out.println (v.elementAt (i));
}
=============================================
class A{
int a = 10;
void a(){
}
}
class B{
int b = 20;
void b(){
}
}
public class Ex_instanceOf extends A{
public static void main(String[] args) {
A b = new A();
System.out.println(b instanceof A);
}
}
以上代碼會輸出true,但是將代碼System.out.println(b instanceof A);
改為System.out.println(b instanceof B);并不會輸出false,而是不能通過編譯請問怎么樣才能達到輸出false的效果呢!!!
答:
一般instanceof用在未知類型(比如Object)之間的比較。由于b顯式定義為class A,而A與B之間顯式沒有繼承關系,所以,編譯器會報錯。你把A b= new A();改成Object b = new A();就行了
==================================================
雖然Scanner可以不用進行顯示的聲明異常,但是遇到非法的用戶輸入時程序就直接退出去了。這在實際的用戶程序中是不行的,也就是說容錯處理不好!所以建議還是用BufferedReader
posted on 2008-07-08 11:01
henry1451 閱讀(272)
評論(2) 編輯 收藏