这个想法是关于序列化这个盒子代码。该程序是构建闪存卡,我希望使用序列化来保存fl ashapp框的状态,当应用程序退出到文件中,并在启动时加载文件。所以我不必从头开始重做所有的闪卡。包装箱代码如下:
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);
}
}
我的flashcard类是:
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();
那么如何让savebox类保存用户刚刚使用的结果?
答案 0 :(得分:-1)
根据我的理解,您希望将Flashcard对象保存到文件中,然后再读取。这是一个解释如何:How to write and read java serialized objects into a file
的链接