zoukankan      html  css  js  c++  java
  • Generic Interfaces

    interface MinMax<T extends Comparable<T>> {
      T min();
    
      T max();
    }
    
    class MyClass<T extends Comparable<T>> implements MinMax<T> {
      T[] vals;
    
      MyClass(T[] o) {
        vals = o;
      }
    
      public T min() {
        T v = vals[0];
    
        for (int i = 1; i < vals.length; i++)
          if (vals[i].compareTo(v) < 0)
            v = vals[i];
    
        return v;
      }
    
      public T max() {
        T v = vals[0];
    
        for (int i = 1; i < vals.length; i++)
          if (vals[i].compareTo(v) > 0)
            v = vals[i];
    
        return v;
      }
    }
    
    public class MainClass {
      public static void main(String args[]) {
        Integer inums[] = { 3, 6, 2, 8, 6 };
        Character chs[] = { 'A', 'r', 'V', 'w' };
    
        MyClass<Integer> iob = new MyClass<Integer>(inums);
        MyClass<Character> cob = new MyClass<Character>(chs);
    
        System.out.println("Max value in inums: " + iob.max());
        System.out.println("Min value in inums: " + iob.min());
    
        System.out.println("Max value in chs: " + cob.max());
        System.out.println("Min value in chs: " + cob.min());
      }
    }
    Max value in inums: 8
    Min value in inums: 2
    Max value in chs: w
    Min value in chs: A
    

      

  • 相关阅读:
    php 函数汇总
    php 图片base64编码生成dataurl和保存为图片
    bootstrap
    PHPWord
    js json排序
    UE用法
    判断移动端是苹果还是安卓,以及安卓版本
    shell终极操作
    LINUX yum用法
    jquery对checkbox的操作汇总
  • 原文地址:https://www.cnblogs.com/hephec/p/4605882.html
Copyright © 2011-2022 走看看