String--特性:immutable(永遠不變), 字串變化可使用運算子「+」或是concat()方法
StringBuffer---特性:mutable ; thread-safe ; 可使用的方法substring()、replace()、delete()
StringBuilder--特性:mutable ;non-thread-safe ; 可使用的方法substring()、replace()、delete()
substring public String substring(int beginIndex, int endIndex)
substring 回傳值為string,
參數:
beginIndex - 起始索引(包括)。
endIndex - 結束索引(不包括)。
但replace()、delete() 回傳值為StringBuffer或StringBuilder
所以substring()與delete()併用時可能會發生錯誤的用法:
EX:
SringBuilder s=new StringBuilder("123456789");
s.substring(0,5).delete(0,3).replace(0,3,"Java");
System.out.println(s);
會造成編譯錯誤(cannot find symbol)!
2009年2月3日 星期二
Question 1
1.序列化是幹什麼的?
簡單說就是為了保存在記憶體中的各種物件的狀態(也就是實例變數,不是方法),並且可以把保存的物件狀態再讀出來。雖然你可以用你自己的各種各樣的方法來保存object states,但是Java給你提供一種應該比你自己好的保存物件狀態的機制,那就是序列化。
2、什麼情況下需要序列化
a)當你想把的記憶體中的物件狀態保存到一個檔中或者資料庫中時候;
b)當你想用套接字在網路上傳送物件的時候;
c)當你想通過RMI傳輸物件的時候;
3、當對一個物件實現序列化時,究竟發生了什麼?
在沒有序列化前,每個保存在堆(Heap)中的物件都有相應的狀態(state),即實例變數(instance ariable)
比如:
java 代碼
Foo myFoo = new Foo();
myFoo .setWidth(37);
myFoo.setHeight(70);
當 通過下面的代碼序列化之後,MyFoo物件中的width和Height實例變數的值(37,70)都被保存到foo.ser檔中,這樣以後又可以把它從檔中讀出來,重新在堆中創建原來的物件。當然保存時候不僅僅是保存物件的實例變數的值,JVM還要保存一些小量資訊,比如類的類型等以便恢復原來的對 象。
java 代碼
FileOutputStream fs = new FileOutputStream("foo.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(myFoo);
4、實現序列化(保存到一個檔)的步驟
a)Make a FileOutputStream
java 代碼
FileOutputStream fs = new FileOutputStream("foo.ser");
b)Make a ObjectOutputStream
java 代碼
ObjectOutputStream os = new ObjectOutputStream(fs);
c)write the object
java 代碼
os.writeObject(myObject1);
os.writeObject(myObject2);
os.writeObject(myObject3);
d) close the ObjectOutputStream
java 代碼
os.close();
5、舉例說明
java 代碼
import java.io.*;
public class Box implements Serializable {
private int width;
private int height;
public void setWidth(int width){ this.width = width; }
public void setHeight(int height){ this.height = height; }
public static void main(String [] args){
Box myBox = new Box();
myBox.setWidth(50);
myBox.setHeight(30);
try{
FileOutputStream fs = new FileOutputStream("foo.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(myBox);
os.close();
}catch(Exception ex){ ex.printStackTrace(); }
}
}
6、相關注意事項
a)序列化時,只對物件的狀態進行保存,而不管物件的方法;
b)當一個父類實現序列化,子類自動實現序列化,不需要顯式實現Serializable介面;
c)當一個物件的實例變數引用其他物件,序列化該物件時也把引用物件進行序列化;
d)並非所有的物件都可以序列化,,至於為什麼不可以,有很多原因了,比如:
1.安全方面的原因,比如一個物件擁有private,public等field,對於一個要傳輸的物件,比如寫到檔,或者進行rmi傳輸 等等,在序列化進行傳輸的過程中,這個物件的private等域是不受保護的。
2. 資源分配方面的原因,比如socket,thread類,如果可以序列化,進行傳輸或者保存,也無法對他們進行重新的資源分 配,而且,也是沒有必要這樣實現。
Question 5
The doesFileExist method takes an array of directory names representing a path from the root filesystem and a filename.The
method returns true if the file exists,false if does not.
Place the code fragments in position to complete this method.
//insert here A
for(String dir:directories){
//insert here B
}
//insert here C
//insert here D
Code Fragments
1.path = path.getSubdirectory(dir);
2.return !file.inNew();
3.return (file!=null);
4.String path="";
5.path = path.getFile(filename);
6.File path = new File("");
7.return file.exist();
8.return path.isFile();
9.File file = new File(path,filename);
10.path = new File(path,dir);
11.File path = new File(File.separator);
12.path = path+FileSeparator+dir;
當然了,要想做出這道題目,首先要知道這個程式段想要做什麼,要完成什麼任務.
這是從doesFileExist方法中截取的一段代碼,doesFileExist方法完成的主要功能是:這個方法會獲得一個字串陣列和一個檔案名.這個字元
串陣列存放的是從根目錄開始的一個路徑.doesFileExist方法中,如果檔存在,返回true,如果不存在,返回false;
既然知道了要做什麼事情,我們就很清楚的知道我們要去做什麼事情了:
首先,我們要先把字串陣列中的元素依次取出來,把它們連接到一起,形成一個完整的絕對路徑.然後,創建用這個路徑和已經得到的檔案名做
參加,生成一個File物件,利用File物件的exist()方法,來判斷這個檔是否存在.
那麼,我們的答案是什麼呢:
A:4
B:12
C:9
D:7
我也試著寫了一下測試類
import java.io.File;
public class Tester {
public static void main(String[] args) {
String[] directories = new String[] { "D:", "homework", "d0427" };
String filename = "table.xml";
System.out.println(doesFileExist(directories, filename));
}
public static boolean doesFileExist(String[] directories, String filename) {
String path = "";
for (String dir : directories) {
path = path + File.separator + dir;
}
File file = new File(path, filename);
return file.exists();
}
}
在我的電腦裏,D:\homework\d0427確實存在著一個檔table.xml,所以,返回值為true
本貼來自ZDNetChina中文社區 http://bbs.zdnet.com.cn ,本貼地址:http://bbs.zdnet.com.cn/viewthread.php?tid=384016
method returns true if the file exists,false if does not.
Place the code fragments in position to complete this method.
//insert here A
for(String dir:directories){
//insert here B
}
//insert here C
//insert here D
Code Fragments
1.path = path.getSubdirectory(dir);
2.return !file.inNew();
3.return (file!=null);
4.String path="";
5.path = path.getFile(filename);
6.File path = new File("");
7.return file.exist();
8.return path.isFile();
9.File file = new File(path,filename);
10.path = new File(path,dir);
11.File path = new File(File.separator);
12.path = path+FileSeparator+dir;
當然了,要想做出這道題目,首先要知道這個程式段想要做什麼,要完成什麼任務.
這是從doesFileExist方法中截取的一段代碼,doesFileExist方法完成的主要功能是:這個方法會獲得一個字串陣列和一個檔案名.這個字元
串陣列存放的是從根目錄開始的一個路徑.doesFileExist方法中,如果檔存在,返回true,如果不存在,返回false;
既然知道了要做什麼事情,我們就很清楚的知道我們要去做什麼事情了:
首先,我們要先把字串陣列中的元素依次取出來,把它們連接到一起,形成一個完整的絕對路徑.然後,創建用這個路徑和已經得到的檔案名做
參加,生成一個File物件,利用File物件的exist()方法,來判斷這個檔是否存在.
那麼,我們的答案是什麼呢:
A:4
B:12
C:9
D:7
我也試著寫了一下測試類
import java.io.File;
public class Tester {
public static void main(String[] args) {
String[] directories = new String[] { "D:", "homework", "d0427" };
String filename = "table.xml";
System.out.println(doesFileExist(directories, filename));
}
public static boolean doesFileExist(String[] directories, String filename) {
String path = "";
for (String dir : directories) {
path = path + File.separator + dir;
}
File file = new File(path, filename);
return file.exists();
}
}
在我的電腦裏,D:\homework\d0427確實存在著一個檔table.xml,所以,返回值為true
本貼來自ZDNetChina中文社區 http://bbs.zdnet.com.cn ,本貼地址:http://bbs.zdnet.com.cn/viewthread.php?tid=384016
訂閱:
文章 (Atom)