Java 在ArrayList中创建ArrayList
因此,对于我正在开发的一个程序,我试图创建一个通过文本文件进行解析的方法。使用文本文件中的值,在ArrayList中创建ArrayList。以下是我目前的代码: 公共ArrayList getRelsString relsFile { 字符串[]currentId; int i=-1; ArrayList relsList=新的ArrayList; relsList.addnewarraylist; 尝试 { //打开第一个文件 //命令行参数 FileInputStream fstream=新的FileInputStreamrelsFile; BufferedReader br=新BufferedReadernew InputStreamReaderStream; 弦斯特林; 而strLine=br.readLine!=null { //向数组列表中添加值 currentId=strLine.split,; relsList.get++i.addcurrentId[0]; //??????????? } //关闭输入流 近距离; } 捕获异常e { System.err.printlnError:+e.getMessage; } 返回重新列表; } 这是我正在查看的文本文件的一个示例Java 在ArrayList中创建ArrayList,java,arraylist,nested,text-files,nested-class,Java,Arraylist,Nested,Text Files,Nested Class,因此,对于我正在开发的一个程序,我试图创建一个通过文本文件进行解析的方法。使用文本文件中的值,在ArrayList中创建ArrayList。以下是我目前的代码: 公共ArrayList getRelsString relsFile { 字符串[]currentId; int i=-1; ArrayList relsList=新的ArrayList; relsList.addnewarraylist; 尝试 { //打开第一个文件 //命令行参数 FileInputStream fstream=新
rId13,image3
rId18,image8
rId26,image16
rId39,image29
rId21,image11
rId34,image24
rId7,image1
rId12,image2
rId17,image7
rId25,image15
rId33,image23
rId38,image28
rId16,image6
rId20,image10
rId29,image19
rId24,image14
rId32,image22
rId37,image27
rId15,image5
rId23,image13
rId28,image18
rId36,image26
rId19,image9
rId31,image21
rId14,image4
rId22,image12
rId27,image17
rId30,image20
rId35,image25
本质上,我希望能够拆分这些值,将所有rId值存储在第一个ArrayList中,然后在嵌套的ArrayList中列出它们对应的图像值。然而,我有点卡住了,不知道现在该怎么办。有人能帮我吗?非常感谢您提供的任何帮助。我建议您使用调试器来准确查看正在发生的事情 通过阅读代码,我猜它可以读取第一行,但在这一点之后失败,因为您只添加了一个嵌套的ArrayList,但尝试使用其中的许多 最简单的方法是返回Map,因为我假设第一列是唯一的
public static Map<String, String> getRels(String relsFile) {
Map<String, String> map = new LinkedHashMap<String, String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(relsFile));
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",", 2);
if (parts.length > 1)
map.put(parts[0], parts[1]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
} catch (IOException ignored) {
}
}
return map;
}
我建议您使用调试器来查看到底发生了什么 通过阅读代码,我猜它可以读取第一行,但在这一点之后失败,因为您只添加了一个嵌套的ArrayList,但尝试使用其中的许多 最简单的方法是返回Map,因为我假设第一列是唯一的
public static Map<String, String> getRels(String relsFile) {
Map<String, String> map = new LinkedHashMap<String, String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(relsFile));
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",", 2);
if (parts.length > 1)
map.put(parts[0], parts[1]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
} catch (IOException ignored) {
}
}
return map;
}
我认为执行此任务的更好方法是使用任何现有的库来读取CSV文件。这将使您的代码更加简单。我认为执行此任务的更好方法是使用任何现有库来读取CSV文件。这将使代码更加简单。创建两个ArrayList。填充它们。。然后将它们添加到另一个arraylist中 也就是说,那一定是arraylist吗?难道你不能只拥有一个包含两个不同ArrayList的常规对象吗?更好的是,也许你想考虑一个不同的数据结构本身就像一张地图?
try
{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(relsFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
while ((strLine = br.readLine()) != null)
{
// Add values to array list
currentId = strLine.split(",");
list1.add(currentId[0]);
list2.add(currentId[1]);
}
//Close the input stream
in.close();
relsList.add(list1);
relsList.add(list2);
}
创建两个ArrayList。填充它们。。然后将它们添加到另一个arraylist中 也就是说,那一定是arraylist吗?难道你不能只拥有一个包含两个不同ArrayList的常规对象吗?更好的是,也许你想考虑一个不同的数据结构本身就像一张地图?
try
{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(relsFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
while ((strLine = br.readLine()) != null)
{
// Add values to array list
currentId = strLine.split(",");
list1.add(currentId[0]);
list2.add(currentId[1]);
}
//Close the input stream
in.close();
relsList.add(list1);
relsList.add(list2);
}
我同意Peter L的观点-一定要使用Map实现。我使用树映射进行测试,因为它是一个排序映射,但是您可以使用任何您想要的映射。我还测试了它,以确保它工作正常 根据你的描述,我相信这就是你想要的:
public TreeMap<String, ArrayList<String>> getRels(String relsFile)
{
String[] currentId;
int i = -1;
TreeMap<String, ArrayList<String>> relsList = new TreeMap<String, ArrayList<String>>();
try
{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(relsFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null)
{
// Add values to array list
currentId = strLine.split(",");
if(relsList.containsKey(currentId[0])) {
relsList.get(currentId[0]).add(currentId[1]);
} else {
relsList.put(currentId[0], new ArrayList<String>());
relsList.get(currentId[0]).add(currentId[1]);
}
}
//Close the input stream
in.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
return relsList;
}
public static void main(String[] args) {
ListReader lr = new ListReader();
TreeMap<String, ArrayList<String>> l = lr.getRels("test.txt");
for(String s : l.keySet()) {
for(String k : l.get(s)) {
System.out.println("Key: " + s + " Value: " + k);
}
}
}
我在解析部分所做的是用一个简单的IF…ELSE语句解析文件。它检查rId部分中的键是否已经存在,如果已经存在,则只需将右侧的值添加到现有的ArrayList中。如果没有,它将添加一个新键,然后创建一个ArrayList,然后将值添加到该ArrayList中
希望这就是你想要的 我同意Peter L的观点-一定要使用Map实现。我使用树映射进行测试,因为它是一个排序映射,但是您可以使用任何您想要的映射。我还测试了它,以确保它工作正常 根据你的描述,我相信这就是你想要的:
public TreeMap<String, ArrayList<String>> getRels(String relsFile)
{
String[] currentId;
int i = -1;
TreeMap<String, ArrayList<String>> relsList = new TreeMap<String, ArrayList<String>>();
try
{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(relsFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null)
{
// Add values to array list
currentId = strLine.split(",");
if(relsList.containsKey(currentId[0])) {
relsList.get(currentId[0]).add(currentId[1]);
} else {
relsList.put(currentId[0], new ArrayList<String>());
relsList.get(currentId[0]).add(currentId[1]);
}
}
//Close the input stream
in.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
return relsList;
}
public static void main(String[] args) {
ListReader lr = new ListReader();
TreeMap<String, ArrayList<String>> l = lr.getRels("test.txt");
for(String s : l.keySet()) {
for(String k : l.get(s)) {
System.out.println("Key: " + s + " Value: " + k);
}
}
}
我在解析部分所做的是用一个简单的IF…ELSE语句解析文件。它检查rId部分中的键是否已经存在,如果已经存在,则只需将右侧的值添加到现有的ArrayList中。如果没有,它将添加一个新键,然后创建一个ArrayList,然后将值添加到该ArrayList中
希望这就是你想要的 地图将帮助您满足当前的需求。按照下面的回答:+1@Marcelo-或者,如果rIdxx键只有一个图像值,那么从示例数据中选择一个简单的映射就足够了,因为我在第一列中没有看到任何重复项。映射将为您当前的需要提供更多帮助。按照下面的回答:+1@Marcelo-或者,如果rIdxx键只有一个图像值,那么一个简单的映射就足够了,因为我在第一列中没有看到任何重复项。啊,是的,谢谢。我甚至没有考虑使用hashmap,因为我在尝试处理我同事的代码,而他使用的是ArrayList。肯定是一个更好的实现。感谢所有做出贡献的人
d、 :啊,是的,谢谢你。我甚至没有考虑使用hashmap,因为我在尝试处理我同事的代码,而他使用的是ArrayList。肯定是一个更好的实现。感谢所有做出贡献的人