问题描述
嘿,抱歉,我是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:
多维ArrayList
MultiDimensional ArrayList
- [0]:问题(可能超过100个.)
- [1]个答案(可能超过100个.)
- [2]类别(可能超过100个.)
- [3]文章(可能超过100篇)
推荐答案
似乎是带有列表列表"的作业.
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.
这篇关于如何使用多维arraylist?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!