gpt4 book ai didi

java - 序列化 Arraylist

转载 作者:行者123 更新时间:2023-12-02 03:21:09 24 4
gpt4 key购买 nike

我创建了一个名为“Item”的对象,并且我想序列化其中包含 Items 的 ArrayList。我的程序与 ArrayList<String> 完美配合,但它不适用于 ArrayList<Item> 。我相信这与我的对象有关。这是:

public class Item implements Serializable{

private static String name;
private static BufferedImage picture;
private static boolean craftable;
private static Item[][] craftTable;
private static boolean smeltable;
private static Item smelt_ancestor;
private static Item smelt_descendant;

public Item(String name, boolean craftable, boolean smeltable){
this.name = name;
this.craftable = craftable;
if(craftable){
craftTable = new Item[3][3];
}else{
craftTable = null;
}
this.picture = null;
this.smeltable = smeltable;
this.smelt_ancestor = null;
this.smelt_descendant = null;
}

public String getName(){
return name;
}

public void setName(String name){
this.name=name;
}

public BufferedImage getPicture(){
return picture;
}

public boolean setPicture(){
boolean verify = false;
String pictureName = name.replaceAll("\\s+","");
String newNamePng = pictureName + ".png";
String newNameJpg = pictureName + ".jpg";
File imagePng = new File(newNamePng);
File imageJpg = new File(newNameJpg);
if(imagePng.exists()){
return true;
}else if(imageJpg.exists()){
return true;
}else{
return false;
}
}

public boolean getCraftable(){
return craftable;
}

public void setCraftable(boolean value){

this.craftable = value;
}

public boolean setCraftTable(Item[][] table){
if(this.craftable==true){
craftTable = table;
return true;
}else{
return false;
}

}

public Item[][] getCraftTable(){
return craftTable;
}

public boolean getSmeltable(){
return smeltable;
}

public void setSmeltable(boolean value){
smeltable = value;
}

public Item getAncestor(){
return smelt_ancestor;
}

public void setAncestor(Item ancestor){
smelt_ancestor = ancestor;
}

public Item getDescendant(){
return smelt_descendant;
}

public void setDescendant(Item des){
smelt_descendant = des;
}

public String toString(){
return name;
}

忽略导入,我在其他我省略的方法中使用它们,因为它们工作得很好。对象是否存在任何问题,可能会阻止其正确序列化?

最佳答案

静态变量未序列化。看起来您可能希望它们成为非静态实例变量。

关于java - 序列化 Arraylist<CustomObject>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39582629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com