guyanqing 6 mēneši atpakaļ
vecāks
revīzija
93c0160a26
36 mainītis faili ar 706 papildinājumiem un 0 dzēšanām
  1. 34 0
      src/main/java/com/sf/day05/Animal.java
  2. 15 0
      src/main/java/com/sf/day05/Car.java
  3. 23 0
      src/main/java/com/sf/day05/Husband.java
  4. 262 0
      src/main/java/com/sf/day05/Te.java
  5. 31 0
      src/main/java/com/sf/day05/Wife.java
  6. 16 0
      src/main/java/com/sf/sx/day05/Te.java
  7. 8 0
      src/main/java/com/sf/sx/day05/lx/Animal.java
  8. 13 0
      src/main/java/com/sf/sx/day05/lx/Car.java
  9. 14 0
      src/main/java/com/sf/sx/day05/lx/NewCar.java
  10. 15 0
      src/main/java/com/sf/sx/day05/lx/OldCar.java
  11. 10 0
      src/main/java/com/sf/sx/day05/lx/impl/AnimalImpl.java
  12. 57 0
      src/main/java/com/sf/sx/day06/TE.java
  13. 60 0
      src/main/java/com/sf/sx/day07/Data.java
  14. 5 0
      src/main/java/com/sf/sx/day07/MyPredicate.java
  15. 60 0
      src/main/java/com/sf/sx/day07/Product.java
  16. 83 0
      src/main/java/com/sf/sx/day07/Te.java
  17. BIN
      target/classes/com/sf/day04/HomeWork01.class
  18. BIN
      target/classes/com/sf/day05/Animal.class
  19. BIN
      target/classes/com/sf/day05/Car.class
  20. BIN
      target/classes/com/sf/day05/Husband.class
  21. BIN
      target/classes/com/sf/day05/Te.class
  22. BIN
      target/classes/com/sf/day05/Wife.class
  23. BIN
      target/classes/com/sf/sx/day05/Te.class
  24. BIN
      target/classes/com/sf/sx/day05/lx/Animal.class
  25. BIN
      target/classes/com/sf/sx/day05/lx/Car.class
  26. BIN
      target/classes/com/sf/sx/day05/lx/NewCar.class
  27. BIN
      target/classes/com/sf/sx/day05/lx/OldCar.class
  28. BIN
      target/classes/com/sf/sx/day05/lx/impl/AnimalImpl.class
  29. BIN
      target/classes/com/sf/sx/day06/TE.class
  30. BIN
      target/classes/com/sf/sx/day07/Data.class
  31. BIN
      target/classes/com/sf/sx/day07/MyPredicate.class
  32. BIN
      target/classes/com/sf/sx/day07/Product.class
  33. BIN
      target/classes/com/sf/sx/day07/Te$1.class
  34. BIN
      target/classes/com/sf/sx/day07/Te$2.class
  35. BIN
      target/classes/com/sf/sx/day07/Te$3.class
  36. BIN
      target/classes/com/sf/sx/day07/Te.class

+ 34 - 0
src/main/java/com/sf/day05/Animal.java

@@ -0,0 +1,34 @@
+package com.sf.day05;
+
+public class Animal {
+
+    //  属性   名字和年龄
+    /**
+     * 包装类和基本数据类型
+     * 装箱  拆箱     int   0    null
+     */
+    public String name;
+    public Integer age;
+    @Override
+    public String toString() {
+        return "Animal{" +
+                "name='" + name + '\'' +
+                ", age=" + age +
+                '}';
+    }
+    //成员方法
+    public void eat() {
+        System.out.println("动物吃");
+    }
+    public void run() {
+        System.out.println("动物跑");
+    }
+
+    public static void main(String[] args) {
+        Animal animal = new Animal();
+        animal.age = 12;
+        animal.name = "sam";
+        animal.eat();
+        System.out.println(animal.toString());   //com.sf.day05.Animal@45ee12a7
+    }
+}

+ 15 - 0
src/main/java/com/sf/day05/Car.java

@@ -0,0 +1,15 @@
+package com.sf.day05;
+
+
+public class Car {
+    //属性
+    public String color;
+    public double speed;
+    public int   weight;
+
+    //动作 行为
+    public void playMusic(){
+        System.out.println("播放音乐");
+
+    }
+}

+ 23 - 0
src/main/java/com/sf/day05/Husband.java

@@ -0,0 +1,23 @@
+package com.sf.day05;
+
+public class Husband {
+
+    String name;
+    int age;
+    String desc;
+
+    Wife wife;
+
+
+    @Override
+    public String toString() {
+        return "Husband{" +
+                "name='" + name + '\'' +
+                ", age=" + age +
+                ", desc='" + desc + '\'' +
+                ", wife=" + wife +
+                '}';
+    }
+}
+    // 妻子类
+

+ 262 - 0
src/main/java/com/sf/day05/Te.java

@@ -0,0 +1,262 @@
+package com.sf.day05;
+
+import org.junit.Test;
+import java.util.*;
+import java.util.Scanner;
+
+public class Te {
+    @Test
+    public void t1(){
+
+        /**
+         * 二维数组的定义
+         *
+         * 动态:
+         * int[] arr = new int[n];
+         * int[][] arr = new int[m][n];
+         * m:代表的是二维数组的长度
+         * n:代表的是二维数组里一维数组的长度
+         *
+         * int[][] arr = new int[3][2];
+         * [ [1,2] , [1,1] ,[1,9 ]  ]        9  :   arr[2][1]
+         */
+        int[][] arr ;
+        int arr1[][];
+        int[] arr2[];
+
+        int[] arr3 = new int[]{};
+        String[][] grade = new String[][]{
+                {"小舞","唐三","胖子"},{"戴沐白","朱竹清"},{"唐晨","唐浩","唐四","唐五"}
+        };
+    }
+
+    /**
+     * 商城,
+     * 第一季度 每一个月的销售额 分别是 12,14,24,
+     * 第二季度 每一个月的销售额 分别是 23,18,35,
+     * 第三季度 每一个月的销售额 分别是 33,48,25,
+     * 第四季度 每一个月的销售额 分别是 27,28,35,
+     * 二维数组实现
+     * 求出 每个月的平均销售额,和总销售额。
+     */
+    @Test
+    public void t2(){
+    int[][] arr = {{12,14,24},{23,18,35},{33,48,25},{27,28,35}};
+    double sum = 0.0;  //总销售额
+    int count = 0;
+    for (int i = 0;i<arr.length;i++){
+        for (int j = 0;j<arr[i].length;j++){
+           sum += arr[i][j];
+           count++;
+        }
+    }
+        System.out.println("总销售额"+sum);
+        System.out.println("平均销售额"+sum/count);
+    }
+
+
+    /**
+     * 判断一个数组是否为平衡数组
+     */
+    @Test
+    public void t3(){
+
+    }
+
+
+    public boolean isPHArray(int[] arr){
+        for (int i = 0,j = arr.length-1;i<j;i++,j--){
+            if(arr[i] != arr[j]){
+                return false;
+            }
+        }
+        return true;
+
+    }
+
+
+    //8、求数组中元素的最短距离   5、找数组平衡数
+
+    /**
+     * 二分查找
+     */
+    @Test
+    public void t4(){
+        int[] arr = {-23,9,10,34,56,234,567};
+        int head = 0,end = arr.length-1;
+        int value = 2341;
+        boolean flag = true;
+        while (head <= end){
+            int mid = (head+end)/2;
+            if(value == arr[mid]){
+                System.out.println("中间值"+arr[mid]);
+                flag = false;
+                break;
+            }else if (value < arr[mid]){
+                end = mid-1;
+            }else {
+                head = mid+1;
+            }
+        }
+        if(flag){
+            System.out.println("no~");
+        }
+    }
+
+    /**
+     * 冒泡排序
+     */
+    @Test
+    public void t5(){
+        int[] arr = {23,9,10,34,56,234,567};
+        //  不用讲
+
+        System.out.println(Arrays.toString(arr));
+//        System.out.println(Arrays.sort(arr,1,2));
+    }
+
+
+    // 已知某个数组中只有1个数字的次数出现奇数次,请找出这个数字。
+    @Test
+    public void t6(){
+        int[] arr = {2,2,5,7,7};
+       int value = arr[0];
+        for (int i = 1;i<arr.length;i++){
+            value ^= arr[i];
+        }
+
+        System.out.println(value);
+
+    }
+
+
+    /**
+     * 公司年会有一个寻找锦鲤的游戏,
+     * 每一个员工随意写一个字,
+     * 如果在“锦鲤”词库中有这个字,
+     * 那么就奖励500元锦鲤红包,否则就没有,每人只能玩一次。
+     */
+    public static void main(String[] args) {
+        char[] koiFishWords = {'一','今','地','定','爱','年','开','我','果','火','爱','扣','结','花','钉','遍'};
+        Scanner sc = new Scanner(System.in);
+        char next = sc.next().charAt(0);
+        for (char koiFishWord : koiFishWords) {
+            if(koiFishWord == next){
+                System.out.println("500");
+                break;
+            }else {
+                System.out.println("no");
+                break;
+            }
+        }
+
+    }
+
+    //mm  模拟大乐透
+
+    /**
+     * ## 2、模拟大乐透
+     *
+     * 大乐透(前区“35选5”+后区“12选2”),即前区在1-35之间的号码中随机选取5个,后区在1-12之间的号码中随机选取2个,组成一期的中奖号码,请用程序模拟产生一组大乐透中奖号码。
+     *
+     * 开发提示:
+     *
+     * - 声明一个int类型的数组front,长度为35,默认值都是0;
+     * - 声明一个int类型的数组after,长度为12,默认值都是0;
+     * - 随机产生[0,35)之间的整数。如果随机产生的是0,那么就把front[0]修改为1,如果随机产生的是5,那么就把front[5]修改为1,如果随机产生的是10,就把front[10]修改为1。但是如果本次随机产生的是5,而front[5]已经是1了,那么需要重新随机产生一个整数。用这种方式把front数组的5个元素修改为1。
+     * - 随机产生[0,12)之间的整数。使用同样的方式,把after数组的2个元素修改为1。
+     * - 遍历front和after数组,输出大乐透中奖号码,判断front和after数组元素是否为1,如果为1,就显示它的下标+1值。
+     */
+
+@Test
+public void t16(){
+    int[]  front = new int[35];
+    int[]  after = new int[12];
+
+    for (int i =1;i<6;i++){
+        int n = (int) (Math.random()*35);
+        if(front[n] == 0){
+            front[n] =1;
+        }else {
+            i--;
+        }
+//        System.out.println(Arrays.toString(front));
+    }
+
+//    System.out.println(Arrays.toString(front));
+
+    for (int i =1;i<3;i++){
+        int n = (int) (Math.random()*12);
+        if(after[n] == 0){
+            after[n] =1;
+        }else {
+            i--;
+        }
+    }
+
+
+    for (int i =0;i<35;i++){
+        if(front[i] == 1){
+            System.out.println(i+1);
+        }
+    }
+
+    for (int i =0;i<12;i++){
+        if(after[i] == 1){
+            System.out.println(i+1);
+        }
+    }
+}
+
+    /**
+     * 7、查找数组中个数过半的数字
+     */
+    @Test
+public void t7(){
+        int[] arr = {1, 2, 3, 2, 2, 2, 5, 4, 2};
+        Arrays.sort(arr);
+        int mid = arr.length/2;
+        System.out.println(arr[mid]);
+        int count = 0;
+        for (int i : arr) {
+            if(i == arr[mid]){
+                count++;
+            }
+        }
+        System.out.println(count);
+}
+
+//     //8、求数组中元素的最短距离
+
+    /**
+     * 随机产生10个[0,100)之间整数存储到数组中,找出数组中的两个元素x和y,使得(x - y)绝对值最小。
+     *
+     * 开发提示:
+     *
+     * - 将数组进行排序
+     * - 求相邻元素的差,差值最小值就是最短距离
+     */
+    @Test
+    public void t8(){
+        int[] arr = new  int[10];
+        for (int i = 0;i<10;i++){
+            arr[i] = (int)(Math.random()*100);
+        }
+        Arrays.sort(arr);
+        System.out.println(Arrays.toString(arr));
+        int min = arr[1] - arr[0];
+        int index1 = arr[0];
+        int index2 = arr[1];
+        for (int i = 0;i<9;i++){
+            int dis = arr[i+1] - arr[i];
+            if(dis <= min){
+                min = dis;
+                index1 = i;
+                index2 = i+1;
+            }
+        }
+        System.out.println(min);
+        System.out.println(arr[index1]);
+        System.out.println(arr[index2]);
+    }
+}

+ 31 - 0
src/main/java/com/sf/day05/Wife.java

@@ -0,0 +1,31 @@
+package com.sf.day05;
+
+public class Wife {
+    String name;
+    int age;
+    String desc;
+//    Husband husband;
+
+    @Override
+    public String toString() {
+        return "Wife{" +
+                "name='" + name + '\'' +
+                ", age=" + age +
+                ", desc='" + desc + '\'' +
+//                ", husband=" + husband +
+                '}';
+    }
+
+    public static void main(String[] args) {
+        Husband husband1 = new Husband();
+        husband1.age = 32;
+        husband1.desc = "nan";
+        husband1.name = "zs";
+        Wife wife = new Wife();
+        wife.age = 20;
+        wife.name= "ls";
+//        wife.husband = husband1;
+        husband1.wife = wife;
+        System.out.println(husband1);
+    }
+}

+ 16 - 0
src/main/java/com/sf/sx/day05/Te.java

@@ -0,0 +1,16 @@
+package com.sf.sx.day05;
+
+import org.junit.Test;
+
+public class Te {
+    @Test
+    public void t1(){
+        /**
+         * 小明的父亲有一辆20年的小汽车:汽车中有 启动  行驶的方法。
+         *
+         * 小明买了又买了一辆智能汽车,汽车中也有 启动 行驶 的方法。
+         *
+         * 这是对于父类的启动行驶的方法,在智能汽车中重写这两个方法就可以了,因为需求不同。
+         */
+    }
+}

+ 8 - 0
src/main/java/com/sf/sx/day05/lx/Animal.java

@@ -0,0 +1,8 @@
+package com.sf.sx.day05.lx;
+//  函数式接口
+@FunctionalInterface
+public interface Animal {
+
+    //  抽象方法
+    void  run();
+}

+ 13 - 0
src/main/java/com/sf/sx/day05/lx/Car.java

@@ -0,0 +1,13 @@
+package com.sf.sx.day05.lx;
+
+public abstract class Car {
+
+    public void start() {
+
+    }
+
+
+    public void drive(){
+        System.out.println("汽车、行驶");
+    }
+}

+ 14 - 0
src/main/java/com/sf/sx/day05/lx/NewCar.java

@@ -0,0 +1,14 @@
+package com.sf.sx.day05.lx;
+
+public class NewCar extends  Car{
+    @Override
+    public void start() {
+        System.out.println("智能汽车启动");
+    }
+
+
+    @Override
+    public void drive() {
+        System.out.println("智能汽车行驶");
+    }
+}

+ 15 - 0
src/main/java/com/sf/sx/day05/lx/OldCar.java

@@ -0,0 +1,15 @@
+package com.sf.sx.day05.lx;
+
+public class OldCar extends Car{
+
+    @Override
+    public void start() {
+        System.out.println("老式汽车启动");
+    }
+
+
+    @Override
+    public void drive() {
+        System.out.println("老式汽车行驶");
+    }
+}

+ 10 - 0
src/main/java/com/sf/sx/day05/lx/impl/AnimalImpl.java

@@ -0,0 +1,10 @@
+package com.sf.sx.day05.lx.impl;
+
+import com.sf.sx.day05.lx.Animal;
+
+public class AnimalImpl implements Animal {
+    @Override
+    public void run() {
+        System.out.println("实现类进行接口中的抽象方法实现");
+    }
+}

+ 57 - 0
src/main/java/com/sf/sx/day06/TE.java

@@ -0,0 +1,57 @@
+package com.sf.sx.day06;
+
+import org.junit.Test;
+
+import java.util.*;
+
+public class TE {
+    @Test
+    public void t1(){
+        List<String> list = new ArrayList();
+        List<String> list2 = new ArrayList();
+        list.add("aa");
+        list.add("aa1");
+        list.add("bb");
+//        for (String s : list) {
+//            System.out.println(s);
+//        }
+
+        list2.add("aa");
+        list2.add("bb");
+        list.addAll(list2);
+        System.out.println(list);
+        for (int i = 0; i <list.size(); i++) {
+            if(list.get(i).contains("aa")){
+                list.remove(i);
+                i--;
+            }
+        }
+        System.out.println(list);
+
+        //  采用迭代器进行删除
+        Iterator<String> iterator = list.iterator();
+        while (iterator.hasNext()){
+            if(iterator.next().contains("aa")){
+                iterator.remove();
+            }
+        }
+        System.out.println(list);
+    }
+
+
+    @Test
+    public void t2(){
+        Set<String> set = new HashSet<>();
+        set.add("aof");
+        set.add("qqq");
+        set.add("qqq");
+        System.out.println(set);
+        Map<String,String> map = new HashMap();
+        map.put("name","admin");
+        map.put("userName","admin1");
+        System.out.println(map);
+        String s = map.get("name");
+        System.out.println(s);
+
+    }
+}

+ 60 - 0
src/main/java/com/sf/sx/day07/Data.java

@@ -0,0 +1,60 @@
+package com.sf.sx.day07;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Data {
+    // 创建一个集合 用于承装数据
+    public static List<Product> products = new ArrayList<>();
+    static {
+        products.add(new Product(1L, "苹果手机", 8888.88,"手机"));
+        products.add(new Product(2L, "华为手机", 6666.66,"手机"));
+        products.add(new Product(3L, "联想笔记本",7777.77,"电脑"));
+        products.add(new Product(4L, "机械键盘", 999.99,"键盘"));
+        products.add(new Product(5L, "雷蛇鼠标", 222.22,"鼠标"));
+    }
+
+
+    public static void main(String[] args) {
+        //   帅选出商品名字包含手机的商品
+        List<Product> productByName = findProductByName(Data.products);
+        System.out.println(productByName);
+        List<Product> productByPrice = findProductByPrice(Data.products);
+        System.out.println(productByPrice);
+
+    }
+    /**
+     * 筛选出商品名字包含手机的商品
+     * @param products
+     * @return
+     */
+    public static List<Product> findProductByName(List<Product> products){
+        List<Product> list = new ArrayList<>();
+        for (Product product : products) {
+            if(product.getName().contains("手机")){
+                list.add(product);
+            }
+        }
+        return  list;
+    }
+
+    /**
+     * 价格大于1000
+     * @param products
+     * @return
+     */
+    public static List<Product> findProductByPrice(List<Product> products){
+        List<Product> list = new ArrayList<>();
+        for (Product product : products) {
+            if(product.getPrice()>1000){
+                list.add(product);
+            }
+        }
+        return  list;
+    }
+
+
+
+}

+ 5 - 0
src/main/java/com/sf/sx/day07/MyPredicate.java

@@ -0,0 +1,5 @@
+package com.sf.sx.day07;
+
+public interface MyPredicate {
+    boolean test(Product product);
+}

+ 60 - 0
src/main/java/com/sf/sx/day07/Product.java

@@ -0,0 +1,60 @@
+package com.sf.sx.day07;
+
+public class Product {
+    private Long id;
+    private String name;
+    private double price;
+    private String type;
+
+    public Product() {
+    }
+
+    public Product(Long id, String name, double price, String type) {
+        this.id = id;
+        this.name = name;
+        this.price = price;
+        this.type = type;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public double getPrice() {
+        return price;
+    }
+
+    public void setPrice(double price) {
+        this.price = price;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    @Override
+    public String toString() {
+        return "Product{" +
+                "id=" + id +
+                ", name='" + name + '\'' +
+                ", price=" + price +
+                ", type='" + type + '\'' +
+                '}';
+    }
+}

+ 83 - 0
src/main/java/com/sf/sx/day07/Te.java

@@ -0,0 +1,83 @@
+package com.sf.sx.day07;
+
+import org.junit.Test;
+import java.util.ArrayList;
+import java.util.List;
+
+
+public class Te {
+    @Test
+    public void t1(){
+        int[] arr = {1,2,3,4,3,2,1};
+        for (int left = 0,right = arr.length-1;left<right;left++,right--){
+            if(arr[left] != arr[right]){
+                break;
+            }
+        }
+    }
+
+    @Test
+    public void t2(){
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                System.out.println("线程启动");
+            }
+        }).start();
+
+        //  用新特性lambda进行编写
+        new Thread(() -> System.out.println("线程启动")).start();
+    }
+
+    /**
+     *   匿名内部类   筛选出所有名称包含手机的商品
+     */
+    @Test
+    public void t3(){
+        List<Product> productList = findProductByCondition(Data.products, new MyPredicate() {
+            @Override
+            public boolean test(Product product) {
+                return product.getName().contains("手机");
+            }
+        });
+        System.out.println(productList);
+
+
+        List<Product> productListByPrice = findProductByCondition(Data.products, new MyPredicate() {
+            @Override
+            public boolean test(Product product) {
+                return product.getPrice()>1000;
+            }
+        });
+        System.out.println(productListByPrice);
+    }
+
+
+    public static List<Product> findProductByCondition(List<Product> products,MyPredicate myPredicate){
+          List<Product> list = new ArrayList<>();
+        for (Product product : products) {
+            if(myPredicate.test(product)){
+                list.add(product);
+            }
+        }
+        return list;
+
+    }
+
+    /**
+     * 采用lambda表达式的方式
+     */
+    @Test
+    public void t6(){
+        //需求1: 筛选出所有名称包含手机的商品
+        List<Product> productListByName = findProductByCondition(Data.products,
+                (product) -> product.getName().contains("手机"));
+        System.out.println(productListByName);
+
+        // 需求2: 筛选出所有价格大于1000的商品
+        List<Product> productListByPrice = findProductByCondition(Data.products,
+                (product) -> product.getPrice()>1000);
+        System.out.println(productListByPrice);
+    }
+
+}

BIN
target/classes/com/sf/day04/HomeWork01.class


BIN
target/classes/com/sf/day05/Animal.class


BIN
target/classes/com/sf/day05/Car.class


BIN
target/classes/com/sf/day05/Husband.class


BIN
target/classes/com/sf/day05/Te.class


BIN
target/classes/com/sf/day05/Wife.class


BIN
target/classes/com/sf/sx/day05/Te.class


BIN
target/classes/com/sf/sx/day05/lx/Animal.class


BIN
target/classes/com/sf/sx/day05/lx/Car.class


BIN
target/classes/com/sf/sx/day05/lx/NewCar.class


BIN
target/classes/com/sf/sx/day05/lx/OldCar.class


BIN
target/classes/com/sf/sx/day05/lx/impl/AnimalImpl.class


BIN
target/classes/com/sf/sx/day06/TE.class


BIN
target/classes/com/sf/sx/day07/Data.class


BIN
target/classes/com/sf/sx/day07/MyPredicate.class


BIN
target/classes/com/sf/sx/day07/Product.class


BIN
target/classes/com/sf/sx/day07/Te$1.class


BIN
target/classes/com/sf/sx/day07/Te$2.class


BIN
target/classes/com/sf/sx/day07/Te$3.class


BIN
target/classes/com/sf/sx/day07/Te.class