泛型的使用

泛型是一种强大的编程工具,允许在代码中定义类型参数,从而实现更灵活的数据操作。在Java中,泛型用于创建可重复使用的代码,确保在编译时类型安全,避免了类型转换的麻烦。本文通过示例展示了如何声明和使用泛型方法及泛型类,包括泛型方法的声明、泛型类的实例化以及在排序不同类型的数组时的应用。通过泛型,我们可以对整型、浮点数和字符串等类型数组进行排序,提高了代码的复用性和效率。
摘要由CSDN通过智能技术生成

泛型

为什么要使用泛型

我们需要在数组中对各种类型(如整形数组,字符串数组等其他类型数组)的数组进行排序,但是传统的数组只能对单一类型的数组进行排序。所以,我们可以用泛型的方法来对整型,浮点数,字符串等类型数组进行排序.。

泛型分为泛型方法和泛型类

使用方法

  1. 声明泛型参数,在方法返回类型的前面, 在类名的后面,用<>表示,在<>里写一个通配符,里面可以写任何单词,字母。常用E表示元素。

  2. 在泛型里声明要注意类型参数只能使用引用型类型。如(int要使用integer,char要使用string)

    byte->Byte short->Short int->Integer long->Long

    float->Float double->Double char->Character boolean->Boolean

泛型方法

public class Test
{
    // 泛型方法
    Object [] e;// 存储结构 : Object[] 是所有数据类型的父类
    public static < E > void get( Object[] e )
    {
        // 输出数组元素
        for (int i = 0 ;i<5;i++){
            System.out.println(e [i].toString());
        }
        System.out.println();
    }

    public static void main( String args[] )
    {
        // 创建不同类型数组: Integer, Double 和 String
        Integer[] intArray = { 1, 2, 3, 4, 5 };
        Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 ,5.5};
        String[] stringArray = {"he","llo","hello","hell","helo"};

        System.out.println( "整型数组元素为:");
        get(intArray);
		// 整数型数组

        System.out.println( "\n双精度型数组元素为:");
        get(doubleArray);
         // 双精度型数组

        System.out.println( "\n字符型数组元素为:");
        get(stringArray);
        // 字符串数组
    }
}

输出样式:在这里插入图片描述

泛型类

public class List<E> {
    E e;
    public void add(E e) {
        this.e = e;
    }

    public E print() {
        return e;
    }

    public static void main(String[] args) {
        List<Integer> integerList = new List<Integer>();
        List<String> stringList = new List<String>();

        integerList.add(10);
        stringList.add("hello");

        System.out.println("整型值为 :");
        System.out.println(integerList.print());

        System.out.println("字符串为 :");
        System.out.println(stringList.print());
    }
}

输出样式:在这里插入图片描述

泛型(Generics)是一种在编程语言中实现参数化类型的技术,可以让我们编写更加灵活和通用的代码。下面是一个泛型使用案例: 假设我们有一个需求,需要实现一个通用的栈(Stack)数据结构,可以存储任何类型的元素。我们可以使用泛型来实现这个通用的栈数据结构。以下是一个基于Java的示例代码: ```java public class Stack<T> { private ArrayList<T> items; public Stack() { items = new ArrayList<T>(); } public void push(T item) { items.add(item); } public T pop() { if (items.isEmpty()) { throw new RuntimeException("Stack is empty"); } return items.remove(items.size() - 1); } public boolean isEmpty() { return items.isEmpty(); } } ``` 在上面的代码中,我们使用了一个类型参数 `T`,它代表任何类型。我们在类的定义中使用了 `<T>` 来声明这个类是一个泛型类,它可以接受任何类型的元素。在类的内部,我们使用 `T` 来代表元素的类型。我们将元素存储在一个 `ArrayList<T>` 中,这个 `ArrayList` 可以存储任何类型的元素。 我们定义了三个方法:`push()`、`pop()` 和 `isEmpty()`。`push()` 方法用于将元素压入栈中,`pop()` 方法用于弹出栈顶元素,并从栈中移除它,`isEmpty()` 方法用于判断栈是否为空。 使用泛型,我们可以使用这个通用的栈数据结构来存储任何类型的元素,例如: ```java Stack<Integer> intStack = new Stack<Integer>(); intStack.push(1); intStack.push(2); intStack.push(3); intStack.pop(); // 返回 3 intStack.pop(); // 返回 2 Stack<String> strStack = new Stack<String>(); strStack.push("Hello"); strStack.push("World"); strStack.pop(); // 返回 "World" ``` 在上面的示例代码中,我们分别使用了 `Stack<Integer>` 和 `Stack<String>` 来存储整数和字符串类型的元素。由于使用泛型,这个通用的栈数据结构可以存储任何类型的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nothing8727

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值