Java填坑筆記:OGG-PRM項(xiàng)目知識(shí)點(diǎn)

  1. 三元運(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ù)
}
  1. 遍歷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)雅的寫法!

  1. 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支持批處理

  1. 判斷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)換為其哈希值的比較。

  1. 遍歷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);
 }
}
  1. 將Array轉(zhuǎn)換成List
String[] userid = {"aa","bb","cc"};
List<String> userList = new ArrayList<String>();
Collections.addAll(userList, userid);
  1. 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è)樣子的:

  1. 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();
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,644評(píng)論 18 399
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,011評(píng)論 0 11
  • 01 昨天見到同學(xué)老多,他情緒低落的說了一句,“國(guó)慶假期咱同學(xué)老張回來了,去棺木店看...
    花似錦年閱讀 564評(píng)論 0 0
  • 北書房閱讀 539評(píng)論 2 13
  • 《夏目友人帳 第五季》已經(jīng)更新到第6集 又有不少人被暖暖的夏目和萌萌的豬貓老師圈粉 當(dāng)然在我心中暖心動(dòng)畫的第一也非...
    阿雅喵閱讀 358評(píng)論 0 1

友情鏈接更多精彩內(nèi)容