关于Java的LinkedList类的问题
我有一个关于 Java 中的 LinkedList 类的问题。 我有一个场景,其中我需要根据索引是否存在于链表中来添加或设置索引。我想要实现的伪代码是——
if index a exists within the linkedlist ll
ll.set(a,"arbit")
else
ll.add(a,"arbit")
我确实浏览了 LinkedList 类的 Javadocs,但没有遇到任何相关的内容。
有什么想法吗?
谢谢 p1ng
I have a question regarding the LinkedList class in Java.
I have a scenario wherein i need to add or set an index based on whether the index exists in the linkedlist or not. A pseudo-code of what i want to achieve is --
if index a exists within the linkedlist ll
ll.set(a,"arbit")
else
ll.add(a,"arbit")
I did go through the Javadocs for the LinkedList class but did not come across anything relevant.
Any ideas ?
Thanks
p1ng
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为此使用 Map 怎么样:
甚至如果
a
已经存在,put
将只替换旧的字符串。What about using a Map for this:
Even if
a
already exists,put
will just replace the old String.在链表中搜索效率不高(O(n))。您是否考虑使用不同的数据结构 - 例如 HashMap 会给您 O(1) 访问时间?
Searching in linked list is not very efficient (O(n)). Have you considering using different data structure - e.g. HashMap which would give you O(1) access time?
如果您需要顺序访问以及键控访问,您可能需要尝试 LinkedHashMap,从 1.4.2 开始可用
http://download.oracle.com/javase /1.4.2/docs/api/java/util/LinkedHashMap.html
If you need sequential access as well as keyed access you might want to try a LinkedHashMap, available as from 1.4.2
http://download.oracle.com/javase/1.4.2/docs/api/java/util/LinkedHashMap.html
Map
绝对是一个很好的(最好的?)方法。如果出于某种奇怪的原因需要,这里有一个选项可以保留
LinkedList
。它具有可怕的运行时性能并且不允许 null,因为 null 现在成为索引未被占用的指示符。如果您打算走这条路,那么使用
ArrayList
可能会更好。为什么这么糟糕?假设索引 100,000 处只有一个元素。此实现需要列表中有 100,000 个指向
null
的条目。这会导致可怕的运行时性能和内存使用。Map<Integer, String>
is definitely a good (the best?) way to go here.Here's an option for keeping with
LinkedList
if that's for some bizarre reason a requirement. It has horrible runtime performance and disallows null, since null now becomes an indicator that an index isn't occupied.If you're going to take this gross road, you might be better off with an
ArrayList
.Why is it so bad? Say you had only one element at index 100,000. This implementation would require 100,000 entries in the list pointing to
null
. This results in horrible runtime performance and memory usage.LinkedList 内部不能有洞,所以你不能有列表
[1,2,3,4]
然后ll.add(10,10)
,所以我认为你的例子有问题。使用Map
或搜索其他稀疏数组LinkedList cannot have holes inside, so you can't have list
[1,2,3,4]
and thenll.add(10,10)
, so I think there's something wrong with your example. Use eitherMap
or search for some other sparse array看起来您正在尝试使用
a
作为键,并且不说明索引i
i
处是否有项目。一个。如果您在
ll.size() <= a
时运行代码,那么最终会出现NullPointerException
。如果您在索引
a
处添加一个项目,则a
处的前一个项目现在将位于a+1
处。在这种情况下,最好先删除
a
处的项目(如果存在),然后将项目"arbit"
添加到a
中。当然,上面的条件:ll.size() <=a
在这里仍然适用。如果结果的顺序很重要,则可以使用不同的方法使用
HashMap<Integer,String>
创建数据集,然后使用HashMap<? 提取键,?>.getKeySet()
然后按自然顺序对它们进行排序(毕竟它们是数字),然后在迭代 keySet 时从映射中提取值。讨厌,但是做你想要的...或者创建你自己的 OrderedMap 类,它的作用是一样的...你能详细说明为什么你需要使用 LinkedList 吗?结果的排序重要吗?
It looks like you're trying to use
a
as a key, and don't state whether you have items at indexi < a
. If you run your code whenll.size() <= a
then you'll end up with aNullPointerException
.And if you add an item at index
a
the previous item ata
will now be ata+1
.In this case it would be best to remove item at
a
first (if it exists) then add item"arbit"
intoa
. Of course, the condition above re:ll.size() <=a
still applies here.If the order of the results is important, a different approach could use a
HashMap<Integer,String>
to create your dataset, then extract the keys usingHashMap<?,?>.getKeySet()
then sort them in their natural order (they're numeric after all) then extract the values from the map while iterating over the keySet. Nasty, but does what you want... Or create your own OrderedMap class, that does the same...Could you expand on why you need to use a LinkedList? Is ordering of the results important?