需要创建一个FlashCard类型的ArrayList,它将值存储为String?

时间:2016-12-05 19:08:24

标签: java arraylist

对于我的任务,我被要求创建一个存储一系列问题和答案的FlashCards ArrayList。当然,这些将存储在String类型中,但存储它们的ArrayList必须是FlashCard类型,因此我对如何发生这种情况感到困惑。以下是我到目前为止的情况:

 public class FlashCard 
{

         ArrayList<FlashCard> cardlist = new ArrayList<FlashCard>();


FlashCard(String question, String answer)
{

}


public void getQuestion() 
{

cardlist.add("1Q - By what initials was Franklin Roosevelt better known?");
cardlist.add("2Q - Which number president was Franklin Roosevelt?");
cardlist.add("3Q - Which state was Franklin Roosevelt born in?");
cardlist.add("4Q - In which year did Roosevelt become Governor of New York?");
cardlist.add("5Q - What was the name of Franklin Roosevelt's wife?");
cardlist.add("6Q - How many children did Franklin Roosevelt have?");
cardlist.add("7Q - From which university did Franklin Roosevelt graduate with an A.B in history?");
cardlist.add("8Q - What was the first name of Franklin Roosevelt's 5th cousin, who was also President?");
cardlist.add("9Q - Which disease is believed to be the causes of Franklin Roosevelt's paralysis?");
cardlist.add("10Q - At what age did Franklin Roosevelt die?");

}

public void getAnswer()
{

 cardlist.add("1A - FDR");
 cardlist.add("2A - 32");
 cardlist.add("3A - New York");
 cardlist.add("4A - 1929");
 cardlist.add("5A - Elenor");
 cardlist.add("6A - 6");
 cardlist.add("7A - Hrrvard");
 cardlist.add("8A - Theodore");
 cardlist.add("9A - Polio");
 cardlist.add("10A - 63");

}

}

关于如何将这些存储到FlashCard ArrayList中的任何想法?感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

要将Flashcard添加到列表中,您必须放入对象抽认卡,而不是字符串值。

示例:

//Create both frames

JFrame frame1 = new JFrame("Frame1");
JFrame frame2 = new JFrame("Frame2");

//Add your buttons using a proper layout manager as suggested above
...
//Later in your code in your actionPerformed:
if (e.getSource.equals(b1)) {
    //To close the frame w/o exiting application
    frame1.dispose();
    frame2 = new JFrame("Frame2"); //Or call the method that creates this JFrame

    //To toggle frame visibility and not closing it
    frame1.setVisible(false);
    frame2.setVisible(true);
} else if (e.getSource.equals(b2)) {
    //To close the frame w/o exiting application
    frame2.dispose();
    frame1 = new JFrame("Frame2"); //Or call the method that creates this JFrame

    //To toggle frame visibility and not closing it
    frame2.setVisible(false);
    frame1.setVisible(true);
}