Java学习笔记——链表的泛型实现和序列化读写
2014-12-18 16:05
471 查看
Java中的泛型即可参数化类型,在定义类的过程中,可对一些同样类型成员函数和成员变量设置为虚定义类型,在具体调用的时候可以按自己的需要设置为自己所需要的类型,如类标LinkedList<T>中,T即为泛型,可以具象为LinkedList<Integer>,LinkedList<String>等等。具体实现代码如下:
LinkedList.java中:
具体实现结果如下:
LinkedList.java中:
//创建可序列化的类别 import java.io.Serializable; public class LinkedList<T extends Serializable>implements Serializable {//实现序列化 public LinkedList() { } public LinkedList(T item) { if (item != null) { current = end = start = new ListItem(item); } } public LinkedList(T[] items) { if (items != null) { for (int i = 0; i < items.length; i++) { addItem(items[i]); } current = start; } } public void addItem(T item){ ListItem newEnd=new ListItem(item); if(start==null){ start=end=newEnd; }else{ end.next=newEnd; end=newEnd; } } public T getFirst(){ current=start; return start==null?null:start.item; } public T getNext(){ if(current!=null){ current=current.next; } return current==null?null:current.item; } private ListItem start=null; private ListItem end=null; private ListItem current=null; private static final long serialVersionUID=1001L;//序列化编号 private class ListItem implements Serializable{ public ListItem(T item){ this.item=item; next=null; } @Override public String toString(){ return "ListItem "+item; } ListItem next; T item; private static final long serialVersionUID=1001L; } }main.java中
import java.util.*; import java.io.*; import java.nio.file.*; import static java.lang.Math.random; public class TryAutoboxing { /** * @param args */ public static void main(String[] args) { // TODO 自动生成方法存根 Path file = Paths.get(System.getProperty("user.home")).resolve(//设置文件路径 "Beginning Java Stuff").resolve("Numbers.bin"); try { Files.createDirectories(file.getParent());//创建父路径 } catch (IOException e) { System.err.println("Error creating directory: " + file.getParent()); e.printStackTrace(); } LinkedList<Integer> numbers = new LinkedList<Integer>();//实体化链表的泛型 for (int i = 0; i < 10; ++i) { numbers.addItem(1 + (int) (100.0 * random())); } System.out.println("\nnumbers list contains:"); listAll(numbers); try { ObjectOutputStream objOut = new ObjectOutputStream( //创建或打开文件,并形成对象流 new BufferedOutputStream(Files.newOutputStream(file))); objOut.writeObject(numbers);//写入数据 objOut.flush();//刷新缓冲池,刷新才能真正将数据从缓冲池写入文件中 } catch (IOException e) { e.printStackTrace(); System.exit(1); } LinkedList<Integer> values = null; try { ObjectInputStream objIn = new ObjectInputStream( new BufferedInputStream(Files.newInputStream(file)));//打开文件,并直接读入对象 values = (LinkedList<Integer>) (objIn.readObject()); } catch (ClassNotFoundException e) { e.printStackTrace(); System.exit(1); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } static void listAll(LinkedList<Integer> list) {//输出所有的链表数据 Integer number = list.getFirst(); int count = 0; do { System.out.printf("%5d", number); if (++count % 5 == 0) { System.out.println(); } } while ((number = list.getNext()) != null); } }
具体实现结果如下:
相关文章推荐
- Java学习笔记---实现文件随机读写-RandomAccessFile
- 链表学习笔记--java语言实现
- java 学习笔记--利用反射实现泛型数组的复制
- Java学习笔记——应用矢量和迭代器来实现类似链表的功能
- 算法(第四版)学习笔记之java实现栈和队列(链表实现)
- 链栈实现算法 - Java 学习笔记(26)
- (源码实例)通过层DIV实现,当鼠标放在链接上面,显示图片及文字 - 流星絮语 JAVA学习笔记 - CSDNBlog
- Java实现泛型链表
- Java 学习笔记:泛型(二)
- Java 死锁的实现 -Java学习笔记(28)
- Java对象序列化学习笔记
- 【学习笔记】Thinking in java (第三版)第五章 隐藏实现(Hiding the implementation)
- java对象序列化学习笔记
- Java对象序列化学习笔记
- java学习笔记---第13章泛型程序设计
- 算法学习,单链表 C# 泛型实现
- JAVA学习笔记-------泛型,约束
- [学习笔记]Java 中对文件的读写操作之比较
- Java 实现 文件目录 遍历 - Java 学习笔记 (22)