基礎(chǔ)復(fù)習(xí):
public static void testSwitch(){
//在java7之前哆窿,switch不支持Sting 在java7之后加入了String,每個(gè)case都要跟個(gè)break饮寞,不然會(huì)繼續(xù)執(zhí)行
//int num = 2;
String num = "2";
switch (num){
case "jia":{
System.out.println("我是jia");
break;
}
case "2":{
System.out.println("我是2");
break;
}
case "3":{
System.out.println("我是3");
break;
}
case "4":{
System.out.println("我是4");
break;
}
default:{
System.out.println("我是default");
break;
}
}
}
//==和equals的區(qū)別,一個(gè)變量是有連個(gè)部分組成的(內(nèi)存空間地址和空間的值)次兆,==是判斷內(nèi)存空間的地址是否一樣号胚,equals是看空間的值是否一樣
public static void testEquals(){
String a = "1";
String b = "1";
if (b==a){
System.out.println("== YES");
}
if (b.equals(a)){
System.out.println("equals = YES");
}
//可變字符串
StringBuffer ss = new StringBuffer();
ss.append("dd");
ss.append("ff");
ss.append("gg");
System.out.println(ss);
//ArrayList 可以存儲(chǔ)任何類型的項(xiàng) && List<類型> 只可以存儲(chǔ)指定類型的項(xiàng)
//List<>比ArrayList使用方便 && 因?yàn)樵谑褂肁rrayList內(nèi)部的值時(shí),必須強(qiáng)制轉(zhuǎn)換才行,因?yàn)榇娣旁贏rrayList里的值都轉(zhuǎn)換成了Object類型
//List是集合最大的父類焚鹊,它包含了ArrayList
//List<String> list=new ArrayList<String>();這樣的形式使得list這個(gè)對(duì)象可以有多種的存在形式
List<String >list = new ArrayList<>();
ArrayList arrayList = new ArrayList();
//list和arrayList PK map和HashMap诗芜,恨闪,其他他們的關(guān)系都是一樣的究飞,上面的說明也可以去解釋map和hashMap
Map<String ,String >map = new HashMap<>();
map.put("1","11");
map.put("2","22");
System.out.println(map);
//JSONObject和map的區(qū)別(JSONObject的key和value可以是不同數(shù)據(jù)類型的值(最終都會(huì)被轉(zhuǎn)化為object)置谦,而map則必須是指定數(shù)據(jù)類型的值)
JSONObject jsobj1 = new JSONObject();
jsobj1.put("type", "news");
jsobj1.put("offset", 1);
jsobj1.put("count", 3);
Map<String ,String >map2 = new HashMap<>();
map2.put("type", "news");
map2.put("offset", "1");
map2.put("count", "3");
//注意list 和 map都屬于集合,對(duì)其進(jìn)行操作時(shí) 一般用CollectionUtils來處理
CollectionUtils.isEmpty(map2);//例如:判斷map是否為空
}
//該類想要被序列化亿傅,必須實(shí)現(xiàn) java.io.Serializable 對(duì)象
//java中的序列化媒峡,為什么要進(jìn)行序列化
//簡(jiǎn)單說就是為了保存在內(nèi)存中的各種對(duì)象的狀態(tài),并且可以把保存的對(duì)象狀態(tài)再讀出來葵擎。雖然你可以用你自己的各種各樣的方法來保存Object States谅阿,但是Java給你提供一種應(yīng)該比你自己好的保存對(duì)象狀態(tài)的機(jī)制,那就是序列化。
//該機(jī)制中酬滤,一個(gè)對(duì)象可以被表示為一個(gè)字節(jié)序列签餐,該字節(jié)序列包括該對(duì)象的數(shù)據(jù)、有關(guān)對(duì)象的類型的信息和存儲(chǔ)在對(duì)象中數(shù)據(jù)的類型
/*
什么情況下需要序列化
a)當(dāng)你想把的內(nèi)存中的對(duì)象保存到一個(gè)文件中或者數(shù)據(jù)庫中時(shí)候盯串;
b)當(dāng)你想用套接字在網(wǎng)絡(luò)上傳送對(duì)象的時(shí)候氯檐;
c)當(dāng)你想通過RMI傳輸對(duì)象的時(shí)候;
*/
public static void SerializeTest() {
SerializeUser user = new SerializeUser();
user.setUserId(95001);
user.setUserName("孩子有課");
user.setAddress("優(yōu)盤時(shí)代");
user.setPhone("123456789");
user.setSex(1);
/*
//注意:所有的對(duì)文件(I/O)操作都需要try-catch
try {
//定義輸出文件路徑
FileOutputStream fileOut = new FileOutputStream("test/user.ser");
//定義文件輸出對(duì)象
ObjectOutputStream out = new ObjectOutputStream(fileOut);
//寫入資源到文件
out.writeObject(user);
//關(guān)閉
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/employee.ser");
}catch (IOException i){
i.printStackTrace();
}
*/
//反序列化
SerializeUser e = null;
try
{
FileInputStream fileIn = new FileInputStream("test/user.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (SerializeUser) in.readObject();
in.close();
fileIn.close();
System.out.println("反序列化結(jié)果="+e);
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
//!!!!!! 注意一點(diǎn)細(xì)節(jié):對(duì)IO的操作体捏,Output(程序的輸出)但是對(duì)文件的輸入冠摄;糯崎;input(程序的輸入)但是對(duì)文件的輸出
//都是以流的方式進(jìn)行的
}
//對(duì)字符串的處理
public static void StringTest(){
String a = "abc-defgb-dekliy";
//查找
int index1 = a.lastIndexOf("b");
int index2 = a.indexOf("b");
System.out.println(index1+"+"+index2);
//替換(替換所有,替換第幾個(gè))
String a1 = a.replaceFirst("b","ff");
System.out.println(a1);
//分割(string[]數(shù)組里面是存放string型的值河泳,List<string>是存放string類型的對(duì)象)
//String []和List<String>作用是一樣的沃呢,但靈活性不同。string[]是定長(zhǎng)的拆挥,不容易實(shí)現(xiàn)容量增長(zhǎng)
String [] a2 = a.split("-");//全部分割
System.out.println(a2[0]);
String [] a3 = a.split("-",2);//之分割前兩段
System.out.println(a3[0]);
//U敛濉!8偷蟆!L掠А食拜! 相互轉(zhuǎn)換 !!!!!!!!
List<String>list = Arrays.asList(a2);
String[] b1 = (String[]) list.toArray();
//字符串拼接
/*
* 1、String定義的副编,可以直接用+號(hào)拼接
* 2负甸、StringBuffer定義的,用append拼接
* */
//字符串格式化(例如吧e格式化成我們的數(shù)字類型等)
}
//數(shù)組和集合的區(qū)別:
//http://www.cnblogs.com/summers/p/4094260.html