1. 程式人生 > >java日常學習:eclipse、Object類、String類

java日常學習:eclipse、Object類、String類

java程式碼塊

eclipse的使用

eclipse 的使用步驟:
1. 要求你選擇一個工作目錄 。 工作目錄: 以後的所有程式碼都會儲存到工作目錄的資料夾中, 2. 以後寫程式碼是以工程作為單位。如果需要寫程式碼首先必須 要先新建一個 工程。

建立一個工程的步驟:

package Explorer  ----> 右擊 ------> new -------->other ------>java Project 

工程目錄的介紹:

        .settings : 該資料夾儲存的是eclipse自動生成的配置檔案,千萬不要碰。 
        bin : 用於儲存class檔案。以後所有的class檔案都會儲存到bin目錄中。
        src :  用於儲存java原始檔。

修改字型:

    windows ----------- perference ----------> 輸入text ----------> text editor -------- colors and fonts 

新建一個java檔案的步驟:

    src ----------右擊 ---------------->  new  ------------>class -----------編寫好類名

執行一個java檔案的方式:

    方式一:  右擊---------》 run as --------------- 》 java Application

    方式二:  綠色箭頭-------》run as --------------- 》 java Application

    方式三: ctrl+ f11

使用eclipse的好處:
1. 不需要我們手動的編譯,只要你儲存java檔案,那麼就會自動編譯。
2. 可以精準的報錯,會自動修復。

快捷鍵的使用:

alt + /             內部補全   

ctrl + 1                自動修復

ctrl + shift + f        程式碼格式化

alt + 上下方向鍵                程式碼移動

ctrl + shift + x                轉大寫

ctrl + shift + y                轉小寫

ctrl+ shift + /                  新增多行註釋

ctrl + shift + \                取消多行註釋

ctrl + /                       新增取消單行註釋。


ctrl + d                      刪除當前行程式碼

設定自定義快捷鍵:

window ----------- > preference -----------keys ------------>getter |  const

object類

找物件的方式:

方式一: 自定義類,通過自定義類建立物件。

方式二:sun定義好了很多的類,我們只需要認識這些類即可通過這些類建立物件為我們所用。

Object 類:

Object類是所有類的終極父類。

Object類常用的方法:
toString() 返回的字串是用於描述該物件的
equals(Object obj) 判斷兩個物件是否為同一個物件, 預設比較 的是記憶體地址。
hashCode() 返回一個物件的雜湊碼值。雜湊碼 值可以看做一個物件的記憶體地址。

java規範: 一般我們在重寫equals方法的時候,我們都會重寫hashCode方法.

檢視原始碼的方式:

方式一: 按住ctrl+ 滑鼠單擊

方式二: 按下f3

Object類練習一

//需求:如果使用者名稱和密碼相等就判定為同一個使用者
package day01;

public class Demo1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Person user1 = new Person("admin1", "123");
        System.out.println(user1);
        Person user2 = new Person("admin1","123" );
        System.out.println(user2);
        System.out.println("是同一個物件嗎?"+user1.equals(user2));
        System.out.println("user1的雜湊碼值:"+user1.hashCode());
        System.out.println("user2的雜湊碼值:"+user2.hashCode());
    }

}
class Person{
    String userName;
    String password;
    public Person(String userName, String password) {
        super();
        this.userName = userName;
        this.password = password;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "使用者名稱:"+userName+" 密碼:"+password;
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        Person user = (Person)obj;
        return  this.userName.equals(user.userName)&&this.password.equals(user.password);
    }

}

Object類練習二

//如果兩個人的身份證號碼一致,則為同一個人,且雜湊值改為身份證值
package day01;

class People {
    int id;
    String name;

    public People(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    /*
    為什麼要重寫toString方法? 因為直接輸出一個user
    物件的時候輸出的是該物件的toString方法返回 的字串,該字串是Object的toString方法返回的不能
    滿足我目前的需求,所以則進行了重寫。

     */

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "身份證號:" + id + " 姓名:" + "name";
    }

    //如果一個人的身份證編號一致,那麼則為同一個人。
    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        People people = (People) obj;
        return this.id == people.id;
    }

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return this.id;
    }
}

public class Demo2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        People p1 = new People(123, "狗娃");
        System.out.println(p1);
        People p2 = new People(123, "狗蛋");
        System.out.println(p2);
        System.out.println("相等嗎? " + p1.equals(p2));
        System.out.println("狗娃的雜湊值:" + p1.hashCode());
        System.out.println("狗蛋的雜湊值: " + p2.hashCode());
    }

}

Sting類

String類練習一

/*
 String 類

 */
public class Demo1 {

    public static void main(String[] args) {
        String str1 = "hello";
        String str2 = "hello";
        String str3 = new String("hello");
        String str4 = new String("hello");

        System.out.println("str1==str2?"+(str1==str2));                 //true      
        System.out.println("str2==str3?"+(str2==str3));                 //false  
        System.out.println("str3==str4?"+(str3==str4));                 // false  
        System.out.println("str2.equals(str3)?"+(str2.equals(str3)));  // true
        System.out.println("str3.equals(str4)?"+(str3.equals(str4)));  //true
        /*
         String類已經重寫了Object的equals方法,比較的是兩個字串的內容是否一致,如果一致
         則返回true,否則返回false

         */

        }

}

String類的構造方法:

String()
String(byte[] bytes) 使用一個byte陣列構建一個字串 . byte陣列儲存的是字元對應的碼值。
String(byte[] bytes, int offset, int length) 使用一個byte位元組陣列構建一個字串物件,指定開始使用的位置索引值與使用的字元個數。
String(char[] value) 使用字元陣列構建 一個字串
String(char[] value, int offset, int count) 使用一個char字元陣列構建一個字串物件,並且指定開始使用的位置索引值與使用的字元個數。
String(“字串”)

總結: 使用位元組陣列與字元陣列是可以構建一個字串物件的。

String類練習二

/*
獲取方法
        int length()  獲取字串的長度
        char charAt(int index) 獲取特定位置的字元 (角標越界)
        int indexOf(String str) 獲取特定字元的位置(overload)
        int lastIndexOf(int ch) 獲取最後一個字元的位置
*/
public class Demo3 {

    public static void main(String[] args) {
        String str  = "中國人abc美國人abc";
        System.out.println("獲取字元的個數:"+ str.length());
        System.out.println("指定索引值查詢字元:"+ str.charAt(2));
        System.out.println("查詢指定字串第一次出現的索引值:"+ str.indexOf("abc"));
        System.out.println("查詢指定字串最後一次出現的索引值:"+ str.lastIndexOf("abc"));
    }

}

String類練習三

package cn.itcast.string;

import java.util.Arrays;
/*
3.3判斷方法
    boolean endsWith(String str) 是否以指定字元結束
    boolean isEmpty()是否長度為0 如:“” null V1.6
    boolean contains(CharSequences) 是否包含指定序列 應用:搜尋
    boolean equals(Object anObject) 是否相等
    boolean equalsIgnoreCase(String anotherString) 忽略大小寫是否相等

轉換的方法;
    char[] toCharArray()  將字串轉換為字元陣列
    getBytes();   把字串轉換位元組陣列

*/

public class Demo4 {

    public static void main(String[] args) {
        /*
        String str = "Demo1.java";
        System.out.println("是否以指定的字元結束:"+ str.endsWith("java"));
        str = "";
        System.out.println("判斷指定的字元是否為空內容:"+ str.isEmpty());
        str = "hello java";
        System.out.println("判斷字串是否包含指定的字元:"+ str.contains("hel "));

        //str = null;
        System.out.println("判斷的內容是否一致:"+ "HELLO JAVA".equals(str));
        System.out.println("忽略大小寫比較的內容是否一致:"+ "HELLO JAVA".equalsIgnoreCase(str));
        */

        String str = "Demo1";
        char[] arr = str.toCharArray(); //把字串轉換一個字元陣列
        System.out.println("字元陣列的元素:"+ Arrays.toString(arr));
        byte[] buf = str.getBytes();
        System.out.println("位元組陣列 的元素:"+ Arrays.toString(buf));

        }

}

String類練習四

import java.util.Arrays;

/*
3.5其他方法
    String replace(char oldChar, char newChar) 替換
    String[] split(String regex) 切割
    String substring(int beginIndex)   
    String substring(int beginIndex, int endIndex)擷取字串
    String toUpperCase() 轉大寫
    String toLowerCase() 轉小寫
    String trim() 去除空格
     */


public class Demo5 {

    public static void main(String[] args) {
        String str  =  "hello world";
        System.out.println("替換的方法:"+ str.replace("world","大家"));

        str = "你們-大家-下午好";
        String[] datas = str.split("-"); // 指定分隔符進行切割。
        System.out.println("字串陣列 的內容:"+ Arrays.toString(datas));

        str = "hello java";
        System.out.println("指定開始的索引值擷取字串:"+ str.substring(6));
        System.out.println("指定開始與結束的索引值擷取字串:"+ str.substring(6, 8)); // 注意 : 擷取的位置是從startIndex開始,末尾是endIndex-1(包頭不包尾)

        str = "大家好";
        System.out.println("轉大寫:"+ str.toUpperCase());
        str = "aBC";
        System.out.println("轉小寫:"+ str.toLowerCase());

        str = "      大家今晚         要考試   ";                             
        System.out.println("清除首尾的空格:"+str.trim());
            }

}

String練習五

/*練習1: 自己實現一個trim方法。 

練習2: 獲取上傳檔名。

練習3:將字串物件中儲存的字元反序。
*/
package day01;

public class Demo3 {

    public static void main(String[] args) {
        /*
         * // TODO Auto-generated method stub String str =
         * "     我   愛 你 中     國                  ";
         * System.out.println("結果:"+str.replace(" ", "")); 
         * String path = "E:\\新java視訊\\day13\\視訊";
         * 
         * System.out.println("檔案是:"+getFileName(path));
         */
        String str = "吃葡萄不吐葡萄皮";
        System.out.println("結果是:" + reverse(str));
    }

    // 剔除首尾空格;
    /*
     * public static String myTrim(String str) {
     * //定義兩個變數記錄開始於結束的索引值
     * int startIndex = 0; 
     * 
     * int endIndex = str.length()-1;
     * 
     * //確定開始的索引值
     * 
     * while (str.charAt(startIndex)==' ') { 
     * startIndex++; } 
     * 
     * while(str.charAt(endIndex)==' ') {
     *  endIndex--; } 
     *  
     * //指定索引值擷取子串
     * String result = str.substring(startIndex, endIndex+1); 
     * 
     * return result;
     * 
     * 
     * }
     */

    // 獲取檔名;
    /*
     * public static String getFileName(String path) { 
     * int index = path.lastIndexOf("\\");
     * String fileName = path.substring(index + 1);
     *  return  fileName;
     *  }
     */

    // 反轉字串內容;
    public static String reverse(String str) {
        //把字串轉成了字元陣列
        char[] buf = str.toCharArray();
        //定義兩個變數記錄要 交換位置的元素索引值
        int startIndex = 0;
        int endIndex = buf.length - 1;
        while (startIndex < endIndex) {
            //交換位置
            char temp = buf[startIndex];
            buf[startIndex] = buf[endIndex];
            buf[endIndex] = temp;
            //
            startIndex++;
            endIndex--;
        }
        return new String(buf);   // 使用字元陣列構建了字串
    }
}

用於平時自己複習回顧,如有錯誤,歡迎指正