泛型(1)_容器
来源:互联网 发布:windows 8.1哪个版本好 编辑:程序博客网 时间:2024/06/07 01:51
1. 堆栈
public class LinkedStack<T> {private static class Node<U> {private U item;private Node<U> next;public Node() {item = null;next = null;}public Node(U item, Node<U> next) {this.item = item;this.next = next;}boolean end() {return item == null && next == null;}}private Node<T> top = new Node<T>();public void push(T item) {top = new Node<T>(item, top);}public T pop() {T result = top.item;if(!top.end()) {top = top.next;}return result;}public static void main(String[] args) {LinkedStack<String> stack = new LinkedStack<String>();for(String s : "a b c d".split(" ")) {stack.push(s);}String result = null;while((result=stack.pop()) != null) {System.out.print(result + " ");}System.out.println();}}
//outputd c b a
2. 随机查询
import java.util.ArrayList;import java.util.List;import java.util.Random;public class RandomList<T> {private List<T> storage = new ArrayList<T>();private Random random = new Random(47);public void add(T item) {storage.add(item);}public T select() {return storage.get(random.nextInt(storage.size()));}public static void main(String[] args) {RandomList<String> rList = new RandomList<String>();for(String s : "a b c d".split(" ")) {rList.add(s);}for(int i=0; i<20; i++) {System.out.print(rList.select() + " ");}}}
//outputc b c a a c a b c c b d b a a c d a d c
0 0
- 泛型(1)_容器
- C++_set容器_知识点整理1
- Java基础_容器
- 学习笔记_容器
- 黑马程序员_容器
- 03_容器
- 117_容器_自定义泛型_深入1_子类_属性类型_重写方法类型_泛型擦除
- STL 容器和迭代器连载1_简介
- STL 容器和迭代器连载1_简介
- STL 容器和迭代器连载4_顺序容器的操作1
- STL 容器和迭代器连载4_顺序容器的操作1
- 100_容器_List_ArrayList_LinkedList_Vector用法_详解
- 111_容器_自定义实现HashSet
- 126_容器_自定义数据排序
- 142_容器_重点总结
- 127_容器_排序容器_TreeSet与TreeMap
- java基础知识_容器类
- java容器之一_概述
- C#中类嵌套定义的访问权限
- BASE64 的加密与解密
- 升级到MAC 10.9.1 iOSOpenDev问题
- PullToRefresh使用详解(四)--利用回调函数实现到底加载
- alter index rebuild 效率分析
- 泛型(1)_容器
- 近义词与同义词来源
- java解析json
- Ajax学习---responseText
- Java多线程-(6)java 原子操作
- Ajax中onreadystatechange的作用
- JSP的九大内置对象
- ssh使用签名登录
- js异步加载