- 三元運(yùn)算符:用來完成簡(jiǎn)單的選擇邏輯,使用格式:
(條件表達(dá)式)?表達(dá)式1:表達(dá)式2;
public static void ternary() {
boolean boo = false;
int c = boo ? test1(1) : test2(2); //返回?cái)?shù)據(jù)
}
- 遍歷List集合,對(duì)集合中最后一個(gè)元素進(jìn)行判斷:
foreach語句是for語句在特殊情況下的增強(qiáng)版本,簡(jiǎn)化編程,提高了代碼的可讀性和安全性(不用擔(dān)心數(shù)組越界),相對(duì)于for語句是一個(gè)很好的補(bǔ)充,但foreach并不能替代for語句:
int lastIndex = colList.size()-1;
for (int index = 0;index<=lastIndex;index++){
String colName = colList.get(index);
s.append(colName+" = "+colName);
if(lastIndex == index){
s.append("\n--插入標(biāo)記\n),\n");
}else {
s.append(",\n");
}
}
還不能確定這樣是最優(yōu)雅的寫法!
- PrepareStatement的用法及解釋
1.PreparedStatement是預(yù)編譯的,對(duì)于批量處理可以大大提高效率. 也叫JDBC存儲(chǔ)過程
2.使用 Statement 對(duì)象。在對(duì)數(shù)據(jù)庫只執(zhí)行一次性存取的時(shí)侯,用 Statement 對(duì)象進(jìn)行處理。PreparedStatement 對(duì)象的開銷比Statement大,對(duì)于一次性操作并不會(huì)帶來額外的好處。
3.statement每次執(zhí)行sql語句,相關(guān)數(shù)據(jù)庫都要執(zhí)行sql語句的編譯,preparedstatement是預(yù)編譯得, preparedstatement支持批處理
- 判斷String中是否包含指定字符(判斷前三個(gè)字符):
String c = s.substring(0,3);
String schema = "";
switch (c){
case "ins":
case "INS":
schema = "ZOEINSUR";
break;
}
在java1.7之前大家都清楚switch的比較范圍只能局限于(int 、short 、byte 、char)之間,Java 虛擬機(jī)和字節(jié)代碼這個(gè)層次上,只支持在 switch 語句中使用與整數(shù)類型兼容的類型。在1.7后switch實(shí)現(xiàn)字符串比較的功能。具體是如何做到的?實(shí)際上,Java虛擬機(jī)和字節(jié)碼層次上只支持switch語句中使用與整數(shù)類型兼容的類型沒有變,只是這個(gè)實(shí)現(xiàn)字符串比較的新特性是在編譯器這個(gè)層次上實(shí)現(xiàn)的。實(shí)現(xiàn)的機(jī)制是:將字符串之間的比較轉(zhuǎn)換為其哈希值的比較。
- 遍歷Map
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
//第一種:普遍使用,二次取值
System.out.println("通過Map.keySet遍歷key和value:");
for (String key : map.keySet()) {
System.out.println("key= "+ key + " and value= " + map.get(key));
}
//第二種
System.out.println("通過Map.entrySet使用iterator遍歷key和value:");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第三種:推薦,尤其是容量大時(shí)
System.out.println("通過Map.entrySet遍歷key和value");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第四種
System.out.println("通過Map.values()遍歷所有的value,但不能遍歷key");
for (String v : map.values()) {
System.out.println("value= " + v);
}
}
- 將Array轉(zhuǎn)換成List
String[] userid = {"aa","bb","cc"};
List<String> userList = new ArrayList<String>();
Collections.addAll(userList, userid);
- String分割字符串,通過判斷"\n"換行符進(jìn)行字符串切割,
String[] res = str.split("\n");
需要注意的是:
1、如果這個(gè)文件是在Linux或者mac下建立編寫的,那么用str.split("\n")會(huì)出現(xiàn)正確的結(jié)果,如下圖:
![]()
2、如果這個(gè)文件是在window下編寫的,那么就該注意了,如果你還是用str.split("\n")就會(huì)出現(xiàn)錯(cuò)誤的結(jié)果,如下圖:
![]()
有人可能說沒什么區(qū)別啊,仔細(xì)看好了,第2個(gè)的結(jié)果,this和is testing中間有個(gè)空行,而第一個(gè)沒有。為什么會(huì)出現(xiàn)這個(gè)結(jié)果。
這還要從回車符來講,簡(jiǎn)單來說,window下回車是由\r\n(即0x0D和0x0A)組成的,注意不是\n\r,而linux下回車是由\n(即0x0A)小伙伴們別搞錯(cuò)了,這個(gè)我自己證實(shí)過,
![]()
這個(gè)window下編輯的文件,我用16進(jìn)制打開的,大家看到第3,4列中的是0D、0A,也就是回車。
而在Linux下是這個(gè)樣子的:
![]()
- Java IO流寫入文件
public class WriteFileExample {
public static void main(String[] args) {
File file = new File("c:/newfile.txt");
String content = "This is the text content";
try (FileOutputStream fop = new FileOutputStream(file)) {
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
但這里的字符串如果包含中文,就會(huì)出現(xiàn)亂碼,這是因?yàn)镕ileOutputStream是字節(jié)流,將文本按字節(jié)寫入文件,而一個(gè)漢字是兩個(gè)字節(jié),無法一次寫入,就會(huì)出現(xiàn)亂碼,解決方法是使用OutputStreamWriter將字節(jié)流轉(zhuǎn)換為字符流寫入,同時(shí)指定utf-8編碼,修改后代碼如下:
public static void write2PRMFILE(String string){
File file = new File("d:/test.prm");
String content = "";
content = string;
try (OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "utf-8");) {
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
oStreamWriter.write(content);
oStreamWriter.flush();
oStreamWriter.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}