android-nuc / 17_Java_Train

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

1614011121 申逸凡 #13

Open ZBDXShenYifan opened 6 years ago

ZBDXShenYifan commented 6 years ago

package homework1;

public class BaseSort 
{
     public void sort(double [] list)
     {
         System.out.println("排序算法");
     }

}

public class BubbleSort extends BaseSort
{
    public void sort(double[] list)
    {
       super.sort(list);
       System.out.println("冒泡算法的结果");
        boolean needNextPass = true;
        //设置一个布尔变量,如果在某次遍历中没有发生交换,说明所有的元素都排好了顺序,不需要再进行交换
        for(int k = 1; k < list.length && needNextPass; k++)
        {
            //需要进行length - 1次遍历
            needNextPass = true;
            for(int i = 0; i < list.length - k; i++)
            {//第k次遍历不需要考虑最后共k-1个元素
                if(list[i] > list[i + 1])
                {
                    double temp = list[i];
                    list[i] = list[i+1];
                    list[i+1] = temp;
                    needNextPass = true;
                    //进行了交换,说明还没有都排好序,需要进行下一次遍历
                }
            }
        }
    }

}

public class InsertionSort extends BaseSort
{
    public void sort(double[] list)
    {
        super.sort(list);
        System.out.println("插入算法的结果");
        for(int i = 1; i < list.length; i++)
        {
            //把list[i] 插入到 0 ~ i-1;   
            double currentElement = list[i];
            int k;
            for(k = i - 1; k >=0 && list[k] > currentElement; k--)
            {
                list[k+1] = list[k];
            }

            list[k + 1] = currentElement;
        }

    }

}

public class QuickSort extends BaseSort
{
    //对数组进行排序
    public void sort(double[] list)
    {
        super.sort(list);
        System.out.println("快速算法的结果");
        quickSort(list, 0, list.length-1);

    }

    //对特定的子数组进行排序
    private void quickSort(double[] list, int first, int last)
    {
        if(last > first)
        {
            int pivotIndex = partition(list, first, last);
            quickSort(list, first, pivotIndex - 1);
            quickSort(list, pivotIndex + 1, last);

        }
    }

    //找到主元,并合理放置主元,返回放置好的主元的索引
    private int partition(double[] list, int first, int last)
    {
        double pivot = list[first];
        int low = first + 1;  //从前找值低的索引
        int high = last; //从后找值高的索引

        while(high > low)
        {
            while(low <= high && list[low] <= pivot)
                low++;

            while(low <= high && list[high] > pivot)
                high--;

            if(high > low)
            {
                double temp = list[high];
                list[high] = list[low];
                list[low] = temp;

            }
        }

        while(high > first && list[high] >= pivot)
            high--;

        if(pivot > list[high])
        {
            list[first] = list[high];
            list[high] = pivot;
            return high;
        }
        else
        {
            return first;
        }
    }

}

public class SelectionSort extends BaseSort
{
    public void sort(double[] list)
    {
        super.sort(list);
        System.out.println("选择算法的结果");
       for(int i = 0; i < list.length - 1; i++)
       {
           //找到数列从i到  length - 1 的最小值
           double currentMin = list[i];
           int currentMinIndex = i;

           for(int j = i + 1; j < list.length; j++)
               if(currentMin > list[j])
               {
                   currentMin = list[j];
                   currentMinIndex = j;
               }
           //找到最小值及其索引

        //将最小值与第i个位置的值互换,实现排序   
       if(currentMinIndex != i)
       {
           list[currentMinIndex] = list[i];
           list[i] = currentMin;
       }
    }
    }
}

public class Factory 
{
    private BaseSort sort;
    public void setSort(BaseSort sort)
    {
        this.sort = sort;
    }

    public void doSort(double[] a)
    {
        sort.sort(a);
    }

}

import java.util.*;
public class Test {

    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        Scanner keyboard = new Scanner(System.in);
        double[] list = new double[10];
        System.out.println("请输入10个数字");
        for(int i = 0; i < list.length; i++)
        {
            list[i] = keyboard.nextDouble();
        }

        Factory factory = new Factory();
        BaseSort select_sort = new SelectionSort();
        factory.setSort(select_sort);
        factory.doSort(list);
        for(double element : list)
        {
            System.out.print(element + " ");
        }
        System.out.println();

        BaseSort bubbleSort = new BubbleSort();
        factory.setSort(bubbleSort);
        factory.doSort(list);
        for(double element : list)
        {
            System.out.print(element + " ");
        }
        System.out.println();

        BaseSort insertionSort = new InsertionSort();
        factory.setSort(insertionSort);
        factory.doSort(list);
        for(double element : list)
        {
            System.out.print(element + " ");
        }
        System.out.println();

        BaseSort quickSort = new QuickSort();
        factory.setSort(quickSort );
        factory.doSort(list);
        for(double element : list)
        {
            System.out.print(element + " ");
        }
        System.out.println();

    }

}

```java
ZBDXShenYifan commented 6 years ago

第二次作业

熟悉一种设计模式(建造者模式)

     工厂类提供的是建造单个类的模式,而建造者模式则是将各种产品集中起来进行管理,用来建造复合对象,建造者模式将很多功能集成到一个类里,这个类可以创造出比较复杂的东西。
     在本例中,Builder具有插入排序,冒泡排序,快速排序和选择排序的功能。

     与工厂模式的区分:
    工厂模式关注的是建造单个产品,建造者模式则是关注与创建多功能的复合对象。
import java.util.*;

public class Builder 
{
    private BaseSort sort;

    public void readInput(double[] a)
    {
        System.out.println("请输入" + a.length + "个数字");
        Scanner input = new Scanner(System.in);
        for(int i = 0; i < a.length; i++)
        {
            a[i] = input.nextDouble();
        }

    }

    public void writeOutput(double[] a)
    {
        for(int i = 0; i < a.length; i++)
        {
            System.out.print(a[i] + " ");
        }

        System.out.println();
    }
    public void produceBubbleSort(double[] a)
    {
        this.sort = new BubbleSort();
        this.doSort(a);

    }

    public void produceInsertionSort(double[] a)
    {
        this.sort = new InsertionSort();
        this.doSort(a);
    }

    public void produceQuickSort(double[] a)
    {
        this.sort = new QuickSort();
        this.doSort(a);
    }

    public void produceSelectionSort(double[] a)
    {
        this.sort = new SelectionSort();
        this.doSort(a);

    }

    public void doSort(double[] a)
    {
        this.readInput(a);
        sort.sort(a);
        this.writeOutput(a);

    }

}

import java.util.Scanner;

public class TestBuilder 
{

    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        Builder build = new Builder();
        double[] a1 = new double[10];
        double[] a2 = new double[10];
        double[] a3 = new double[10];
        double[] a4 = new double[10];

        build.produceBubbleSort(a1);
        build.produceInsertionSort(a2);
        build.produceQuickSort(a3);
        build.produceSelectionSort(a4);

    }

}

运行结果:

请输入10个数字 1 2 3 6 5 4 9 8 7 0 排序算法 冒泡算法的结果 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 请输入10个数字 9 8 7 6 5 4 3 2 1 0 排序算法 插入算法的结果 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 请输入10个数字

9 8 7 3 2 1 4 5 6 0 排序算法 快速算法的结果 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 请输入10个数字 7 5 3 4 6 8 3 1 0 2 排序算法 选择算法的结果 0.0 1.0 2.0 3.0 3.0 4.0 5.0 6.0 7.0 8.0

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

接口的default methods只能是public ,抽象类的abstract method可以是protected.

Java 8的接口上的default method最初的设计目的是让已经存在的接口可以演化——添加新方法 而不需要实现该接口的类做任何改变(甚至不需要重新编译),Java 8的接口暂时还无法完全替代抽象类,它不能拥有状态,只能提供公有方法的默认实现。

ZBDXShenYifan commented 6 years ago

第三次作业()

一、 对Map的四种遍历

1.第一种遍历:利用Map.KeySet + foreach遍历value 2.第二种遍历:通过Map.entrySet使用iterator遍历Key和value 3.第三种遍历: 通过Map.entrySet + foreach循环 4.第四种遍历:通过Map.value() + foreach来遍历所有的value

package homework3;
import java.util.*;

public class TestMapIterator 
{

    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        Map<String, String> studentInformation = new HashMap<>();
        studentInformation.put("1614011101", "小张");
        studentInformation.put("1614011102", "小赵");
        studentInformation.put("1614011103", "小可");
        studentInformation.put("1614011104", "小红");
        studentInformation.put("1614011105", "小军");
        studentInformation.put("1614011106", "小李");
        studentInformation.put("1614011107", "小刘");
        studentInformation.put("1614011108", "小王");

        System.out.println("第一种遍历:利用Map.KeySet遍历value");
        //KeySet返回所有Key的值
        for(String learningNumber: studentInformation.keySet())
            System.out.println("value = " + studentInformation.get(learningNumber));

        System.out.println();
        System.out.println("第二种遍历:通过Map.entrySet使用iterator遍历Key和value");
        Iterator<Map.Entry<String, String>> iterator = studentInformation.entrySet().iterator();
        //.entrySet()返回的是键值对的一个集合
        //建议一个迭代器对象,它迭代的元素Map中的键值对
        while(iterator.hasNext())
        {
            Map.Entry<String, String> entry = iterator.next();
            System.out.println("key = " + entry.getKey() + " value = " + entry.getValue());
        }
        System.out.println();

        System.out.println("第三种遍历: 通过Map.entrySet + foreach循环");
        for(Map.Entry<String, String> entry : studentInformation.entrySet())
            System.out.println("key = " + entry.getKey() + " value = " + entry.getValue());

        System.out.println();

        System.out.println("通过Map.value()来遍历所有的value,但不能遍历key");
           for(String aValue : studentInformation.values())
               System.out.println("value = " + aValue);

    }

}

运行结果

第一种遍历:利用Map.KeySet遍历value value = 小红 value = 小可 value = 小李 value = 小军 value = 小赵 value = 小张 value = 小王 value = 小刘

第二种遍历:通过Map.entrySet使用iterator遍历Key和value key = 1614011104 value = 小红 key = 1614011103 value = 小可 key = 1614011106 value = 小李 key = 1614011105 value = 小军 key = 1614011102 value = 小赵 key = 1614011101 value = 小张 key = 1614011108 value = 小王 key = 1614011107 value = 小刘

第三种遍历: 通过Map.entrySet + foreach循环 key = 1614011104 value = 小红 key = 1614011103 value = 小可 key = 1614011106 value = 小李 key = 1614011105 value = 小军 key = 1614011102 value = 小赵 key = 1614011101 value = 小张 key = 1614011108 value = 小王 key = 1614011107 value = 小刘

通过Map.value()来遍历所有的value,但不能遍历key value = 小红 value = 小可 value = 小李 value = 小军 value = 小赵 value = 小张 value = 小王 value = 小刘

二、对Stream的入门级学习

import java.util.*;
import java.util.stream.Stream;
public class TestStream 
{
    /**
     * 原始版本的Iterator,用户只能一个一个的遍历元素并对他执行某些操作,而Stream,用户只需要
     * 给出对其包含的元素执行什么操作
     */

    public static void main(String[] args)
    {
        System.out.println("利用stream的建造集合与遍历");
        Stream<Integer> integerStream = Stream.of(1,3,3,4,5,6,6,8,9);
        //forEach()
        integerStream.forEach(number -> System.out.print(number + " "));
        System.out.println();
        //filter()
        System.out.println("stream中过滤出大于等于5的元素");
        Stream<Integer> integerStream2 = Stream.of(1,3,3,4,5,6,6,8,9);
        integerStream2.filter(number -> number >= 5)
           .forEach(number -> System.out.print(number + " "));
        System.out.println();

        //distinct()
        System.out.println("删除重复的元素");
        Stream<Integer> integerStream3 = Stream.of(1,3,3,4,5,6,6,8,9);
        integerStream3.distinct()
          .forEach(number -> System.out.print(number + " "));
                System.out.println();

        //sorted()
                System.out.println("进行降序排序");
        Stream<Integer> integerStream4 = Stream.of(1,3,3,4,5,6,6,8,9);
        integerStream4.sorted((num1, num2) -> num2 - num1)
        .forEach(number -> System.out.print(number + " "));
        System.out.println();

        //map()
        System.out.println("对所有元素进行+1的操作");
        Stream<Integer> integerStream5 = Stream.of(1,3,3,4,5,6,6,8,9);
        integerStream5.map(number-> number + 1)
        .forEach(number -> System.out.print(number + " "));
        System.out.println();

    //  flatMap()
        System.out.println("将两个集合合并");
        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        ArrayList<Integer> list2 = new ArrayList<>();
        list2.add(4);
        list2.add(5);
        list2.add(6);
        Stream<ArrayList<Integer>> stream = Stream.of(list1,list2);
        stream.flatMap(list -> list.stream())
        .forEach(number -> System.out.print(number + " "));
        System.out.println();
    }

}

运行结果

利用stream的建造集合与遍历 1 3 3 4 5 6 6 8 9 stream中过滤出大于等于5的元素 5 6 6 8 9 删除重复的元素 1 3 4 5 6 8 9 进行降序排序 9 8 6 6 5 4 3 3 1 对所有元素进行+1的操作 2 4 4 5 6 7 7 9 10 将两个集合合并 1 2 3 4 5 6