【问题标题】:Format an arraylist格式化数组列表
【发布时间】:2013-09-20 03:05:15
【问题描述】:

我有一个文件要存储到 ArrayList 中,但我不知道如何格式化它,以便将某些文本字符串存储在特定索引中。第一行是类别,第二行是问题,第三行是琐事问题的答案。我需要这样做,以便我可以随机选择问题,然后检查问答游戏的答案。到目前为止,我得到的只是每个单词都用逗号分隔。教授的话,

"输入文件包含不同类别的问题和答案。对于每个类别,第一行表示类别的名称。这一行后面将有若干对行。对的第一行是问题,第二行是其对应的答案。 类别之间用空行分隔。”

到目前为止,这是我的代码:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;



public class TriviaGamePlayer {

/**
 * @param args
 */
public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    ArrayList<String> triviaQuestion = new ArrayList<String>();

    Scanner infile = new Scanner(new File("trivia.txt"));



    while(infile.hasNext()){

        triviaQuestion.add(infile.next());
}


    System.out.println(triviaQuestion);
}


}

【问题讨论】:

  • 您需要只使用一个ArrayList,还是可以使用多个数据结构?实际上,ArrayList 的 add 方法使您将项目添加到数组中的最后一个元素。
  • 那么会有几类琐事问题,就像我提到的那样,我需要从每个类别中一次随机挑选一个问题来运行游戏。所以我想我可以为每个琐事类别使用多个数组列表。
  • 看来我需要使用 java API 中 String 类的 split 方法,但我不确定如何实现它以满足我的需求。

标签: java random arraylist string-comparison


【解决方案1】:

从到目前为止我在问题中看到的情况来看,您最好创建自己的 TriviaQuestion 对象,它看起来像

public class TriviaQuestion
{ 
     public String question;
     public String answer;
     public boolean asked;
     public String category;

     TriviaQuestion (String q, String a, String c)
     {
         question = q;
         answer = a;
         category = c;
     }
 }

然后你有几个选择,但如果你有这个对象,那么一切都会变得容易一些。我会创建一个Map&lt;String,List&lt;TriviaQuestion&gt;&gt;,其中关键是您的类别。

那么在读取文件的时候,你也应该使用infile.hasNextLine()inFile.nextLine()

  1. 读一行(首先我假设是类别)
  2. 阅读接下来的两行(问答)
    • 创建新实例`new TriviaQuestion(question, answer, category)'
    • 将此添加到数组列表中
    • 重复直到空白
  3. 如果下一行为空,则将列表添加到映射并循环回 (1)

喜欢:(这是假设文件格式良好)

String line = inFile.nextLine();  //first line
String category = line;
while(infile.hasNextLine())
{
    line = inFile.nextLine();
    if(line.isEmpty()) //blank line
         category = inFile.nextLine();
    else
    {
         String q = line;
         String a = inFile.nextLine();
         //do other stuff
    }
}  

然后要问一个问题,获取该类别的列表,选择一个随机问题,然后将其设置为已询问,这样就不会再出现了

ArrayList<TriviaQuestion> questions = yourMap.get("Science");
Integer aRandomNumber = 23 //(create a random Number using list size)
TriviaQuestion questionToAsk = questions.get(aRandomNumber)
System.out.println(questionToAsk.question)
questionToAsk.asked = true

【讨论】:

  • 目前还不太清楚如何在 Java 中使用地图。但我看到你来自不同的属性。但是,我仍然不确定是否从该文件中读取并获取我需要的部分以如何使用它们。如果这是有道理的。
  • @Steven Eck 地图非常有用,有机会就去了解它们。另请参阅我的编辑阅读
  • 这里是地图的 Java 文档:docs.oracle.com/javase/7/docs/api/java/util/Map.html 我还添加了一个使用地图的示例。
【解决方案2】:

我会通过确定需要什么来解决这个问题。您有一个类别列表(字符串)。在每个类别中,都会有一个问题(字符串)和答案(字符串)对的列表。从那里我们已经看到了一些组织数据的“合乎逻辑”的方式。

  • 问题 - 字符串
  • 答案 - 字符串
  • 问题/答案对 - 编写一个类(现在,我们将其称为 QAPair),其中包含两个字符串作为字段(一个用于问题,一个用于答案)
  • 一个类别中的 Q/A 对列表 - ArrayList
  • 类别列表,映射到 Q/A 对列表 - 也许 Map 可以解决问题。类型为:地图>

从那里开始解析文件;对于第一行,或者在遇到空行之后,您知道 String 将给出一个类别名称。您可以调用 containsKey() 来检查类别名称是否已经存在;如果它确实为该类别获取 Q/A Pairs 的 ArrayList 并继续添加到列表中,否则初始化一个新的 ArrayList 并将其添加到该类别的映射中。

然后你可以读到两行。对于您读取的每一对行,初始化一个 QAPair 对象,然后将其添加到它们所属类别的 ArrayList 中。

这是一个使用地图的例子:

Map<String, ArrayList<QAPair>> categories = new HashMap<String, ArrayList<QAPair>>();
if (!categories.containsKey("Math")) { // Check to see if a Math category exists
    categories.put("Math", new ArrayList<QAPair>()); // If it doesn't, create it
}
QAPair question1 = new QAPair("2+2", "4");
// get() method returns the ArrayList for the "Math" category
// add() method adds the QAPair to the ArrayList for the "Math" category
categories.get("Math").add(question1);

从地图中获取类别列表并选择一个:

// Convert to an array of Strings
String[] catArray = categories.toArray(new String[0]);
// Get the 10th category in the array
// Use catArray.length to find how many categories there are total
catArray[10];

【讨论】:

  • 除非你不能初始化Map,因为它是一个接口
  • 公平点。假设它是一个HashMap。 TreeMap 或 LinkedHashMap 也可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-14
  • 2023-03-12
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多