Code1.1.2

代码

String
遍历,拼接,反转
StringBuilder: append、reverse、toString
与String的转化
链式编程

public class Nineteen {
    public static void main(String[] args) {
        char[] chs = {'A', 'B', 'C', 'D', 'E'};
        byte[] bys = {65, 66, 67, 68, 69};

        String s1 = new String();
        String s2 = new String(chs);
        String s3 = new String(bys);
        String s4 = "ABCDE";

        arrayToS(s1);
        arrayToS(s2);
        arrayToS(s3);
        arrayToS(s4);

        System.out.println("--------");

        String s5 = arrayAppendS(s4);
        System.out.println(s5);

        System.out.println("--------");

        String s6 = reverseS(s4);
        System.out.println(s6);

        System.out.println("--------");

        String s7 = reverseSS(s4);
        System.out.println(s7);
    }

    public static void arrayToS(String s) {
        for (int i = 0; i < s.length(); i++) {
            System.out.print(s.charAt(i));
        }
        System.out.println();
    }

    public static String arrayAppendS(String si) {
        StringBuilder so = new StringBuilder();
        for (int i = 0; i < si.length(); i++) {
            if (i == si.length() - 1) {
                so.append(si.charAt(i));
            } else {
                so.append(si.charAt(i));
                so.append(",");
            }
        }
        return so.toString();
    }

    public static String reverseS(String si) {
        StringBuilder so = new StringBuilder();
        for (int i = si.length() - 1; i >= 0; i--) {
            so.append(si.charAt(i));
        }
        return so.toString();
    }

    public static String reverseSS(String s) {
        return new StringBuilder(s).reverse().toString();
    }
}

多态
向上转型,向下转型
static,final

public class Twenty {
    public static void main(String[] args) {
        Human h1 = new Superman();
        h1.setName("Clark");
        h1.setHeight(180.0);
        h1.show();
        h1.eat();
        Human.communication();
        h1.move();
        Superman s1 = (Superman) h1;
        s1.duty();

        System.out.println("--------");

        Superman h2 = new Superman("David", 179.0);
        h2.show();
        h2.eat();
        Human.communication();
        h2.move();
        h2.duty();
    }
}

class Human {
    private String name;
    static private double height;
    final static private String COUNTRY = "American";

    public Human() {
    }

    public Human(String name, double height) {
        this.name = name;
        Human.height = height;
    }

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

    public String getName() {
        return name;
    }

    public void setHeight(double height) {
        Human.height = height;
    }

    public double getHeight() {
        return height;
    }

    public void show() {
        System.out.println(" " + COUNTRY);
    }

    public final void eat(){
    	System.out.println("eat food");
    }
    
    public static void communication() {
        System.out.println("speak " + COUNTRY + " language");
    }

    public void move() {
        System.out.println("walk");
    }

}

class Superman extends Human {
    public Superman() {
    }

    public Superman(String name, double height) {
        super(name, height);
    }

    public void show() {
        System.out.print(getName() + " " + getHeight());
        super.show();
    }

    public void move() {
        System.out.println("fly");
    }

    public void duty() {
        System.out.println("save lives");
    }
}

抽象类,接口

public class TwentyOne {
    public static void main(String[] args) {
        Coder c1 = new Coder();
        c1.setName("wz");
        c1.setId(120);
        c1.setSalary(5000);
        c1.show();

        Manager m1 = new Manager();
        m1.setName("kpz");
        m1.setId(70);
        m1.setSalary(3000);
        m1.setBonus(30000);
        m1.show();

        System.out.println("---------");

        QCoder c2 = new QCoder();
        c2.setName("ztz");
        c2.setId(59);
        c2.setSalary(6000);
        c2.show();
        c2.goToWork();

        QManager m2 = new QManager();
        m2.setName("e");
        m2.setId(50);
        m2.setSalary(4000);
        m2.setBonus(40000);
        m2.show();
        m2.goToWork();
    }
}

abstract class Employee {
    private final boolean work = true;
    private int id;
    private String name;
    private static double salary;

    public Employee() {
    }

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

    public String getName() {
        return name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setSalary(double salary) {
        Employee.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public boolean getWork() {
        return work;
    }

    public abstract void show();
}

class Coder extends Employee {
    public Coder() {
    }

    public void show() {
        if (getWork()) {
            System.out.print("worker ");
        }
        System.out.println(getName() + " " + getId() + " " + getSalary());
    }
}

class Manager extends Employee {
    private static double bonus;

    public Manager() {
    }

    public void setBonus(double bonus) {
        Manager.bonus = bonus;
    }

    public double getBonus() {
        return bonus;
    }

    public void show() {
        if (getWork()) {
            System.out.print("working ");
        }
        System.out.println(getName() + " " + getId() + " " + getSalary() + " " + getBonus());
    }
}

interface Duty {
    public abstract void goToWork();
}

class QCoder extends Coder implements Duty {
    public QCoder() {
    }

    public void goToWork() {
        System.out.println("doing jobs");
    }
}

class QManager extends Manager implements Duty {
    public QManager() {
    }

    public void goToWork() {
        System.out.println("watching coders doing jobs");
    }
}

内部类
private、static类非static方法、static类static方法
局部内部类

public class TwentyTwo {
    public static void main(String[] args) {
        Outer1.Inner1 oi1 = new Outer1().new Inner1();
        oi1.print1();

        System.out.println("--------");

        Outer1 o2 = new Outer1();
        o2.print2();

        System.out.println("--------");

        Outer1.Inner3 oi3 = new Outer1.Inner3();
        oi3.print3();

        Outer1.Inner3.print4();

        System.out.println("--------");

        Outer1 o5 = new Outer1();
        o5.method();
    }
}

class Outer1 {
    public int num1 = 10;
    private int num2 = 100;
    static int num3 = 1000;
    static int num4 = 2000;
    int num5 = 10000;

    class Inner1 {
        public int num1 = 20;

        public void print1() {
            int num1 = 30;
            System.out.println(num1);
            System.out.println(this.num1);
            System.out.println(Outer1.this.num1);
        }
    }

    private class Inner2 {
        public void personal() {
            System.out.println(num2);
        }
    }

    public void print2() {
        Inner2 i2 = new Inner2();
        i2.personal();
    }

    static class Inner3 {
        public void print3() {
            System.out.println(num3);
        }

        public static void print4() {
            System.out.println(num4);
        }
    }

    public void method() {
        class Inner5 {
            public void print5() {
                System.out.println(num5);
            }
        }

        Inner5 i5 = new Inner5();
        i5.print5();
    }
}

匿名内部类

public class TwentyThree {
    public static void main(String[] args) {
        Outer2.method().show1();
    }
}

interface InterShow {
    void show1();

    void show2();
}

class Outer2 {
    public static InterShow method() {
        return new InterShow() {
            public void show1() {
                System.out.println("hello world");
            }

            public void show2() {
                System.out.println();
            }
        };
    }
}

匿名内部类(lambda表达式)

public class TwentyThree {
    public static void main(String[] args) {
        Outer2.method().show();
    }
}

interface InterShow {
    void show();
}

class Outer2 {
    public static InterShow method() {
        return () -> System.out.println("hello world");
    }
}

冒泡排序(原理 + Arrays.sort()版)
选择排序
随机数字、计时、String、String与int相互转化、增强for

import java.util.Arrays;

public class TwentyFour {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        int length = (int) (Math.random() * 100 + 1);
        int[] arr1 = getArr(length);
        int[] arr5 = getArr(length);

        System.out.println(arrayToString1(arr1));
        System.out.println(arrayToString1(arr5));
        System.out.println("----------------");

        String arr2 = arrayToString2(arr1);
        String[] arr3 = arr2.split(" ");
        int[] arr4 = new int[length];
        for (int i = 0; i < arr4.length; i++) {
            arr4[i] = Integer.parseInt(arr3[i]);
        }

        for (int i = 0; i < arr1.length - 1; i++) {
            for (int j = 0; j < arr1.length - 1 - i; j++) {
                if (arr1[j] > arr1[j + 1]) {
                    swap(arr1, j, j + 1);
                }
            }
        }

        for (int i = 0; i < arr5.length - 1; i++) {
            for (int j = i + 1; j < arr5.length; j++) {
                if (arr5[i] > arr5[j]) {
                    swap(arr5, i, j);
                }
            }
        }

        Arrays.sort(arr4);

        System.out.println(arrayToString1(arr1));
        System.out.println(arrayToString1(arr4));
        System.out.println(arrayToString1(arr5));

        long end = System.currentTimeMillis();
        System.out.println("用时: " + (end - start));
    }

    private static int[] getArr(int length) {
        int[] arr0 = new int[length];
        for (int i = 0; i < arr0.length; i++) {
            arr0[i] = (int) (Math.random() * 1000 + 1);
        }
        return arr0;
    }

    private static void swap(int[] arr0, int i, int j) {
        int temp = arr0[i];
        arr0[i] = arr0[j];
        arr0[j] = temp;
    }

    public static String arrayToString1(int[] arr0) {
        StringBuilder s = new StringBuilder();
        s.append("[");
        for (int i = 0; i < arr0.length; i++) {
            if (i == arr0.length - 1) {
                s.append(arr0[i]);
            } else {
                s.append(arr0[i]).append(", ");
            }
        }
        s.append("]");
        return s.toString();
    }

    public static String arrayToString2(int[] arr0) {
        StringBuilder s = new StringBuilder();
        for (int j : arr0) {
            String s0 = String.valueOf(j);
            s.append(s0).append(" ");
        }
        return s.toString();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值