简体   繁体   English

将字符串从数组列表字符串转换为数组列表字符串

[英]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:你在这里有几个问题:

  1. You shouldn't capitalize a variable name in Java (by convention.) Doing so makes your code much harder for other people to read/debug.您不应该在 Java 中将变量名大写(按照惯例)。这样做会使您的代码更难被其他人阅读/调试。 Therefore, you should change your code sample to be 'someArrayList' or 'arrayList'.因此,您应该将代码示例更改为“someArrayList”或“arrayList”。 This would help clear things up.这将有助于澄清事情。
  2. You misunderstand the add method of ArrayLists and the nextLine method of whatever reader you are using.您误解了 ArrayLists 的 add 方法和您使用的任何阅读器的 nextLine 方法。 'add' does no parsing whatsoever, and 'nextLine' also does no parsing. 'add' 不做任何解析,'nextLine' 也不做解析。 If your file currently is [5,1,3] and you call your code, it will simply add the whole [5,1,3] String to your ArrayList as a single entry.如果您的文件当前是 [5,1,3] 并且您调用您的代码,它只会将整个 [5,1,3] 字符串作为单个条目添加到您的 ArrayList 中。 This is why you are seeing this nesting behavior.这就是您看到这种嵌套行为的原因。
  3. Now, when you're talking about where the brackets even come from, we're talking about the toString method of ArrayList (which you are probably calling implicitly).现在,当您讨论括号的来源时,我们讨论的是 ArrayList 的 toString 方法(您可能隐式调用了该方法)。 The toString method of the ArrayList class prints out the entries, separated by commas, with opening and closing brackets. ArrayList 类的 toString 方法打印出条目,以逗号分隔,并带有左括号和右括号。

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.将来您最好更彻底地研究一般主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM