gpt4 book ai didi

java - 我怎样才能使这个序列化工作?

转载 作者:行者123 更新时间:2023-12-01 06:08:20 27 4
gpt4 key购买 nike

这个想法是关于序列化这个框代码。该程序用于构建闪存卡,当应用程序退出到文件中时,我想使用序列化来保存闪存应用程序框的状态,并在启动时加载该文件。所以我不必从头开始重做所有的抽认卡。盒子代码如下:

import java.util.ArrayList;
import java.util.Iterator;



public class Box implements java.io.Serializable {
private ArrayList<Flashcard> cards;
private double prossibility;
private double pivot; //determine the box's selected scope [pivot - prossibility,pivot)
public Box(){
cards = new ArrayList<Flashcard>();
prossibility = 0.0;
pivot = 0.0;
}
public Box(double prossibility,double pivot){
this.cards = new ArrayList<Flashcard>();
this.prossibility = prossibility;
this.pivot = pivot;
}
public Box(ArrayList<Flashcard> cards){
this.cards = cards;
}


public ArrayList<Flashcard> getCards() {
return cards;
}

public void setProssibility(double prossibility){
this.prossibility = prossibility;
}
public double getProssibility(){
return prossibility;
}

public void setPivot(double pivot){
this.pivot = pivot;
}
public double getPivot(){
return pivot;
}

public void addCard(Flashcard card){
cards.add(card);
}
public int searchCard(String challenge){
Iterator<Flashcard> it = cards.iterator();
int index = 0;
while(it.hasNext()){
Flashcard temp = (Flashcard)it.next();
if(temp.getChallenge().equals(challenge)){
break;
}
++index;
}
if(index >= cards.size())
index = -1;
return index;
}
public void removeCard(String challenge){
int index = searchCard(challenge);
if(index >= 0)
cards.remove(index);
}

}

我的抽认卡类(class)如下:

enum ESide{
FRONT, //challenge side
BACK //response side
};
public class Flashcard implements Cloneable{
private String challenge;
private String response;
private ESide side;
private int boxIndex;
public Flashcard(){
challenge = new String();
response = new String();
side = ESide.BACK;
boxIndex = 0;
}
public Flashcard(String challenge, String response, ESide side){
this.challenge = challenge;
this.response = response;
this.side = side;
this.boxIndex = 0;
}
public Flashcard(String challenge, String response){
this.challenge = challenge;
this.response = response;
this.side = ESide.BACK;
this.boxIndex = 0;
}


public void setChallenge(String challenge){
this.challenge = challenge;
}
public String getChallenge(){
return challenge;
}

public void setResponse(String response){
this.response = response;
}
public String getResponse(){
return response;
}

public void setSide(ESide side){
this.side = side;
}
public ESide getSide(){
return side;
}

public void setBoxIndex(int index){
this.boxIndex = index;
}
public int getBoxIndex(){
return boxIndex;
}

public void flipSide(){
if(side == ESide.BACK)
side = ESide.FRONT;
else
side = ESide.BACK;
}

public Object clone(){
Flashcard o = null;
try {
o = (Flashcard)super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return o;
}
public boolean equals(Object o){
if(this == o)
return true;
if(o == null)
return false;
if(!(o instanceof Flashcard))
return false;
Flashcard temp = (Flashcard)o;
if(!temp.challenge.equals(this.challenge) || !temp.response.equals(this.response)){
return false;
}

return true;
}

}

我所做的序列化代码是这样的:

import java.io.*;

public class Savingbox implements Serializable {
public static void main(String[] args){
Box e = new Box();

那么如何让 savingbox 类保存用户刚刚使用的结果呢?

最佳答案

据我了解,您希望将抽认卡对象保存到文件中,然后将其读回。以下链接解释了如何操作: How to write and read java serialized objects into a file

关于java - 我怎样才能使这个序列化工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40291666/

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