Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1607064207--靳璐 #18

Open
jinluovo opened this issue Nov 22, 2017 · 3 comments
Open

1607064207--靳璐 #18

jinluovo opened this issue Nov 22, 2017 · 3 comments
Labels

Comments

@jinluovo
Copy link

jinluovo commented Nov 22, 2017

策略模式&排序算法

基础类

public class BaseSort {
    void sort(int[] a){

    }
    void print(int[] a,String str){
        System.out.print(str + ":");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "  ");
        }
        System.out.println();
    }
}

插入排序

public class InsertSort extends BaseSort {
    void sort(int[] a){
        for (int i = 0; i < a.length; i++) {
            int get = a[i];     //右手抓牌
            int j = i - 1;
            while(j >= 0 && a[j] > get){
                a[j+1] = a[j];
                j--;
            }
            a[j+1] = get;           //插入右手抓到的牌
        }
        print(a,"InsertSort");
    }
}

选择排序

public class SelectSort extends BaseSort {
    @Override
    void sort(int []a) {
        for (int i = 0; i < a.length; i++) {
            int temp;
            int k = i;
            a[k] = a[i];
            for (int j = i + 1; j < a.length; j++) {
                if (a[k] > a[j]){
                    k = j;
                }
            }
            temp = a[i];
            a[i] = a[k];
            a[k] = temp;
        }
        print(a,"SelectSort");
    }
}

快速排序

public class QuickSort extends BaseSort {
    @Override
    void sort(int[] a) {
        sort(a,0,a.length-1);
        print(a,"QuickSort");
    }
    void sort(int[] a,int left,int right){
        if (left < right){
            int i = left;
            int j = right;
            int key = a[i];
            while(i < j){
                while(i < j && a[j] >= key){
                    j--;
                }
                a[i] = a[j];
                while(i < j && a[i] <= key){
                    i++;
                }
                a[j] = a[i];
            }
            a[i] = key;
            sort(a,left,i - 1);
            sort(a,i+1,right);
        }
    }
}

策略类

public class Fact {

    private BaseSort sort;
    void setSort(BaseSort sort){
        this.sort = sort;
    }
    void doSort(int[] a){
        sort.sort(a);
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        int[] a = new int[10];
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要排序的数组:");
        for (int i = 0; i < 10; i++) {
            a[i] = scanner.nextInt();
        }
        Fact fact = new Fact();
        BaseSort sortInsert = new InsertSort();
        BaseSort sortQuick = new QuickSort();
        BaseSort sortSelect = new SelectSort();
        fact.setSort(sortInsert);
        fact.doSort(a);
        fact.setSort(sortQuick);
        fact.doSort(a);
        fact.setSort(sortSelect);
        fact.doSort(a);
    }
}

测试结果

输入要排序的数组:
6 8 5 3 9 0 2 7 1 4
InsertSort:0  1  2  3  4  5  6  7  8  9  
QuickSort:0  1  2  3  4  5  6  7  8  9  
SelectSort:0  1  2  3  4  5  6  7  8  9  
@DreamYHD
Copy link
Member

good

@jinluovo
Copy link
Author

jinluovo commented Dec 1, 2017

Java第二次培训

Java 8 之后有了接口为什么还要用抽象类?

答:(1)在 Java 8 的接口中,即使有了 default method ,也暂时无法完全取代抽象类。

(2)接口中没有 this ,没有构造函数,不能拥有实例变量或者实例方法,无法保存状态,而抽象方法可以。

(3)接口不能拥有非静态成员变量。Java 8 中接口只能提供静态常量、公有方法声明、公有默认方法和类方法;而 Java 9 新特性中允许接口中拥有私有方法。默认方法最初的设计目的是让已经存在的接口可以演化——添加新方法时不需要原本已经实现该接口的类做任何改变(甚至不需要重新编译)就可以直接使用新版本的接口,但是这已经在很大程度上覆盖了抽象类的特性。未来的 Java 版本的接口可能会有更强大的功能,或许能更大程度的替代原本需要抽象类的场景。

设计模式之工厂方法模式

产品接口及产品实现:

/**
   *产品接口:定义产品实现的规范,所有的产品实现都必须遵循产品接口定义的规范,
   * 也可以用抽象类来代替,注意不要违背里氏替换原则
   */
public interface IProduct {
    void create();
    void show();
}
//产品实现1
public class Product1 implements IProduct {
     @Override
     public void create() {
         System.out.println("Create Product1");
     }

     @Override
     public void show() {
         System.out.println("Show Product1");
     }
}
//产品实现2
public class Product2 implements IProduct{
    @Override
    public void create() {
        System.out.println("Create Product2");
    }

    @Override
    public void show() {
        System.out.println("Show Product2");
    }
}

工厂接口及工厂实现:

//工厂接口:工厂方法模式的核心
public interface IFactory {
  public IProduct createProduct();
}
/**
   *工厂实现:决定如何实例化产品,是实现扩展的途径,需要有多少种产品,
   * 就有多少个具体的工厂实现
   */
//创建product1对象的工厂实现
public class FactoryMethod1 implements IFactory {
  @Override
  public IProduct createProduct() {
      return new Product1();
}
//创建product2对象的工厂实现
public class FactoryMethod2 implements IFactory {
    @Override
    public IProduct createProduct() {
        return new Product2();
    }
}

客户类:

//客户类
public class Client {
    public static void main(String[] args) {
        //创建product1类型的具体产品
        IFactory iFactory1 = new FactoryMethod1();
        IProduct product1 = iFactory1.createProduct();
        product1.create();
        product1.show();
        //创建product2类型的具体产品
        IFactory iFactory2 = new FactoryMethod2();
        IProduct product2 = iFactory2.createProduct();
        product2.create();
        product2.show();
    }
}

运行结果:

Create Product1
Show Product1
Create Product2
Show Product2

我的笔记:
抽象类和接口
工厂方法模式

@InnoFang InnoFang added Nice Good and removed Nice labels Dec 3, 2017
@jinluovo
Copy link
Author

jinluovo commented Dec 9, 2017

遍历集合的几种方式

fori遍历

public class Each {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(27,"kris"));
        list.add(new Student(19,"jin"));
        list.add(new Student(22,"hao"));
        list.add(new Student(18,"lin"));
        list.add(new Student(20,"qin"));
        list.add(new Student(21,"zhi"));
        list.add(new Student(19,"yu"));
        list.add(new Student(21,"zhong"));
        list.add(new Student(44,"ma"));
        list.add(new Student(49,"fa"));

        for (int i = 0; i < list.size(); i++) {
            System.out.println("name is:" + list.get(i).getName() + "\tage is:" + list.get(i).getAge());
        }
    }
}

foreach遍历

for (Student s:
     list) {
    System.out.println("name is:" + s.getName() + "\tage is:" + s.getAge());
}

Lambda表达式遍历集合

list.forEach(s-> System.out.println("name is:" + s.getName() + "\tage is:" + s.getAge()));

集合操作符stream遍历集合

System.out.println("(1)Lambda表达式");
list.stream().forEach(s-> System.out.println("name is:" + s.getName() + "\tage is:" + s.getAge()));
System.out.println("(2)accept------");
list.stream().forEach(new Consumer<Student>() {
        @Override
        public void accept(Student student) {
            System.out.println("name is:" + student.getName() + "\tage is:" + student.getAge());
        }
});

迭代器遍历集合

Iterator<Student> iterator = list.iterator();
while(iterator.hasNext()){
    Student s = iterator.next();
    System.out.println("name is:" + s.getName() + "\tage is:" + s.getAge());
}

遍历结果:

name is:kris	age is:27
name is:jin	age is:19
name is:hao	age is:22
name is:lin	age is:18
name is:qin	age is:20
name is:zhi	age is:21
name is:yu	age is:19
name is:zhong	age is:21
name is:ma	age is:44
name is:fa	age is:49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants