zoukankan      html  css  js  c++  java
  • 泛型

    1、基本概念

    class ArrayList<E>

    <>: typeof,元素是什么类型的

    E:泛型的形式参数类型

    <Integer>:泛型的实际参数类型

    ArrayList<E>:带有泛型的类型

    ArrayList<Integer>:参数化的泛型类型(ParameterizedType)

    2、

    ArrayList<String> list = new ArrayList<Object>();  X

    ArrayList<Object> list = new ArrayList<String>();  X

    ArrayList<String> list = new ArrayList (); 正确

    ArrayList list = new ArrayList<String>(); 正确

    使用:两边都带有泛型的,泛型的类型必须完全一致。可以只有一边有。

    3.

     1 //泛型方法的定义
     2 public class GenericDemo1 {
     3     //<T>:声明泛型。必须放在方法的返回值的前面
     4     //泛型只有声明后才能使用
     5     public <T> T m1(){
     6         return null;
     7     }
     8     public <T> void m2(T t){
     9         
    10     }
    11     public static <T> T m3(){
    12         return null;
    13     }
    14 }
    GenericDemo1
     1 //泛型类的定义:GenericDemo2<T>
     2 //类上定义的泛型,实例方法上就可以直接使用了
     3 //静态方法都必须先定义后使用
     4 public class GenericDemo2<T> {
     5     
     6     public T m1(){
     7         return null;
     8     }
     9     public void m2(T t){
    10         
    11     }
    12     public static <T> T m3(){
    13         return null;
    14     }
    15 }
    GenericDemo2
    1 //定义多个泛型类型
    2 public class GenericDemo3 {
    3     
    4     public static <K,V> V m3(K k){//Map
    5         return null;
    6     }
    7 }
    GenericDemo3
     1 public class ArrayUtil {
     2     //实现任意数组指定位置上元素的交换(1 2 3 4            0 3-->4 2 3 1)
     3     public static <T> void change(T[] t,int index1,int index2){
     4         T tmp = t[index1];
     5         t[index1] = t[index2];
     6         t[index2] = tmp;
     7     } 
     8     //翻转数组中的元素(1 2 3 4 ---》4 3 2 1)
     9     public static <T> void reverse(T[] t){
    10         
    11         int startIndex = 0;
    12         int endIndex = t.length-1;
    13         
    14         while(startIndex<endIndex){
    15             T tmp = t[startIndex];
    16             t[startIndex] = t[endIndex];
    17             t[endIndex] = tmp;
    18             startIndex++;
    19             endIndex--;
    20         }
    21     }
    22 }
    ArrayUtil
     1 import static org.junit.Assert.*;
     2 
     3 import java.util.Arrays;
     4 
     5 import org.junit.Test;
     6 
     7 import com.itheima.util.ArrayUtil;
     8 
     9 public class ArrayUtilTest {
    10 
    11     @Test
    12     public void testChange() {
    13         Integer i[] = {1,2,3,4,5,6};
    14         ArrayUtil.change(i, 1, 4);
    15         System.out.println(Arrays.asList(i));
    16     }
    17     @Test
    18     public void testReverse(){
    19         Integer i[] = {1,2,3,4,5};
    20         ArrayUtil.reverse(i);
    21         System.out.println(Arrays.asList(i));
    22     }
    23 }
    ArrayUtilTest
    合群是堕落的开始 优秀的开始是孤行
  • 相关阅读:
    2020年4月13日
    2021年4月12日
    梦断代码阅读笔记02
    Shell基本命令
    远程链接Linux
    Linux文档与目录结构
    VMware与Centos系统安装
    linux 第一天
    day88 Vue基础
    python 生成随机验证码
  • 原文地址:https://www.cnblogs.com/biaogejiushibiao/p/9351450.html
Copyright © 2011-2022 走看看