如何使用多维数组列表? [英] how to work with Multidimensional arraylist?
问题描述
大家好,抱歉,我是 Java 的菜鸟,但我正在尝试做一些事情,我需要一个多维数组列表.它必须实现为以下代码:
Hey guys sorry i'm a noob at java but im trying to make something and i need a multidimensional array list. It must be implemented into the following code:
public static void OpenFile() {
ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
int retrival = chooser.showOpenDialog(null);
if (retrival == JFileChooser.APPROVE_OPTION) {
try (BufferedReader br = new BufferedReader(new FileReader(
chooser.getSelectedFile()))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
if (sCurrentLine.equals("")) {
continue;
} else if (sCurrentLine.startsWith("Question")) {
System.out.println(sCurrentLine.split(":")[1]);
//add to [0] in array ArrayList
} else if (sCurrentLine.startsWith("Answer")) {
System.out.println(sCurrentLine.split(":")[1]);
//add to [1] in array ArrayList
} else if (sCurrentLine.startsWith("Category")) {
System.out.println(sCurrentLine.split(":")[1]);
//add to [2] in array ArrayList
} else if (sCurrentLine.startsWith("Essay")) {
System.out.println(sCurrentLine.split(":")[1]);
//add to [3] in array ArrayList
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我拆分的字符串的所有 [1] 索引需要按以下顺序进入多维数组:问题、答案、类别、文章.
所以我目前使用的是一个普通的多维数组,但你不能轻易改变它的值.我希望我的多维数组列表看起来像这样:
All the [1] index's of the strings i split need to go into a multidimesional array in this order: question, answer, category, essay.
So i am currently using a normal multidimensional array but you cant change the values of it easily. What i want my multidimensional arraylist to look like is this:
多维数组列表
- [0]:问题(可能超过 100 个.)
- [1] 个答案(可能超过 100 个.)
- [2] 类别(可能超过 100 个.)
- [3] 论文(可能超过 100 篇.)
推荐答案
看起来像是一个 List of List 的作业.
Seems like an assignment with List of List.
这是伪代码.尝试实现休息.
Here is the psuedo code. Try to implement rest.
ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
你要做的第一件事是获取 4 个新的 ArrayLists
First thing you have to do is take 4 fresh ArrayLists
ArrayList<String> qtns= new ArrayList<String>>();
ArrayList<String> answrs= new ArrayList<String>>();
--
--
将它们全部添加到主列表
Add all of them to main list
array.add(qtns);
--
--
然后现在像填充它们
else if (sCurrentLine.startsWith("Question")) {
System.out.println(sCurrentLine.split(":")[1]);
//add to [0] in array ArrayList
array.get(0).add(sCurrentLine.split(":")[1]);//get first list(which is qtns list) and add to it.
} else if (sCurrentLine.startsWith("Answer")) {
System.out.println(sCurrentLine.split(":")[1]);
//add to [1] in array ArrayList
array.get(1).add(sCurrentLine.split(":")[1]);//get second list(which is answers list) and add to it.
}
-- -
---
这样最终您将获得一个包含数据的列表.
So that in the end you'll have a list of list which contains the data.
这篇关于如何使用多维数组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!