java初学者必读-经验总结

0 views
Skip to first unread message

sowill

unread,
Oct 30, 2005, 2:02:50 PM10/30/05
to Hack Soul
由于java初学者必读-经验总结这个帖子,得到大家的好评和关注,但是由于回复的太多了不利于大家阅读,所以在开一帖。希望大家能把自己的经验帖出来,以便后人参考。希望各位高手大力支持。帮助初学者少走弯路。

java版是大家的java版,这里的技术含量提高了,大家也都会从这里取得更大的进步!

另外,支持这里的,总结经验丰富的,我在另行开帖送分!就象上一帖一样!


我相信付出就有汇报!你呢?
呵,未来的日子,我们一起走过!

---------------------------------------------------------------

使用StringTokenizer类分析字符串
import java.util.*;
public class UseStringTokenizer
{ public static void main(String args[])
{ String s1="I am Geng.X.y,she is my girlfriend";
String s2="Lowood?what is that?";
StringTokenizer fenxi_1=new StringTokenizer(s1,"
,");//空格和逗号做分隔符。
StringTokenizer fenxi_2=new StringTokenizer(s2,"
?");//问号和空格做分隔符。
int n1=fenxi_1.countTokens(),n2=fenxi_2.countTokens();
while(fenxi_1.hasMoreTokens())
{ String s=fenxi_1.nextToken();
System.out.println(s);
}
System.out.println("s1有单词:"+n1+"个");
while(fenxi_2.hasMoreTokens())
{ String s=fenxi_2.nextToken();
System.out.println(s);
}
System.out.println("s2有单词:"+n2+"个");
}
}

---------------------------------------------------------------

qxjavajava(射手座 =---> 你今天JAVA了吗):

呵呵!,小弟我整理了一下你的东东,希望抛砖引玉,如下:


import java.util.*;

public class UseStringTokenizer{
public UseStringTokenizer(){
}

public int getWordCount(String str){
int iWordCount;
StringTokenizer sTok=new StringTokenizer(str," ,.!?'");
iWordCount=sTok.countTokens();
System.out.println(str+"有单词:"+iWordCount+"个");
return iWordCount;
}

public void PrintStrWord(String str){
int i=0;
StringTokenizer sTok=new StringTokenizer(str," ,.!?'");
while(sTok.hasMoreTokens()){
++i;
String pStr=sTok.nextToken();
System.out.println(i +". "+ pStr);

}

}

public static void main(String args[]){
String s1="I am Geng.X.y,she is my girlfriend ? ateta";

String s2="Lowood?what is that?";

UseStringTokenizer Tok=new UseStringTokenizer();

Tok.getWordCount(s1);
Tok.PrintStrWord(s1);

Tok.getWordCount(s2);
Tok.PrintStrWord(s2);
}
}
---------------------------------------------------------------

关于用JBuilder编译的程序,涉及到打成Jar包以后的问题,主要是指访问Jar包以后的路径,请大家多提意见!

public class SysConfigPath
{

private static String homePath;

public static String getHome()
{
String path = System.getProperty("aygl_import.home");
if (path != null) {
return path;
}
String homePath = getProjectPath();
System.setProperty("aygl_import.home",homePath);
return homePath;
}

public static String getClassPath(){
String path = System.getProperty("aygl_import_class.home");
if (path != null) {
return path;
}
String homePath = new
SysConfigPath().getClass().getResource("").getFile();
String packpath = "aygl_import/";
int index = homePath.lastIndexOf(packpath);
homePath = homePath.substring(0, index);
System.setProperty("aygl_import_class.home",homePath);
return homePath;
}

private static String getProjectPath(){
String homePath = getClassPath();
int index = homePath.lastIndexOf(".jar!/");

if (index != -1){
// System.out.println("是jar");
homePath = homePath.substring(0, homePath.length()-1);
index = homePath.lastIndexOf("/");
homePath = homePath.substring(0, index+1);
homePath = homePath.substring(5);
// System.out.println(homePath);
}
homePath = homePath.substring(0, homePath.length()-1);
index = homePath.lastIndexOf("/");
homePath = homePath.substring(0, index+1);
// System.out.println(homePath);
return homePath;
}

}
---------------------------------------------------------------

贴一个数据库的优化吧,可能对做数据库方面开发的有用


如何让你的SQL运行得更快
----
人们在使用SQL时往往会陷入一个误区,即太关注于所得的结果是否正确,而忽略了不同的实现方法之间可能存在的性能差异,这种性能差异在大型的或是复杂的数据库环境中(如联机事务处理OLTP或决策支持系统DSS)中表现得尤为明显。笔者在工作实践中发现,不良的SQL往往来自于不恰当的索引设计、不充份的连接条件和不可优化的where子句。在对它们进行适当的优化后,其运行速度有了明显地提高!下面我将从这三个方面分别进行总结:


----
为了更直观地说明问题,所有实例中的SQL运行时间均经过测试,不超过1秒的均表示为(<
1秒)。

---- 测试环境--
---- 主机:HP LH II
---- 主频:330MHZ
---- 内存:128兆
---- 操作系统:Operserver5.0.4
----数据库:Sybase11.0.3

一、不合理的索引设计
----例:表record有620000行,试看在不同的索引下,下面几个
SQL的运行情况:
---- 1.在date上建有一非个群集索引

select count(*) from record where date >
'19991201' and date < '19991214'and amount >
2000 (25秒)
select date,sum(amount) from record group by date
(55秒)
select count(*) from record where date >
'19990901' and place in ('BJ','SH') (27秒)
---- 分析:
----date上有大量的重复值,在非群集索引下,数据在物理上随机存放在数据页上,在范围查找时,必须执行一次表扫描才能找到这一范围内的全部行。


---- 2.在date上的一个群集索引

select count(*) from record where date >
'19991201' and date < '19991214' and amount >
2000 (14秒)
select date,sum(amount) from record group by date
(28秒)
select count(*) from record where date >
'19990901' and place in ('BJ','SH')(14秒)
---- 分析:
----
在群集索引下,数据在物理上按顺序在数据页上,重复值也排列在一起,因而在范围查找时,可以先找到这个范围的起末点,且只在这个范围内扫描数据页,避免了大范围扫描,提高了查询速度。


---- 3.在place,date,amount上的组合索引

select count(*) from record where date >
'19991201' and date < '19991214' and amount >
2000 (26秒)
select date,sum(amount) from record group by date
(27秒)
select count(*) from record where date >
'19990901' and place in ('BJ, 'SH')(< 1秒)
---- 分析:
----
这是一个不很合理的组合索引,因为它的前导列是place,第一和第二条SQL没有引用place,因此也没有利用上索引;第三个SQL使用了place,且引用的所有列都包含在组合索引中,形成了索引覆盖,所以它的速度是非常快的。


---- 4.在date,place,amount上的组合索引

select count(*) from record where date >
'19991201' and date < '19991214' and amount >
2000(< 1秒)
select date,sum(amount) from record group by date
(11秒)
select count(*) from record where date >
'19990901' and place in ('BJ','SH')(< 1秒)
---- 分析:
----
这是一个合理的组合索引。它将date作为前导列,使每个SQL都可以利用索引,并且在第一和第三个SQL中形成了索引覆盖,因而性能达到了最优。


---- 5.总结:

----
缺省情况下建立的索引是非群集索引,但有时它并不是最佳的;合理的索引设计要建立在对各种查询的分析和预测上。一般来说:


---- ①.有大量重复值、且经常有范围查询

(between, >,< ,>=,< =)和order by
、group by发生的列,可考虑建立群集索引;
----
②.经常同时存取多列,且每列都含有重复值可考虑建立组合索引;


----
③.组合索引要尽量使关键查询形成索引覆盖,其前导列一定是使用最频繁的列。


二、不充份的连接条件:
----
例:表card有7896行,在card_no上有一个非聚集索引,表account有191122行,在

account_no上有一个非聚集索引,试看在不同的表连接条件下,两个SQL的执行情况:


select sum(a.amount) from account a,
card b where a.card_no = b.card_no(20秒)
---- 将SQL改为:
select sum(a.amount) from account a,
card b where a.card_no = b.card_no and a.
account_no=b.account_no(< 1秒)
---- 分析:
----
在第一个连接条件下,最佳查询方案是将account作外层表,card作内层表,利用card上的索引,其I/O次数可由以下公式估算为:


----
外层表account上的22541页+(外层表account的191122行*内层表card上对应外层表第一行所要查找的3页)=595907次I/O


----
在第二个连接条件下,最佳查询方案是将card作外层表,account作内层表,利用account上的索引,其I/O次数可由以下公式估算为:


----
外层表card上的1944页+(外层表card的7896行*内层表account上对应外层表每一行所要查找的4页

Reply all
Reply to author
Forward
0 new messages