• 学习java第19天


    1.泛型

    一种java的语言特征

    使用泛型更安全,适用更广泛

    *自定义泛型

    import java.util.*;
    class GenericTreeClass {
     public static void main(String[] args){
      TNode<String> t = new TNode<>("Roo");
      t.add("Left"); t.add("Middle"); t.add("Right");
      t.getChild(0).add("aaa");
      t.getChild(0).add("bbb");
      t.traverse();
     }
    }
    class TNode<T>
    {
      private T value;
      private ArrayList<TNode<T>> children = new ArrayList<>();
      TNode(T v) { this.value = v; }
      public T getValue() { return this.value; }
      public void add(T v) {
        TNode<T> child = new TNode<>(v);
        this.children.add(child);
      }
      public TNode<T> getChild(int i) {
        if ((i < 0) || (i > this.children.size())) return null;
        return (TNode<T>)this.children.get(i);
      }
      public void traverse() {
        System.out.println(this.value);
        for (TNode child : this.children)
          child.traverse();
      }
    }
    *自定义泛型方法
    import java.util.*;
    class GenericMethod {
     public static void main(String[] args){
      Date date = BeanUtil.<Date>getInstance("java.util.Date");
      System.out.println(date);
     }
    }
    class BeanUtil{
     public static <T> T getInstance( String clzName ){
      try
      {
       Class c = Class.forName(clzName);
       return (T) c.newInstance();
      }
      catch (ClassNotFoundException ex){}
      catch (InstantiationException ex){}
      catch (IllegalAccessException ex){}
      return null;
     }
    }
     
    2.协变和逆变
    协变用于获取,用于out,用于Producer,逆变用于加入,用于in,用于Consumer
    3.遍试
    穷举
    例如求水仙花数
    public class All_153
    {
     public static void main(String args[]){
      for( int a=1; a<=9; a++ )
       for( int b=0; b<=9; b++ )
        for( int c=0; c<=9; c++ )
         if( a*a*a+b*b*b+c*c*c == 100*a+10*b+c)
          System.out.println( 100*a+10*b+c );
      
     }
    }
    遍试算法基本模式: for(;;){ if();}
     
    4.迭代
    多次利用同一公式进行计算,将计算结果再带入公式进行计算,让解更精确
    public class Sqrt
    {
     public static void main(String args[]){
      System.out.println( sqrt( 98.0 ) );
      System.out.println( Math.sqrt(98.0) );
     }
     static double sqrt( double a ){
      double x=1.0;
      do{
       x = ( x + a/x ) /2;
       System.out.println( x + "," + a/x );
      }while( Math.abs(x*x-a)/a > 1e-6 );
      return x;
     }
    }
    迭代基本模式  while() { x=f(x); }
    5.递归
    调用自身
    递归求阶乘
    public class Fac
    {
     public static void main(String args[])
     {
      System.out.println("Fac of 5 is " + fac( 5) );
     }
     static long fac( int n ){
      if( n==0 ) return 1;
      else return fac(n-1) * n;
     }
     
    }
    递归基本模式  f(n) { f(n-1); }
    6.回溯
    回溯法是一种选优搜索法,按选优条件向前搜索,以达到目标。但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”。
    基本模式  x++; if(...) x--;
     
    明天学习内容:
    多线程的创建和控制
     
     
     
  • 相关阅读:
    EXCRT
    棋盘组合数
    Luogu P3687 [ZJOI2017]仙人掌 题解
    P4727 [HNOI2009]图的同构记数 题解
    WC2019 T1 数树 题解
    生成函数学习笔记
    luogu P1275 魔板 题解
    多项式ln、牛顿迭代学习笔记
    白帽子讲web安全——白帽子兵法(设计安全方案中的技巧)
    白帽子讲web安全——一个安全解决方案的诞生细节
  • 原文地址:https://www.cnblogs.com/SirNie/p/13373779.html
Copyright © 2020-2023  润新知