您的位置:首页 > 编程语言 > Java开发

Java学习笔记——链表的泛型实现和序列化读写

2014-12-18 16:05 471 查看
Java中的泛型即可参数化类型,在定义类的过程中,可对一些同样类型成员函数和成员变量设置为虚定义类型,在具体调用的时候可以按自己的需要设置为自己所需要的类型,如类标LinkedList<T>中,T即为泛型,可以具象为LinkedList<Integer>,LinkedList<String>等等。具体实现代码如下:

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);
}

}


具体实现结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: