Java第十四天(字符串的其餘功能、StringBuffer、Random、System)

(9)字符串的其餘的功能java

public static void main(String[] args) {
        String s="  Baba.Mama.gotowork   ";
// split(String regex)  按照給定的正則表達式拆分字符串成爲數組
        String[] s1=s.split("\\.");//必須是符號才能夠,並且最好都加上\\
        for (String s2 : s1) {
            System.out.println(s2);
        }
// replace(char oldChar, char newChar)  用新字符替換字符串中指定舊字符
        String s3=s.replace('a','o');
        System.out.println(s3);
// replace(CharSequence target, CharSequence replacement)  用新子字符串替換字符串中指定舊子字符串
        String s4=s.replace("work","atrip");
        System.out.println(s4);
// trim()   去掉字符串中先後的空格不能去掉中間的空格
        String s5=s.trim();
        System.out.println(s5);
    }

練習題:正則表達式

需求:找出下面字符串中li的數量數組

  "liasdflihsdhllihsdflihsdfiligsdfglikhsdfklilisdflio"安全

/*需求:找出下面字符串中li的數量
  "liasdflihsdhllihsdflihsdfiligsdfglikhsdfklilisdflio"*/
public class Homework2 {
    public static void main(String[] args) {
        String s="liasdflihsdhllihsdflihsdfiligsdfglikhsdfklilisdflio";
        int sum=0;
        int i=s.indexOf("li");
        //當索引大於字符串長度的時候會返回-1
        while(i!=-1){
            sum++;
            i=s.indexOf("li",i+2);
        }
        /*char[] s1=s.toCharArray();
        for (int i = 0; i < s1.length; i++) {
            if(s1[i]=='l'&&s1[i+1]=='i'&&i+1<s1.length){
                sum++;
            }
        }*/
        System.out.println("這段字符串中li的數量爲"+sum);
    }
}

練習題2:app

定義一個方法,傳入字符串格式(idcard:name:age:gender)的分隔形如dom

「120384722312901:kexin:20:1|3219212131312:hantao:22:1|120121121312:zhangmeiling:21:2」,將該字符串分解,將屬性賦給定義的Person類的對象上而且輸出。ide

提示:建立每一個Person對象,每一個對象都有idcard、name、age、gender屬性性能

public class Person {
    private String idcard;
    private String name;
    private int age;
    private int gender;

    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public String getIdcard() {
        return idcard;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getGender() {
        return gender;
    }

    public Person() {
    }

    public Person(String idcard, String name, int age, int gender) {
        this.idcard = idcard;
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Person{" +
                "idcard='" + idcard + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}
import java.util.Arrays;

public class Homework3 {
    /*定義一個方法,傳入字符串格式(idcard:name:age:gender)的分隔形如
「120384722312901:kexin:20:1|3219212131312:hantao:22:1|120121121312:zhangmeiling:21:2」,
    將該字符串分解,將屬性賦給定義的Person類的對象上而且輸出。
    提示:建立每一個Person對象,每一個對象都有idcard、name、age、gender屬性*/
    public static void main(String[] args) {

        String str="120384722312901:kexin:20:1|3219212131312:hantao:22:1|120121121312:zhangmeiling:21:2";
//     以|爲分隔把對象分開變成數組
        String[] sc=str.split("\\|");
        Person[] ps=new Person[3];
        for (int i = 0; i < sc.length; i++) {
            //以:爲分隔將我的屬性拆到一個數組
            String[] s=sc[i].split("\\:");
            //建立一個Person對象
            Person p=new Person();
            //將數組中的屬性賦值給這我的
            p.setIdcard(s[0]);
            p.setName(s[1]);
            //Integer.parseInt(String str)將字符串轉化整數
            p.setAge(Integer.parseInt(s[2]));
            p.setGender(Integer.parseInt(s[3]));
            System.out.println(p);
            ps[i]=p;
        }
        //數組的打印
        System.out.println(Arrays.toString(ps));

    }
}

1.lang下面的類都不須要引入就能夠直接使用,String不須要引入就可使用,String類被final修飾了不能被繼承this

2.==:spa

基本數據類型:比較的是值;

引用數據類型:比較的是地址

equals針對的是object的對象或者子類的對象,基本數據類型不是類不能用equals,equals能夠比較的兩個對象的業務含義判斷兩個對象是否相等(經過方法覆寫定義業務規則)

3.數組並非對象不是類

4.String下有覆寫父類equals的方法,經過equals比較的是兩個String的值不是地址,其餘的類中沒有覆寫equals方法的調用equals比較的是地址

 

 

61.可變字符串

(1)String一旦被建立後,值不能改變,若是參與了操做,引用發生了變化,不是在原有的字符串上操做,而是新產生了一個字符串

StringBuffer建立後值能夠變化(增刪改),地址不會有變化,不會產生新的字符串。

public class StringBufferDemo {
    public static void main(String[] args) {
        StringBuffer s=new StringBuffer("我要吃");
        StringBuffer s1=s.append("紅燒肉");
        System.out.println(s);
        System.out.println(s1);
        System.out.println(s==s1);
    }
}

(2)可變字符串是線程安全的

多個線程同時操做同一個資源的時候,可能發生數據安全性的問題。

StringBuffer是線程同步的(線程安全的),可是犧牲了性能。

synchronized 關鍵字,表明這個方法加鎖,至關於無論哪個線程(例如線程A),運行到這個方法時,都要檢查有沒有其它線程B(或者C、 D等)正在用這個方法(或者該類的其餘同步方法),有的話要等正在使用synchronized方法的線程B(或者C 、D)運行完這個方法後再運行此線程A,沒有的話,鎖定調用者,而後直接運行。它包括兩種用法:synchronized 方法和 synchronized 塊。

(3)StringBuffer構造器

public class StringBufferDemo1 {
    public static void main(String[] args) {
        //創造一個可變字符,默認容量是16個字符
        StringBuffer s1=new StringBuffer();
        StringBuffer s2=new StringBuffer(16);
        StringBuffer s3=new StringBuffer("God");
        System.out.println(s1.capacity());
        System.out.println(s3.capacity());
        //追加的時候若是超出了16個會自動的擴容
        s3.append("dhjgbduhcgcgauandjkhliuw");
        System.out.println(s3.capacity());
    }
}

(4)StringBuffer的API

可變字符串的追加返回的都是這個可變字符串的自己的引用,因此經過鏈式方式來追加代碼。

public class StringBufferDemo2 {
    public static void main(String[] args) {
        StringBuffer s=new StringBuffer();
//鏈式追加
        s.append(true)
                .append(2)
                .append('K')
                .append(1.2f)
                .append("沒有一點點防備")
                .append(3.4)
                .append(new Student("小明",8));
        System.out.println(s);
    }
}

public class StringBufferDemo3 {
    public static void main(String[] args) {
        StringBuffer s=new StringBuffer("wonderfulyou");
        //在字符串中索引爲6的位置插入why
        s.insert(6,"why");
        System.out.println(s);
    }
}

public class StringBufferDemo4 {
    public static void main(String[] args) {
        StringBuffer s=new StringBuffer("wonderfulyou");
        //刪除索引處的字符
        s.deleteCharAt(3);
        System.out.println(s);
        //刪除索引範圍內的字符串,包頭不包尾
        s.delete(6,9);
        System.out.println(s);
    }
}

public class StringBufferDemo5 {
    public static void main(String[] args) {
        StringBuffer s=new StringBuffer("wonderfulyou");
        //用指定的字符串替換指定索引範圍內的子字符串
        s.replace(9,12,"me");
        System.out.println(s);
        //字符串的倒置
        s.reverse();
        System.out.println(s);
    }
}

public class StringBufferDemo6 {
    public static void main(String[] args) {
        StringBuffer s=new StringBuffer("hoohwoowmoomgxs");
        //檢索指定字符串最後一次出現的索引
        int i =s.lastIndexOf("oo");
        System.out.println(i);
        //從開始到指定索引處的子字符串中檢索指定字符串最後一次出現的索引
        int j=s.lastIndexOf("oo",8);
        System.out.println(j);
    }
}

 

62.Random隨機數產生類

能夠本身定義一些獲取隨機的整數的規則

import java.util.Random;

public class RandomTest {
    public static void main(String[] args) {
        Random r=new Random();
        //得到一組隨機數,每次運行隨機數都不同
        for (int i=0;i<10;i++) {
            int n= 0;
            n = r.nextInt();
            System.out.println(n);
        }
        //在0-100之間取一組隨機數
        for (int i = 0; i < 10; i++) {
            int n=r.nextInt(100);
            System.out.println(n);
        }
    }
}
import java.util.Random;

public class RandomTest1 {
    public static void main(String[] args) {
        //經過指定種子的隨機數對象來得到一組不變的數據
        Random r1=new Random(3);
        for (int i = 0; i < 10; i++) {
            int n=0;
            n = r1.nextInt();
            System.out.println(n);
        }
        //在0-1000之間取一組隨機數
        for (int i = 0; i < 10; i++) {
            int n=0;
            n = r1.nextInt(1000);
            System.out.println(n);
        }
    }
}

應用場景:驗證碼、文件命名

63.System類(瞭解)

public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    System.out.println();
}

import java.util.Arrays;

public class SystemDemo1 {
    public static void main(String[] args) {
       // arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
        int[] arr={11,22,33,44,55,66,77};
        int[] arr1={001,002,003,004,005};
    /* @param      src      the source array.
     * @param      srcPos   starting position in the source array.
     * @param      dest     the destination array.
     * @param      destPos  starting position in the destination data.
     * @param      length   the number of array elements to be copied.
     須要複製插入的數組,從插入數組的索引出開始,目標數組,從目標數組的索引處開始插入,插入的位數*/
        System.arraycopy(arr,2,arr1,1,3);
        System.out.println(Arrays.toString(arr1));
    }
}

public class SystemDemo2 {
    public static void main(String[] args) {
         int[] s1={12,344,566,32,45};
         long start=System.currentTimeMillis();
         method(s1);
         long end=System.currentTimeMillis();
        System.out.println("程序運行的時間是:"+(end-start)+"毫秒");
        System.exit(1);//只要status不爲0就是正常停止
    }
    public static void method(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

import java.util.Properties;
import java.util.Set;

public class SystemDemo3 {
    public static void main(String[] args) {
        //得到當前的系統屬性
        Properties p=System.getProperties();
        //System.out.println(p);
        Set<Object> objects=p.keySet();
        for(Object obj:objects){
            String key=(String)obj;
            String value=p.getProperty(key);
            System.out.println(key+"------->"+value);
        }
    }
}

相關文章
相關標籤/搜索