我需要帮助将数据从4 ArrayLists
传输到多维数组。请查看我的变量中的注释。
我需要以这种格式将所有arraylists的数据放入card[][]
:
card[][] = {{questions,answers,category,essay}};
代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;
public class Test {
static JFileChooser chooser = new JFileChooser();
//these need to go into card[][]
static List<String> questions = new ArrayList<String>();
static List<String> answers = new ArrayList<String>();
static List<String> categories = new ArrayList<String>();
static List<String> essays = new ArrayList<String>();
static String[][] card;
public static void main(String[] args) {
OpenFile();
}
public static void OpenFile() {
int retrival = chooser.showOpenDialog(null);
if (retrival == JFileChooser.APPROVE_OPTION) {
String line;
try (BufferedReader br = new BufferedReader(new FileReader(
chooser.getSelectedFile()))) {
while ((line = br.readLine()) != null) {
String[] splitted = line.split("Question: ");
String[] splittedAnswer = line.split("Answer: ");
String[] splittedCategory = line.split("Category: ");
String[] splittedEssay = line
.split("Essay Question Possibility: ");
if (splittedAnswer.length == 2) {
answers.add(splittedAnswer[1]);
} else if (splitted.length == 2) {
questions.add(splitted[1]);
} else if (splittedCategory.length == 2) {
categories.add(splittedCategory[1]);
} else if (splittedEssay.length == 2) {
essays.add(splittedEssay[1]);
}
}
br.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
答案 0 :(得分:1)
假设所有数组列表大小相同,您可以按照以下方式执行,
String[][] card = new String[4][questions.size()];
for (int i = 0; i < questions.size(); i++) {
card[0][i] = questions.get(i);
card[1][i] = answers.get(i);
card[2][i] = categories.get(i);
card[3][i] = essays.get(i);
}
的建议强> 的
但我建议您使用面向对象的数据结构来保存数据。不建议使用多个列表或2D数组。
您可以简单地将结构定义为
public class Card {
String question;
String answer;
String categories;
String essay;
}
你有这个清单。
List<Card> cards = new ArrayList<Cards>();
答案 1 :(得分:0)
试试这个,
String card[][] ;
List<String> questions = new ArrayList<String>();
List<String> answers = new ArrayList<String>();
List<String> categories = new ArrayList<String>();
List<String> essays = new ArrayList<String>();
questions.add("11");
questions.add("12");
questions.add("13");
answers.add("21");
answers.add("22");
answers.add("23");
categories.add("31");
categories.add("32");
categories.add("33");
essays.add("41");
essays.add("42");
essays.add("43");
card=new String [essays.size()][4];
for(int i=0;i<questions.size();i++)
{
card[i][0]=questions.get(i);
card[i][1]=answers.get(i);
card[i][2]=categories.get(i);
card[i][3]=essays.get(i);
}
输出
{{“11”,“21”,“31”,“41”},{“12”,“22”,“32”,“42”},{“13”,“23 “,”33“,”43“},...};
答案 2 :(得分:0)
我认为所有的arraylists都有一个尺寸。如果'不'你首先应该找到最小尺寸的arraylists然后使用它而不是questions.size():
card = new String[questions.size()][4];
for (int i = 0; i < card.length; i++){
card[i][0] = questions.get(i);
card[i][1] = answers.get(i);
card[i][2] = categories.get(i);
card[i][3] = essays.get(i);
}
结果将如下:
{{“q1”,“a1”,“c1”,“e1”},{“q2”,“a2”,“c2”,“e2”},{“q3”,“a3”,“ c3“,”e3“},{”q4“,”a4“,”c4“,”e4“},...};