Java--泛型
2015-08-22 14:20
351 查看
class TNode<T> //<>写在类名后 { private T value; private ArrayList<TNode<T>> children = new ArrayList<>(); TNode(T v) { this.value = v; } public T getValue() { return this.value; } public void add(T v) { TNode<T> child = new TNode<>(v); this.children.add(child); } public TNode<T> getChild(int i) { if ((i < 0) || (i > this.children.size())) return null; return (TNode<T>)this.children.get(i); } public void traverse() { System.out.println(this.value); for (TNode child : this.children) child.traverse(); } }
泛型一定是引用类型。
相关文章推荐
- Java虚拟机内存分配策略
- 【Java 陷阱】SimpleDateFormat 时间格式化多线程异常
- 用Eclipse建spring mvc项目(图解)
- Myeclipse快捷键介绍
- struts2 jar包说明
- 从eclipse运行UiAutomator的方法,告别cmd
- How to run java and junit in command line
- java Map,Set,List的区别
- Javamail发送邮件的简单实例
- java中的equals和==
- java 对象和引用
- java 访问权限控制
- java中的hashcode方法
- Java 内存划分
- java的异常处理
- JFrame, JPanel, JComponent
- java中的装箱和拆箱
- 使用包内资源
- java 如何有效的避免OOM
- java 字节流和字符流的区别