【发布时间】:2014-09-17 04:01:48
【问题描述】:
我有这个班级应该打开一个文件,其中包含 nr 个问题和 nr 个答案。 问题是当它在 ArrayList 中写入时。我尝试过所有类型的 ArrayLists、LinkedLists,甚至是向量。 文件的第一部分,如问题和答案,它没有任何问题。但是当我想将这些答案存储在一个列表中以便我可以将该列表保存在一个对象中时,它就不起作用了。
如果有人可以提供帮助或知道更好的方法,那么将未知的 nr 字符串保存到对象列表中,那将是史诗般的。
文件格式为:
A question,3,yes,no,maybe
Another question,4,yes,maybe,no,why not
我的班级:
public class GetSurvey {
public static String intrebare;
static int raspunsuri;
public static int i = 1;
static String holder;
//static String[] rasp = new String[250];
static List<String> rasp = new LinkedList<String>();
public static SurveyClass[] obj = new SurveyClass[250];
public static void loadSurvey()
{
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+File.separator+"Survey.dat");
if(!file.exists()){
try {
file.createNewFile();
}
catch (IOException e) {
e.printStackTrace();
}
}
if(file.exists()){
try {
Scanner read = new Scanner(file);
read.useDelimiter(",");
while (read.hasNext())
{
intrebare = read.next();
String raspunsuri1 = read.next();
//Log.w("Date",String.valueOf(raspunsuri));
//obj[i].setNrRasp(raspunsuri);
for(int j = 0;j < 3;j++)
{
Log.w("Date",String.valueOf(j));
rasp.add(j,read.next());
}
String[] stringArr = rasp.toArray(new String[rasp.size()]);
Log.w("Date",stringArr[i]);
//obj[i].setRaspunsuri(rasp);
rasp.clear();
i++;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
标签: java android list object arraylist