我不确定如何实现堆栈,您能否告诉Queue该类是否以正确的方式实现了队列。因为它将运行良好,所以它将执行LIFO和FIFO,但我只想确定。而且您还可以告诉我使用arraylist与堆栈和队列来实现它之间的区别。谢谢
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Deck {
private File file;
private String fileName = "Untitled";
private boolean Modified;
private boolean TestRunning;
private int numCorrect;
private int numWrong;
static Queue<QuizCard> quizCardList = new LinkedList<>();
static Queue<QuizCard> quizCardList2 = new LinkedList<>();
static int curr_size;
Deck()
{
curr_size = 0;
}
static void push(int x)
{
curr_size++;
quizCardList.add((QuizCard) quizCardList);
while (!quizCardList.isEmpty())
{
quizCardList.add(quizCardList.peek());
quizCardList2.remove();
}
// swap the names of two queues
Queue<QuizCard> q = quizCardList;
quizCardList = quizCardList2;
quizCardList2 = q;
}
static void pop(){
if (quizCardList.isEmpty())
return ;
quizCardList.remove();
curr_size--;
}
static QuizCard top()
{
if (quizCardList.isEmpty())
// return -1;
return quizCardList.peek();
return null;
}
static int size()
{
return curr_size;
}
/** addQuizCard - creates and adds a QuizCard to quizCardList */
void addQuizCard(String q, String a){
if(q.length() == 0){
q = " ";
}
if(a.length() == 0){
a = " ";
}
quizCardList.add(new QuizCard(q, a));
}
/** readFile - loads in the data from a saved deck into quizCardList */
void readFile(String fileLocation){
file = new File(fileLocation);
setFileName(file.getName());
assert file.canRead();
try(BufferedReader input = new BufferedReader(new FileReader(file))){
int letterNumber;
StringBuilder dataToParse = new StringBuilder();
while((letterNumber = input.read()) != -1){
dataToParse.append((char) letterNumber);
}
}catch(IOException ioEx){
ioEx.printStackTrace();
}
}
void save(String fileLocation){
file = new File(fileLocation);
assert file.canWrite();
try (BufferedWriter output = new BufferedWriter(new FileWriter(file))) {
for(QuizCard quizCard : quizCardList){
}
}catch(IOException ioEx){
ioEx.printStackTrace();
}
}
void shuffle(){
Collections.shuffle((List<?>) quizCardList);
}
String getFileLocation(){
return file.getAbsolutePath();
}
String getFileName(){
return fileName;
}
boolean getIsModified(){
return Modified;
}
boolean getIsTestRunning(){
return TestRunning;
}
int getNumCorrect(){
return numCorrect;
}
int getNumWrong(){
return numWrong;
}
List<QuizCard> getQuizCardList(){
return (List<QuizCard>) quizCardList;
}
void setFileName(String fileName) {
if(fileName.contains(".")){
fileName = fileName.split("\\.")[0];
}
this.fileName = fileName;
}
void setIsModified(boolean newValue){
Modified = newValue;
}
void setIsTestRunning(boolean newValue){
TestRunning = newValue;
}
void setNumCorrect(int newValue){
numCorrect = newValue;
}
void setNumWrong(int newValue){
numWrong = newValue;
}
}