从文件的ArrayList(具体格式) [英] ArrayList from file (specific format)
问题描述
我有一个看起来像这样的文件:
1日2次n次
E1 V1 1
E1 V3 2
E1 V4 4
E1 V5 7
E2 V1 1
。 。 。
。 。 。
。 。 。
我想的第一列是数组列表(E1,E2或或E3),我想第二列有它的价值(一个int),第n个指标里面的名字。
我会如何做呢?
您想对我是什么意思进一步澄清标识,请不要害怕离开时。
P.S。这是一个更大的项目的一部分,但我有种停留在数据的格式。
我假设我将不得不寻找与名字(第一列)一个ArrayList,如果我找到它,然后在第n个值(第3列)增加值(第2列),如果我并不觉得,我做一个ArrayList中,然后执行上述
更新
所以我设法让程序几乎工作 - 我不能使用的ArrayList的原始结构,因为所有的变量都在编译期间被宣布 - 后不能宣布。
我决定用一个HashMap,键具有第一个值,而第二个值的第n个位置一个ArrayList
`的HashMap<弦乐,ArrayList的<整数GT;> userRatings =新的HashMap<弦乐,ArrayList的<整数GT;>();
如果(!userRatings.containsKey(用户))//用户没有评级。
{
ArrayList的<整数GT;收视率=新的ArrayList<整数GT;();
//收视率= userRatings.get(用户);
对于(INT J = 0; J< 50; J ++)
{
ratings.add(J);
}
userRatings.get(用户)。新增(位置值);
}}
否则//用户有评级
{
userRatings.get(用户)。新增(位置值); **
}
的System.out.println(用户++ userRatings.get(用户));
}
bufferReader.close();
}赶上(FileNotFoundException异常E)
{
的System.out.println(文件不存在或无法找到。);
}
赶上(IOException异常E)
{
的System.out.println(无法从文件读取);
}
赶上(NullPointerException异常E)
{
}`
这个问题我现在是加入到ArrayList。我会怎么做,在粗体领域? - 我将如何填充和操纵的ArrayList
?
说实话,我觉得最初的问题是阅读文件
该格式。
块引用>如果你使用正确的库这很容易。看看这个: http://opencsv.sourceforge.net/ 的,其目的是要分析CSV文件。从该网站:
我可以用我自己的分隔符和引号字符?
是的。有
这迎合提供自己的分隔符,报价构造
字符。假设你正在使用您的分离选项卡,你可以做
是这样的:CSVReader读卡器=新CSVReader(新的FileReader(yourfile.csv),\\ t');
块引用>我觉得这是你想要做什么。你问这个库来解析与制表符分隔值而不是逗号分隔的输入文件。从上面的例子中,这似乎是你的输入格式是什么。
I have a file that looks like this:
1st 2nd nth e1 v1 1 e1 v3 2 e1 v4 4 e1 v5 7 e2 v1 1 . . . . . . . . .
I want the first column to be the name of the arraylist (e1, or e2, or e3), I want the second column to have it's value (an int), inside the nth index.
How would I go about doing that?
Id you'd like further clarification on what I mean, please don't be afraid to leave a pm.
p.s. This is part of a bigger project, but I'm kind of stuck at the format of the data.
I'm assuming i'll have to look for an arraylist with the the name (first column) and if i find it, then add the value (2nd column) at the nth value (3rd column), and if I don't find it, I make an arrayList, and THEN do the aforementioned.
Update
So I managed to get the program almost working -- I couldn't use the original structure of ArrayLists because all variables have to be declared during compilation -- can't be declared after. I decided to use a hashmap, with the key has the 1st value, and an arraylist of the 2nd values in the nth position:
`HashMap<String, ArrayList<Integer>> userRatings = new HashMap<String, ArrayList<Integer>>(); if(!userRatings.containsKey(user)) //The user does not have ratings. { ArrayList<Integer> ratings = new ArrayList<Integer>(); // ratings = userRatings.get(user); for (int j = 0; j < 50; j++) { ratings.add(j); } userRatings.get(user).add(location,value); } } else //The user has ratings { userRatings.get(user).add(location,value);** } System.out.println(user + " " + userRatings.get(user)); } bufferReader.close(); } catch (FileNotFoundException e) { System.out.println("File does not exist or could not be found."); } catch (IOException e) { System.out.println("Can't read from file"); } catch (NullPointerException e) { }`
The problem I now have is in adding to the arraylist. How would I do that in the bolded areas? -- How would I populate and manipulate the arraylist?
解决方案To be honest, I think the initial problem is in reading the file in that format.
This is easy if you use the right library. Take a look at this one: http://opencsv.sourceforge.net/, which is meant to parse csv files. From that site:
Can I use my own separators and quote characters?
Yes. There are constructors that cater for supplying your own separator and quote characters. Say you're using a tab for your separator, you can do something like this:
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t');
I think this is exactly what you're trying to do. You're asking this library to parse the input file with tab-delimited values instead of comma-delimited. From your example above, that seems to be what your input format is.
这篇关于从文件的ArrayList(具体格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!