Java 相当于 C# 排序列表

发布于 2024-10-31 18:28:22 字数 278 浏览 0 评论 0原文

可能的重复:
java中的排序集合

我想知道Java是否有自己的排序列表版本,或者如果我需要创建我自己的。我希望列表在删除某些内容时自动更新。例如,如果我从列表的开头甚至中间删除某些内容,我希望它后面的所有内容在列表中向上移动,并删除剩余的空值空间。

Possible Duplicate:
sorted collection in java

I was wondering if Java has its own version of Sorted List, or if I need to create my own. I want the list to automatically update itself if something is removed. For example, if I remove something from the start of the list, or even in the middle, I want everything behind it to move up in the list, and the remaining null value space to be removed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

离去的眼神 2024-11-07 18:28:23

如果您确实想要 .NET SortedList 的等效项(它实际上是按其键排序的映射),那么最接近的等效项可能是 TreeMap。实际上,这更像是 SortedDictionary 而不是 SortedList,因为它是一棵树而不仅仅是一个列表,但它可能是最接近的可用选项。

但是,您所描述的更像是 ArrayList,它类似于 .NET 的 List

If you're really after the equivalent of a .NET SortedList, which is actually a map ordered by its keys, then the closest equivalent is probably TreeMap. That's actually more like SortedDictionary than SortedList, given that it's a tree rather than just a list, but it's probably the closest available option.

However, what you've described is more like ArrayList, which is similar to .NET's List<T>.

花之痕靓丽 2024-11-07 18:28:23

好吧,Java 有很多比数组更智能的列表实现,尽管听起来您并不真正想要从描述中得到一个排序列表。

ArrayListLinkedList 将执行您想要的插入或删除元素操作:

public Object remove(int index) - 删除此列表中指定位置的元素。将所有后续元素向左移动(从索引中减去 1)。

您真的想要一个排序列表,还是只是比数组更高级别的东西?

Well, Java has quite a few list implementations that are smarter than arrays, though it doens't sound like you really want a sorted list from your description.

An ArrayList or LinkedList will do what you want as far as inserting or removing elements:

public Object remove(int index) - Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Do you really want a sorted list, or just something higher-level than an array?

薄荷→糖丶微凉 2024-11-07 18:28:23

java.util.PriorityQueue< /a>

基于优先级堆的无界优先级队列。优先级队列的元素根据其自然顺序进行排序,或者通过队列构造时提供的比较器进行排序,具体取决于使用的构造函数。优先级队列不允许有空元素。依赖自然排序的优先级队列也不允许插入不可比较的对象(这样做可能会导致 ClassCastException)。

这基本上是一个,允许按顺序从前面读取,并允许删除通过 Iterator.remove 从中间开始,但迭代器不按任何特定顺序进行迭代。

如果您想要一些可以按顺序迭代的东西,并且不需要重复,那么 TreeSet 是您最好的选择。如果你需要欺骗,那么看看 Apache common 的库 TreeBag

java.util.PriorityQueue

An unbounded priority queue based on a priority heap. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).

This is basically a heap that allows reading from the front in order, and allows removal from the middle via Iterator.remove but the iterator does not iterate in any particular order.

If you want something that you can iterate over in order, and don't need dupes, then a TreeSet is your best bet. If you need dupes, then look at a library like Apache common's TreeBag.

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