java 存储字符串列表的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15232743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:56:12  来源:igfitidea点击:

best way to store a list of strings

javaarraysstring

提问by user1538814

What would be the best way to store and read a really long string, with each entry is an index for another array?

存储和读取非常长的字符串的最佳方法是什么,每个条目都是另一个数组的索引?

Right now I have this

现在我有这个

String indices="1,4,6,19,22,54,....."

The string has up to hundred of thousand entries, so I think maybe I could use a data structure like Linked List. Does anyone know if it would be faster to use one?

这个字符串有多达十万个条目,所以我想也许我可以使用像链表这样的数据结构。有谁知道使用一个会更快吗?

回答by PSR

List<String> list = new ArrayList<String>();

list.add("1");
list.add("2");

you need to declare arraylist of type string.Then add to it.

您需要声明字符串类型的arraylist。然后添加到它。

回答by Filipe Fedalto

It would depend on what you'll do with the string (the indices) and the corresponding arrays. Also, it will depend on how you're gonna access them.

这取决于您将如何处理字符串(索引)和相应的数组。此外,这将取决于您将如何访问它们。

I'd suggest you first read an overview about the data structures implemented in java, specially in the Collections Framework.

我建议您首先阅读有关在 java 中实现的数据结构的概述,特别是在Collections Framework 中

We could give some suggestions, but you'd have to provide us more information, specially those I mentioned in the beginning (what you want, how this data will be stored and accessed, and so on).

我们可以提供一些建议,但您必须向我们提供更多信息,特别是我在开头提到的那些信息(您想要什么,这些数据将如何存储和访问,等等)。

For example, if you need to have a fast access to the indexed data, maybe a string isn't even the best approach. Maybe a map would be better. The indexes could be the keys and the indexed arrays could be the values of the map, for example. But this is just a void example, I strongly suggest you give us more information.

例如,如果您需要快速访问索引数据,那么字符串可能甚至不是最好的方法。也许地图会更好。例如,索引可以是键,索引数组可以是映射的值。但这只是一个无效的例子,我强烈建议您提供更多信息。

回答by Hoon

I really like using the ArrayList class, which if your comfortable using arrays, ArrayList or any member of the Collections Framework. Would work really well. For what your trying to do.

我真的很喜欢使用 ArrayList 类,如果你习惯使用数组、ArrayList 或集合框架的任何成员。会很好用。对于你想要做什么。

ArrayList<String> indices = new ArrayList<String>();
indices.add("");

回答by Abhishek Garg

I have similar hunch in my mind , in which I want to like 1k number of strings and parse them (searching purpose to know it contain item or not).

我有类似的预感,我想喜欢 1k 个字符串并解析它们(搜索目的是知道它是否包含项目)。

Hence I found instead of using java collection framework - map or set or list

因此我发现而不是使用 java 集合框架 - map 或 set 或 list

if I store data simply in array and start parsing data using for-loop, it is faster.

如果我将数据简单地存储在数组中并开始使用 for 循环解析数据,它会更快。

You visit this link and see actual output which we calculated in micro seconds.

您访问此链接并查看我们以微秒计算的实际输出。

https://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/

https://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/

So using simple brute force is winner in case of unsorted array (normally we have).

因此,在未排序的数组(通常我们有)的情况下,使用简单的蛮力是赢家。

But arrays.BinarySearch()is winner if array is sorted.

arrays.BinarySearch()如果数组已排序,则是赢家。