java 参数化类型

 1 package com.gxf.collection;
 2 
 3 import java.util.LinkedList;
 4 
 5 public class TestForT<T> {
 6     private LinkedList<T> storage = new LinkedList<T>();
 7     
 8     /**
 9      * 压栈操作
10      * @param element
11      */
12     public void push(T element){
13         storage.addFirst(element);
14     }
15     /**
16      * 出栈
17      * @return
18      */
19     public T pop(){
20         return storage.removeFirst();
21     }
22     
23     /**
24      * 判断栈是否为空
25      * @return
26      */
27     public boolean isEmpty(){
28         return storage.isEmpty();
29     }
30 
31     public static void main(String[] args) {
32         TestForT<Integer> stack = new TestForT<Integer>();
33         stack.push(1);
34         stack.push(2);
35         stack.push(3);
36         stack.push(4);
37         stack.push(5);
38         
39         while(!stack.isEmpty()){
40             System.out.println(stack.pop());
41         }
42     }
43 
44 }

output:

5
4
3
2
1

转载于:https://www.cnblogs.com/luckygxf/p/4225170.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值