[英]Convert a string from an Array List String into an Array List String
I'm working on a way to save an ArrayList String into a .txt document, then open it up, modify the ArrayList String then close it etc.我正在研究一种将 ArrayList 字符串保存到 .txt 文档中,然后打开它,修改 ArrayList 字符串然后关闭它等的方法。
Currently, I'm using this to open the ArrayList up again:目前,我正在使用它再次打开 ArrayList:
ArrayList.add(fileIn.nextLine())
This works, but not as expected because after some saves of the Array List this happens:这有效,但不是预期的,因为在对数组列表进行一些保存后,会发生这种情况:
[ [ [ [ ], 7,68, 9,71, 11,73, 13,72, 12,70], 9,72, 11,73, 13,72, 12,70, 10,69, 9,71], 6,76, 6,78, 8,77, 8,75, 7,74]
The array list 'overlaps'.数组列表“重叠”。 Is there any way to save or load the array list without it overlapping?有没有办法在不重叠的情况下保存或加载数组列表?
Thanks!谢谢!
You have a few problems here:你在这里有几个问题:
To solve #2 and #3, you will need to either use a library (as suggested elsewhere) or do it yourself by looping through and printing each item in the list.要解决 #2 和 #3,您需要使用库(如其他地方所建议的)或通过循环遍历并打印列表中的每个项目来自己完成。 If you do this, you will also need to choose and use a separator whcih you know won't be present in any of the items (comma is usually good).如果您这样做,您还需要选择并使用您知道不会出现在任何项目中的分隔符(逗号通常是好的)。 Finally, you will have to parse the results of the nextLine method.最后,您必须解析 nextLine 方法的结果。 Here's a quick snippet for that.这是一个快速片段。
String nextLine = fileIn.nextLine();
arrayList.addAll(nextLine.split(","));//I use comma here, since I assume you will be separating your list with commas
Finally, a small tip for adding separators... make sure you don't put a hanging separator after the last item in your list.最后,一个关于添加分隔符的小技巧......确保你没有在列表中的最后一项之后放置一个悬挂的分隔符。
This question is, I think, unique (I can't find another question about printing and then reading an ArrayList,) although there are other questions about both printing ArrayList's and parsing files.这个问题,我认为,唯一的(我无法找到关于打印,然后再读一个ArrayList,另一个问题),但大约有两个打印的ArrayList的和解析的文件等问题。 You may be better off researching the general topics more thoroughly in the future.将来您最好更彻底地研究一般主题。
Why do the work by hand, when you could use something like:为什么要手工工作,当你可以使用类似的东西时:
and和
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.