关于Java的LinkedList类的问题

发布于 2024-10-11 17:31:33 字数 290 浏览 3 评论 0原文

我有一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一刻暧昧 2024-10-18 17:31:33

为此使用 Map 怎么样:

Map<Integer, String> map = new HashMap<Integer, String>();

// ...

int a = 5;

map.put(a, "arbit");

甚至如果a已经存在,put将只替换旧的字符串。

What about using a Map for this:

Map<Integer, String> map = new HashMap<Integer, String>();

// ...

int a = 5;

map.put(a, "arbit");

Even if a already exists, put will just replace the old String.

过期以后 2024-10-18 17:31:33

在链表中搜索效率不高(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?

夏末的微笑 2024-10-18 17:31:33

如果您需要顺序访问以及键控访问,您可能需要尝试 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

靖瑶 2024-10-18 17:31:33

Map绝对是一个很好的(最好的?)方法。

如果出于某种奇怪的原因需要,这里有一个选项可以保留 LinkedList 。它具有可怕的运行时性能并且不允许 null,因为 null 现在成为索引未被占用的指示符。

String toInsert = "arbit";
int a = 5;

//grow the list to allow index a
while ( a >= ll.size() ) {
   ll.add(null);
}

//set index a to the new value
ll.set(a, toInsert);

如果您打算走这条路,那么使用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.

String toInsert = "arbit";
int a = 5;

//grow the list to allow index a
while ( a >= ll.size() ) {
   ll.add(null);
}

//set index a to the new value
ll.set(a, toInsert);

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.

七度光 2024-10-18 17:31:33

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 then ll.add(10,10), so I think there's something wrong with your example. Use either Map or search for some other sparse array

雾里花 2024-10-18 17:31:33

看起来您正在尝试使用 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 index i < a. If you run your code when ll.size() <= a then you'll end up with a NullPointerException.

And if you add an item at index a the previous item at a will now be at a+1.
In this case it would be best to remove item at a first (if it exists) then add item "arbit" into a. 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 using HashMap<?,?>.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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文