Java 如何在实现Iterable的类上实现Serializable

Java 如何在实现Iterable的类上实现Serializable,java,Java,我一直在尝试序列化这个iterable java类以用于后续部署。 在下面的类中包含Serializable标记会导致java错误。该代码是一个通用多集(bag)实现,它使用链表数据结构,并实现迭代器实用程序,以便在多集中进行简单的通用项迭代。 有谁能撒些编码精灵的灰尘来拯救局面?帮助JavaBeans制作一个精髓文档 /**my iterable class ** **/ public class Bagged<Item> implements Iterable<Item&

我一直在尝试序列化这个iterable java类以用于后续部署。 在下面的类中包含Serializable标记会导致java错误。该代码是一个通用多集(bag)实现,它使用链表数据结构,并实现迭代器实用程序,以便在多集中进行简单的通用项迭代。 有谁能撒些编码精灵的灰尘来拯救局面?帮助JavaBeans制作一个精髓文档

/**my iterable class **
**/

public class Bagged<Item> implements Iterable<Item> {

private int n;
private Node first;

//create empty bag
public Bagged(){ first= null; n= 0; }

//check if bag is empty
public boolean empty(){return first==null;}
//add item
public void add(Item item){
    Node theold = first;
    first = new Node();
    first.item= item;
    first.nextnode = theold;
    n++;
}
//return the number of items
public int size(){return n;}

//create linked list class as a helper
private class Node{private Item item;  private Node nextnode; }

//returns an iterator that iterates over all items in thine bag
public Iterator<Item> iterator(){
 return new ListIterator();
} 

//iterator class--> remove() function ommited typical of a bag implementation.
private class ListIterator implements Iterator<Item>
    {
     private Node current = first;
     public void remove(){ throw new UnsupportedOperationException();}
     public boolean hasNext(){return current!=null;}
     public Item next() 
             {
        if(!hasNext())throw new NoSuchElementException();
            Item item = current.item;
            current = current.nextnode;
        return item;
             }
}
//main class omitted- end of iterable class.
}
/**我的iterable类**
**/
公共类袋装工具{
私人int n;
私有节点优先;
//创建空包
public Bagged(){first=null;n=0;}
//检查行李是否是空的
public boolean empty(){return first==null;}
//添加项
公共作废添加(项目){
Node theold=第一个;
第一个=新节点();
第一,项目=项目;
first.nextnode=theold;
n++;
}
//返回项目数
public int size(){return n;}
//创建链接列表类作为辅助对象
私有类节点{private Item;private Node nextnode;}
//返回迭代包中所有项目的迭代器
公共迭代器迭代器(){
返回新的ListIterator();
} 
//迭代器类-->remove()函数是一个典型的bag实现。
私有类ListIterator实现了迭代器
{
私有节点电流=第一;
public void remove(){抛出新的UnsupportedOperationException();}
公共布尔值hasNext(){返回当前值!=null;}
公共项目下一步()
{
如果(!hasNext())抛出新的NoSuchElementException();
项目=当前项目;
current=current.nextnode;
退货项目;
}
}
//省略了main类-iterable类的结尾。
}

发布准确的错误,但如果您仅使Bagged可序列化,则错误非常明显:

public static void main(String[] args) throws IOException {
  Bagged<Object> bag = new Bagged<Object>();
  bag.add(new Object());
  new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(bag);
}

Exception in thread "main" java.io.NotSerializableException: Bagged$Node
publicstaticvoidmain(字符串[]args)引发IOException{
袋装袋=新袋装袋();
添加(新对象());
新建ObjectOutputStream(new ByteArrayOutputStream()).writeObject(bag);
}
线程“main”java.io.NotSerializableException中的异常:Bagged$Node
ListIterator类不必是可序列化的,因为Bagged类不包含对它的引用。请注意,如果包中的对象不可序列化,也会出现此异常。要强制执行,您需要按以下方式申报装袋:

public class Bagged <Item extends Serializable> implements Iterable <Item>, Serializable
public类Bagged实现了Iterable、Serializable

@ankit rustagi您肯定可以在Javai中实现多个接口,即使在添加增强功能后,也会出现java.io.NotSerializableException错误。既然列表迭代器无法序列化,我如何对其进行对象引用以使其可序列化?您会得到什么错误-确切地说-?你认为这是什么意思?您不需要将迭代器序列化,因为您通常不希望保存它。好的,爸爸!!我不会让迭代器序列化。我认为它可以工作,但我知道在客户端部署这个类之后,java运行时环境将负责可序列化类中的迭代器类。我从忽略错误中得到安慰。就像在java中不允许的创建泛型数组时欺骗JVM一样。欺骗JVM是一种爱好。唐克斯·沃尔特。请随便插手。