android-nuc / 17_Java_Train

17级 Android 实验室 Java 培训
12 stars 2 forks source link

1607094128-解奕鹏 #8

Open xieyipeng opened 6 years ago

xieyipeng commented 6 years ago

排序算法

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

    }
}

快速排序

public class QuickSort extends baseSort {

    @Override//重写
    public void sort(int[] a){
        sort(a,0,a.length-1);
    }

    public void sort(int[] a,int low,int high) {

        super.sort(a);
        if (low < high) {
            int middle = getMiddle(a, low, high);//将a数组进行一分为二
            sort(a, low, middle - 1);//对低字表进行递归排序
            sort(a, middle + 1, high);//对高字表进行递归排序
        }
    }

    public int getMiddle(int[] a, int low, int high) {
        int tmp = a[low];//数组的第一个作为中轴
        int i = low;
        int j = high;
        while (i != j) {
            while (a[j] >= tmp && i < j) {
                j--;
            }
            //a[low] = a[high];//比中轴小的记录移到低端
            while (a[i] <= tmp && i < j) {
                i++;
            }
            int temp = a[i];
            a[i] = a[j];//比中轴大的记录移到高端
            a[j] = temp;
        }
        int temp = a[low];
        a[low] = a[i];
        a[i] = temp;
        return i;
    }
}

插入排序

public class InsertSort extends baseSort{//插入排序

    @Override
    public void sort(int[] a) {
        for (int j = 1; j < a.length; j++) {//j从第二个元素开始
            int key = a[j];
            int i = j - 1;//i表示在j前面有多少个元素
            while (i >= 0 && a[i] > key) {//i控制循环次数,循环i次并且a[i]要大于key
                a[i + 1] = a[i];//
                i = i - 1;
                a[i + 1] = key;//符合条件换一次
            }
            //a[i + 1] = key;//不好理解。。。
        }
    }

选择排序

public class SelectSort extends baseSort {//选择排序

    @Override
    public void sort(int[] a) {
        int min = 0;//最小值的下标
        int temp;
        for (int i = 0; i < a.length; i++) {
            min = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] < a[min]) {//在后面的集合中寻找最小的下标
                    min = j;//小的下标赋给min
                }
            }
            if (min != i) {//最小值的下标和i不一样的时候,交换
                temp = a[i];
                a[i] = a[min];
                a[min] = temp;
            }
        }
    }
}

Factory

public class Factory {
    private baseSort Sort;//依赖注入
    public void setSort(baseSort Sort){//建立任意三种排序
        this.Sort=Sort;
    }
    public void doSort(int[] a){
        Sort.sort(a);
    }
}

Test

public class Test {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("输入10个数字");
        int[] a=new int[10];
        for (int i=0;i<10;i++){
            a[i]=scanner.nextInt();
        }

//        int[] a = new int[]{5, 8, 6, 7, 4, 3, 16, 2, 9, 12,};

        Factory factory = new Factory();
        baseSort quick_sort = new QuickSort();//向上转型
        factory.setSort(quick_sort);
        factory.doSort(a);

        for (int i = 0; i < 10; i++) {
            System.out.print(a[i] + " ");
        }
    }
}
xieyipeng commented 6 years ago

有了接口为什么还要有抽象类

package day2;

public interface Command {
    public void exe();
}
package day2;

/**
 * 长官
 */
public class Invoker {
    private Command command;

    public Invoker(Command command) {
        this.command = command;
    }

    public void action(){
        command.exe();
    }
}
package day2;

/**
 * 士兵
 */
public class Receiver {

    public void action(){
        System.out.println("command received!");
    }

}
package day2;

/**
 * 命令
 */
public class MyCommand implements Command{

    private Receiver receiver;

    public MyCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    @Override
    public void exe() {
        receiver.action();
    }
}
package day2;

public class test {
    public static void main(String[] args) {
        Receiver receiver=new Receiver();
        Command command=new MyCommand(receiver);
        Invoker invoker=new Invoker(command);
        invoker.action();
    }
}
xieyipeng commented 6 years ago

Stream学习(可以把Stream当成高版本的Iterator)

剖析Stream通用语发

1、创建Stream;

2、转换Stream,每次转换原有Stream对象不改变,返回一个新的Stream对象

3、对Stream进行聚合(Reduce)操作,获取想要的结果;

创建Stream

转换Stream

转换Stream其实就是把一个Stream通过某些行为转换成一个新的Stream。

汇聚(Reduce)Stream

先对java doc中的定义进行翻译:

汇聚操作(也称为折叠)接受一个元素序列为输入,反复使用某个合并操作,把序列中的元素合并成一个汇总的结果。比如查找一个数字列表的总和或者最大值,或者把这些数字累积成一个List对象。Stream接口有一些通用的汇聚操作,比如reduce()和collect();也有一些特定用途的汇聚操作,比如sum(),max()和count()。注意:sum方法不是所有的Stream对象都有的,只有IntStream、LongStream和DoubleStream是实例才有。

1、可变汇聚

可变汇聚对应的方法只有一个:collect,它可以把Stream中的要有元素收集到一个结果容器collection中。

List<Integer>nums=Lists.newArrayList(1,1,null,2,3,4,null,5,6,7,8,9,10);
List<Integer> numsWithoutNull = nums.stream()
.filter(num -> num != null)
.collect(() -> new ArrayList<Integer>(),
(list, item) -> list.add(item),
(list1, list2) -> list1.addAll(list2));
2、其他汇聚
List<Integer> ints = Lists.newArrayList(1,2,3,4,5,6,7,8,9,10);
System.out.println("ints sum is:" + ints.stream().reduce((sum, item) -&gt; sum + item).get());

遍历集合的操作,(list,map),eg:fori,foreach,stream,iterator;

import java.util.ArrayList; import java.util.Iterator; import java.util.List;

/**

import java.util.*; public class MapTest { public static void main(String[] args) { Map<Integer,Student>map=new HashMap<>(); map.put(1,new Student(12,"a")); map.put(2,new Student(18,"b")); map.put(3,new Student(14,"c")); map.put(4,new Student(16,"d")); map.put(5,new Student(13,"e")); map.put(6,new Student(11,"f")); map.put(7,new Student(12,"j")); map.put(8,new Student(15,"h")); map.put(9,new Student(14,"i")); map.put(10,new Student(18,"g"));

    System.out.println("fori :");
    for (Map.Entry<Integer,Student> entry:map.entrySet()) {
        System.out.print(entry.getKey() + " = ");
        System.out.println(entry.getValue().getAge() + " " + entry.getValue().getName());
    }

// for (int i = 0; i < map.size(); i++) { // System.out.println(map.get(i).getAge()); // } System.out.println("foreach :"); for (Map.Entry<Integer,Student> entry: map.entrySet()) { System.out.print(entry.getKey() + " = "); System.out.println(entry.getValue().getAge() + " " + entry.getValue().getName()); }

    System.out.println("iterator :");
    Iterator iterator=map.values().iterator();
    while (iterator.hasNext()){
        //System.out.println(iterator.next());
        Student student=(Student)iterator.next();
        System.out.println(student.getName()+" "+student.getAge());
    }
}

}

```java
//运行结果
fori :
1 = 12 a
2 = 18 b
3 = 14 c
4 = 16 d
5 = 13 e
6 = 11 f
7 = 12 j
8 = 15 h
9 = 14 i
10 = 18 g
foreach :
1 = 12 a
2 = 18 b
3 = 14 c
4 = 16 d
5 = 13 e
6 = 11 f
7 = 12 j
8 = 15 h
9 = 14 i
10 = 18 g
iterator :
a 12
b 18
c 14
d 16
e 13
f 11
j 12
h 15
i 14
g 18

/**