//轉(zhuǎn)載請注明出處:http://hi.baidu.com/jadmin/blog/item/163ed2f8a8e88b0dd8f9fd46.html
/**
* @(#)MatrixMultiply.java
*
*
* @author JAdmin
* @version 1.00 2007/8/21
*/
import java.util.Scanner;
public class MatrixMultiply
{
public static void main(String[] args)
{
??? int m,n,p;
??? System.out.println("請輸入3個矩陣參數(shù)m,n,p.示例a[m][n]*b[n][p]=c[m][p]");
??? Scanner sr = new Scanner(System.in);
??? System.out.print("m=");
??? m = sr.nextInt();
??? System.out.print("n=");
??? n = sr.nextInt();
??? System.out.print("p=");
??? p = sr.nextInt();
??? int[][] a = new int[m][n];
??? int[][] b = new int[n][p];
??? int[][] c = new int[m][p];
??? System.out.println("請輸入數(shù)組a[" + m + "][" + n + "]");
??? input(a);
??? System.out.println("請輸入數(shù)組b[" + n + "][" + p + "]");
??? input(b);
??? //以下開始計算c[][]
??? for(int i = 0;i < c.length;i++)
???? for(int j = 0;j < c[0].length;j++)
????? c[i][j] = getMultiItem(a,b,i,j);
??? System.out.println("************The matrix a[][]************");
??? display(a);
??? System.out.println("************The matrix b[][]************");
??? display(b);
??? System.out.println("************The matrix c[][]************");
??? display(c);
}
private static void input(int[][] arr)
{
??? Scanner sr = new Scanner(System.in);
??? for(int i = 0;i < arr.length;i++)
???? for(int j = 0;j < arr[0].length;j++)
?????? arr[i][j] = sr.nextInt();
}
private static void display(int[][] arr)
{
??? for(int i = 0;i < arr.length;i++)
??? {
???? for(int j = 0;j < arr[0].length;j++)
????? System.out.print(arr[i][j]+" ");
???? System.out.println();
??? }
}
private static int getMultiItem(int[][] a,int[][] b,int p,int q)
{
??? //計算元素c[p][q]
??? int tmp = 0;
??? for(int i = 0;i < a[0].length;i++)
???? tmp += a[p][i]*b[i][q];
??? return tmp;
}
}
文檔對象模型 (DOM) 是一個文檔標(biāo)準(zhǔn),對于完備的文檔和復(fù)雜的應(yīng)用程序,DOM 提供了大量靈活性。DOM標(biāo)準(zhǔn)是標(biāo)準(zhǔn)的。它很強壯且完整,并且有許多實現(xiàn)。這是許多大型安裝的決定因素--特別是對產(chǎn)品應(yīng)用程序,以避免在API發(fā)生改變時進行大量的改寫。
以上是我在選擇處理XML數(shù)據(jù)時之所以沒有選擇JDOM或者dom4j等其它面向?qū)ο蟮臉?biāo)準(zhǔn)的原因,不過也由于DOM從一開始就是一種與語言無關(guān)的模型,而且它更趨向用于像C或Perl這類語言,沒有利用Java的面向?qū)ο蟮男阅埽栽谑褂玫倪^程中也遇到了不少的麻煩,今天這里做一個小結(jié)。另外,我目前使用XML主要是作為數(shù)據(jù)傳輸?shù)慕y(tǒng)一格式,并統(tǒng)一用戶界面展示的接口,應(yīng)用的面并不是很廣,所以使用到的DOM的內(nèi)容其實不多。
在準(zhǔn)備使用它的時候,是做了充足的準(zhǔn)備的,也有遇到困難的準(zhǔn)備,所以一開始就有了一個簡單的工具類來封裝DOM對象使用時必要的公共方法,實際證明這樣做是很明智的,一個簡單的創(chuàng)建Document對象的操作,要是每次都需要寫上5行以上代碼,并且還要處理那些煩人的Exception,實在是會打擊大家的積極性,所以在最初,做了一個XMLTool類,專門封裝了如下的公共方法:
1、 Document對象創(chuàng)建(包括空的Document對象創(chuàng)建,以一個給定Node節(jié)點作為根節(jié)點創(chuàng)建。
2、 將一個規(guī)范的XML字符串轉(zhuǎn)換成一個Document對象。
3、 從物理硬盤讀取一個XML文件并返回一個Document對象。
4、 將一個Node對象轉(zhuǎn)換成字符串。
其中每個方法都截獲相關(guān)的DOM操作所拋出的異常,轉(zhuǎn)換成一個RuntimeException拋出,這些異常在實際使用過程中,一般狀況下其實都不會拋出,特別是象生成一個Document對象時的ParserConfigurationException、轉(zhuǎn)換Node節(jié)點成字符串時要生成一個Transformer對象時的TransformerConfigurationException等等,沒有必要在它們身上花時間精力。而且真就出了相關(guān)的異常的話,其實根本沒有辦法處理,這樣的狀況通常是系統(tǒng)環(huán)境配置有問題(比如必要的DOM實現(xiàn)解析器等包沒有加入環(huán)境),所以包裝該異常時只是很簡要的獲取其Message拋出。
代碼如下:
/**
* 初始化一個空Document對象返回。
* @return a Document
*/
public static Document newXMLDocument() {
try {
return newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* 初始化一個DocumentBuilder
* @return a DocumentBuilder
* @throws ParserConfigurationException
*/
public static DocumentBuilder newDocumentBuilder()
throws ParserConfigurationException {
return newDocumentBuilderFactory().newDocumentBuilder();
}
/**
* 初始化一個DocumentBuilderFactory
* @return a DocumentBuilderFactory
*/
public static DocumentBuilderFactory newDocumentBuilderFactory() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
return dbf;
}
/**
* 將傳入的一個XML String轉(zhuǎn)換成一個org.w3c.dom.Document對象返回。
* @param xmlString 一個符合XML規(guī)范的字符串表達。
* @return a Document
*/
public static Document parseXMLDocument(String xmlString) {
if (xmlString == null) {
throw new IllegalArgumentException();
}
try {
return newDocumentBuilder().parse(
new InputSource(new StringReader(xmlString)));
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* 給定一個輸入流,解析為一個org.w3c.dom.Document對象返回。
* @param input
* @return a org.w3c.dom.Document
*/
public static Document parseXMLDocument(InputStream input) {
if (input == null) {
throw new IllegalArgumentException("參數(shù)為null!");
}
try {
return newDocumentBuilder().parse(input);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* 給定一個文件名,獲取該文件并解析為一個org.w3c.dom.Document對象返回。
* @param fileName 待解析文件的文件名
* @return a org.w3c.dom.Document
*/
public static Document loadXMLDocumentFromFile(String fileName) {
if (fileName == null) {
throw new IllegalArgumentException("未指定文件名及其物理路徑!");
}
try {
return newDocumentBuilder().parse(new File(fileName));
} catch (SAXException e) {
throw new IllegalArgumentException("目標(biāo)文件(" + fileName + ")不能被正確解析為XML!
" + e.getMessage());
} catch (IOException e) {
throw new IllegalArgumentException("不能獲取目標(biāo)文件(" + fileName + ")!
" + e.getMessage());
} catch (ParserConfigurationException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* 給定一個節(jié)點,將該節(jié)點加入新構(gòu)造的Document中。
* @param node a Document node
* @return a new Document
*/
public static Document newXMLDocument(Node node) {
Document doc = newXMLDocument();
doc.appendChild(doc.importNode(node, true));
return doc;
}
/**
* 將傳入的一個DOM Node對象輸出成字符串。如果失敗則返回一個空字符串""。
* @param node DOM Node 對象。
* @return a XML String from node
*/
public static String toString(Node node) {
if (node == null) {
throw new IllegalArgumentException();
}
Transformer transformer = newTransformer();
if (transformer != null) {
try {
StringWriter sw = new StringWriter();
transformer.transform(new DOMSource(node),
new StreamResult(sw));
return sw.toString();
} catch (TransformerException te) {
throw new RuntimeException(te.getMessage());
}
}
return errXMLString("不能生成XML信息!");
}
/**
* 將傳入的一個DOM Node對象輸出成字符串。如果失敗則返回一個空字符串""。
* @param node DOM Node 對象。
* @return a XML String from node
*/
public static String toString(Node node) {
if (node == null) {
throw new IllegalArgumentException();
}
Transformer transformer = newTransformer();
if (transformer != null) {
try {
StringWriter sw = new StringWriter();
transformer.transform(new DOMSource(node),new StreamResult(sw));
return sw.toString();
} catch (TransformerException te) {
throw new RuntimeException(te.getMessage());
}
}
return errXMLString("不能生成XML信息!");
}
/**
* 獲取一個Transformer對象,由于使用時都做相同的初始化,所以提取出來作為公共方法。
* @return a Transformer encoding gb2312
*/
public static Transformer newTransformer() {
try {
Transformer transformer =TransformerFactory.newInstance().newTransformer();
Properties properties = transformer.getOutputProperties();
properties.setProperty(OutputKeys.ENCODING, "gb2312");
properties.setProperty(OutputKeys.METHOD, "xml");
properties.setProperty(OutputKeys.VERSION, "1.0");
properties.setProperty(OutputKeys.INDENT, "no");
transformer.setOutputProperties(properties);
return transformer;
} catch (TransformerConfigurationException tce) {
throw new RuntimeException(tce.getMessage());
}
}
/**
* 返回一段XML表述的錯誤信息。提示信息的TITLE為:系統(tǒng)錯誤。之所以使用字符串拼裝,主要是這樣做一般
* 不會有異常出現(xiàn)。
* @param errMsg 提示錯誤信息
* @return a XML String show err msg
*/
public static String errXMLString(String errMsg) {
StringBuffer msg = new StringBuffer(100);
msg.append("<?xml version="1.0" encoding="gb2312" ?>");
msg.append("<errNode title="系統(tǒng)錯誤" errMsg="" + errMsg + ""/>");
return msg.toString();
}
/**
* 返回一段XML表述的錯誤信息。提示信息的TITLE為:系統(tǒng)錯誤
* @param errMsg 提示錯誤信息
* @param errClass 拋出該錯誤的類,用于提取錯誤來源信息。
* @return a XML String show err msg
*/
public static String errXMLString(String errMsg, Class errClass) {
StringBuffer msg = new StringBuffer(100);
msg.append("<?xml version="1.0" encoding="gb2312" ?>");
msg.append("<errNode title="系統(tǒng)錯誤" errMsg=""+ errMsg
+ "" errSource=""
+ errClass.getName()
+ ""/>");
return msg.toString();
}
/**
* 返回一段XML表述的錯誤信息。
* @param title 提示的title
* @param errMsg 提示錯誤信息
* @param errClass 拋出該錯誤的類,用于提取錯誤來源信息。
* @return a XML String show err msg
*/
public static String errXMLString(
String title,
String errMsg,
Class errClass) {
StringBuffer msg = new StringBuffer(100);
msg.append("<?xml version="1.0" encoding="gb2312" ?>");
msg.append("<errNode title=""
+ title
+ "" errMsg=""
+ errMsg
+ "" errSource=""
+ errClass.getName()
+ ""/>");
return msg.toString();
}
前幾天裝了個Firefox2.0.6瀏覽器,感覺性能確實不錯,但就是太耗內(nèi)存了,最后決定還是用IE+GB,卸掉!
誰知卸了Firefox后,硬盤上所有的htm和html文件的圖標(biāo)都關(guān)聯(lián)失敗了,上網(wǎng)查了下,原來裝Firefox的時候,注冊表被強行修改了,解決如下:
進入注冊表,依次找到HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.htm和HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.html
把他們默認(rèn)值改成htmlfile,再重啟下機器就OK了.
Imagine life as a game in which you are juggling some five balls in the air. You name them: Work, Family, Health, Friends, Spirit. And you're keeping all these in the air. You will soon understand that work is a rubber ball. If you drop it, it will bounce back. But the other four balls-family, health, friends and spirit are made of glass. If you drop one of these, it will be irrevocably scuffed, marked, nicked, damaged or even shattered. They will never be the same. You must understand that and strive for balance in your life.
How?
Don't undermine your worth by comparing yourself to others. It is because we are different that each of us is special.
Don't set your goals by what other people deem important. Only you know what is best for you.
Don't take for granted the things closest to your heart. Cling to them as you would to your life, for without them, life is meaningless.
Don't let your life slip through your fingers by living in the past or for the future. By living your life one day at a time, you live all the days of your life.
Don't give up when you still have something to give. Nothing is really over until the moment you stop trying.
Don't be afraid to admit that you are less than perfect. It is this fragile thread that binds us each together.
Don't be afraid to encounter risks. It is by taking chances that we learn how to be brave.
Don't shut love out of your life by saying it's impossible to find. The quickest way to receive love is to give; the fastest way to lose love is hold it too tightly; and the best way to keep love is to give it wings.
Don't run though life so fast that you forget not only where you've been, but also where you are going.
Don't forget, a person's greatest emotional need is to feel appreciated.
Don't be afraid to learn. Knowledge is weightless, a treasure you can always carry easily.
Don't use time or words carelessly. Neither can be retrieved.
Life is not a race, but a journey to be savored each step of the way.
Yesterday is history. Tomorrow is mystery and Today is a gift: that's why we call it "The Present".
記住,從現(xiàn)在開始,多想想你擁有的,而不是你想要的。如果這樣做,你的生活會比以前更美好,那種感受或許將是你生命中的第一次,你將會懂得心滿意足的含義。
One of the more pervasive and destructive mental tendencies I've seen is that of focusing on what we want instead of what we have. It doesn't seem to make my difference how much we have, we just keep expanding our list of desires, which guarantees we will remain dissatisfied. The mind-set that says "I'll be happy" when this desire is fulfilled is the same mind-set that will repeat itself once that desire is met.
We want this or that. If we don't get what we want, we keep thinking about all that we don't have and we remain dissatisfied. If we do get what we want, we simply recreate the same thinking in our new circumstances. So, despite getting what we want, we still remain unhappy. Happiness can't be found when we are yearning for new desires.
Luckily, there is a way to be happy. It involves changing the emphasis of our thinking from what we want to what we have. Rather than wishing you were able to take a vacation to Hawaii, think of how much fun you have had close to home. The list of possibilities is endless! Each time you notice yourself falling into the "I wish life were different" trap, back off and start over. Take a breath and remember all that you have to be grateful. When you focus not on what you want, but on what you have, you end up getting more of what you want anyway. If you focus on the good qualities of your spouse, she'll be more loving. If you are grateful for your job rather than complaining about it, you'll do a better job, be more productive, and probably end up getting a raise any-way. If you focus on ways to enjoy yourself around home rather than waiting to enjoy yourself in Hawaii, you'll end up having more fun. If you ever do get to Hawaii, you'll be in the habit of enjoying yourself. And, if by some chance you don't, you have a great life anyway.
Make a note of yourself to start thinking more about what you have than what you want. If you do, your life will start appearing much better than before. For perhaps the first time in your life, you'll know what it means to feel satisfied.
Windows自推出以來一直以其快捷的操作,簡單的界面,實用的功能,強大的多媒體功能等諸多特性為買點長期占據(jù)著操作系統(tǒng)的王座。并且我們使用Windows也已經(jīng)有數(shù)個春秋了,但我們真的對Windows操作了如執(zhí)掌了嗎?真的知道如何操作嗎?以下列出一些比較實用的快捷鍵:
CTRL+A:選中活動窗口的所有內(nèi)容
CTRL+C:把選中的東西復(fù)制到粘貼板
CTRL+F:顯示“查找和替換”對話框
CTRL+G:顯示定位對話框
CTRL+N:顯示新建對話框
CTRL+O:顯示打開對話框
CTRL+P:顯示打印對話框
CTRL+S:保存當(dāng)前文檔
CTRL+V:粘貼
CTRL+X:剪切
CTRL+Z:撤銷
ALT+F4:關(guān)閉當(dāng)前活動窗口
CTRL+拖放文件:復(fù)制拖放的文件
CTRL+ESC:顯示開始菜單
CTRL+F6:打開活動應(yīng)用程序的下一個文檔窗口
ALT+ENTER:顯示選中對象的屬性
ALT+F4:關(guān)閉活動項目或者退出活動的程序
ALT+空格:打開當(dāng)前活動窗口的快捷菜單
ALT+TAB:在打開窗口間切換
ALT+ESC:以打開的順序切換窗口
F1:提供當(dāng)前窗口或者選中項目的幫助文件
F2:重命名選中的項目
F3:搜索文件或文件夾
F4:顯示我的電腦或Windows Explorer中的地址欄
F5:刷新活動窗口
F6:在當(dāng)前窗口中的元素間切換,與TAB鍵功能一樣
F10:激活當(dāng)前窗口的菜單欄
Win:顯示或隱藏開始菜單
Win+BREAK:顯示系統(tǒng)屬性對話框
Win+D:顯示桌面
Win+M:最小化所有窗口
Win+SHIFT+M:恢復(fù)最小化窗口
Win+E:打開我的電腦
Win+F:搜索文件或文件夾
CTRL+Win+F:搜索計算機
Win+F1:顯示W(wǎng)indows幫助文件
Win+L:鎖住鍵盤
Win+R:打開運行窗口
Win+U:打開輔助工具管理器
TAB:在選項間切換
SHIFT+TAB:在選項間切換,順序與TAB相反
CTRL+TAB:在標(biāo)簽間進行切換
CTRL+SHIFT+TAB:在標(biāo)簽間反向切換
CTRL+SHIFT+ESC:打開Windows任務(wù)管理器
ALT+帶下劃線的字母:運行相應(yīng)的命令或選擇相應(yīng)的選項
空格鍵:如果焦點選中復(fù)選框,選擇或反選復(fù)選框
Win→U→U:快速關(guān)機(→表示連續(xù)分開執(zhí)行)
以下的快捷鍵用于與Windows資源管理器類型的程序:
NUM LOCK+星號(*):顯示選中文件夾的所有子文件夾(如果系統(tǒng)里的文件很多,反映會很慢哦~)
NUM LOCK+加號(+):顯示選中文件夾的內(nèi)容
NUM LOCK+減號(—):折疊選中文件夾
String的創(chuàng)建
?? String s = "hello";
?? JVM先根據(jù)內(nèi)容"hello"查找對象,如果沒有找到,則在heap上創(chuàng)建新對象,并將其賦予s1,否則使用已經(jīng)存在的對象
?? String s = new String("hello");
?? JVM直接在heap上創(chuàng)建新的對象,所以在heap中會出現(xiàn)內(nèi)容相同,地址不同的String對象
String的比較
?? "=="???????? 比較地址
?? "equals"???? 比較內(nèi)容
?? 舉例:
?? String s1 = "hello";
?? String s2 = "hello";
?? String s3 = new String("hello");
?? s1 == s2;??????????????? // true???????? 地址相同
?? s1 == s3;??????????????? // false??????? 地址不同
?? s1.equals(s2);?????????? // true???????? 內(nèi)容相同
?? s1.equals(s3);?????????? // true???????? 內(nèi)容相同
intern() 方法
??? 查找內(nèi)容相同(equals())的字符串
??? String s1 = "hello";???????????????? // hello不存在,jvm創(chuàng)建新對象 (1)
??? String s2 = new String("hello");???? // 創(chuàng)舉新對象 (2),這時heap中存在兩個內(nèi)容為hello的對象
??? s1 == s2;??????????? // false???????? // 地址不同
??? s1.equals(s2);?????? // true????????? // 內(nèi)容相同
??? s2 = s2.intern();??? // true????????? // 找到對象(1) 并賦予s2
??? s1 == s2;??????????? // true !!?????? // 注意:此時s1,s2同指向(1)
效率:String 與 StringBuffer
???? 情景1:
???? (1) String result = "hello" + " world";
???? (2) StringBuffer result = new String().append("hello").append(" world");
???????? (1) 的效率好于 (2),不要奇怪,這是因為JVM會做如下處理
???????? 編譯前??? String result = "hello" + " world";
???????? 編譯后??? String result = "hello world";
???? 情景2:
???? (1) public String getString(String s1, String s2) {
???????????? return s1 + s2;
???????? }
???? (2) public String getString(String s1, String s2) {
???????????? return new StringBuffer().append(s1).append(s2);
???????? }
???????? (1) 的效率與 (2) 一樣,這是因為JVM會做如下處理
???????? 編譯前??? return s1 + s2;
???????? 編譯后??? return new StringBuffer().append(s1).append(s2);
???? 情景3:
???? (1) String s = "s1";
?????????????? s += "s2";
?????????????? s += "s3";
???? (2) StringBuffer s = new StringBuffer().append("s1").append("s2").append("s3");
???????? (2) 的效率好于(1),因為String是不可變對象,每次"+="操作都會造成構(gòu)造新的String對象
???? 情景4:
???? (1) StringBuffer s = new StringBuffer();
???????? for (int i = 0; i < 50000; i ++) {
???????????? s.append("hello");
???????? }
???? (2) StringBuffer s = new StringBuffer(250000);
???????? for (int i = 0; i < 50000; i ++) {
???????????? s.append("hello");
???????? }
???????
???????? (2) 的效率好于 (1),因為StringBuffer內(nèi)部實現(xiàn)是char數(shù)組,默認(rèn)初始化長度為16,每當(dāng)字符串長度大于char
???????? 數(shù)組長度的時候,JVM會構(gòu)造更大的新數(shù)組,并將原先的數(shù)組內(nèi)容復(fù)制到新數(shù)組,(2)避免了復(fù)制數(shù)組的開銷
????????
關(guān)鍵點
???? 1). 簡單的認(rèn)為 .append() 效率好于 "+" 是錯誤的!
???? 2). 不要使用 new 創(chuàng)建 String
???? 3). 注意 .intern() 的使用
???? 4). 在編譯期能夠確定字符串值的情況下,使用"+"效率最高
???? 5). 避免使用 "+=" 來構(gòu)造字符串
???? 6). 在聲明StringBuffer對象的時候,指定合適的capacity,不要使用默認(rèn)值(18)
???? 7). 注意以下二者的區(qū)別不一樣
???????? - String s = "a" + "b";
???????? - String s = "a";
?????????? s += "b";