[英]Java data structure for handling 2D points, array, list or other?
I'am a Ruby developer, thanks to the Android platform, I will give Java a chance. 我是Ruby开发人员,由于有了Android平台,我将给Java一个机会。
I have a set of 2D points [p1,p2,p3,p4...p10], I want to reorder the set, sorted ascending by the x values of the points. 我有一组2D点[p1,p2,p3,p4 ... p10],我想对这些点重新排序,按点的x值升序排列。 In Ruby I would do something like this:
在Ruby中,我将执行以下操作:
points.sort! {|a,b| a.x <=> b.x}
I want be able to insert into the set new points, between the existing ones, in Ruby: 我希望能够在Ruby中的现有点之间插入新的设置点:
points.insert(3, point)
What is the best Java practice to accomplish this ? 实现此目的的最佳Java做法是什么?
Should the points be stored in an array, and the sort and inserting mechanism designed by me ? 应该将点存储在数组中,以及我设计的排序和插入机制吗?
Or exists already a structure for this purpose ? 还是已经存在用于此目的的结构? (which one, for simplifing inserting another elements, between the existing ones, and sorting elements by their properties)
(其中一个,用于简化在现有元素之间插入另一个元素,并按其属性对元素进行排序)
I know there are plenty Java resources over the Internet, but I would like to know opinions of people who has practice with this kind of problem. 我知道Internet上有很多Java资源,但是我想了解那些有实践经验的人的意见。
There are entire books devoted to your question (data structures), however I will try to keep it simple. 整本书都专门讨论您的问题(数据结构),但是我会尽量简化。 There are many MANY options such as heap sort, qucksort, merge sort, binary trees, etc... but rather than learning these methods I would suggest the simple built in features.
有很多选项,例如堆排序,qucksort,合并排序,二叉树等。但是我不建议学习这些方法,而是建议使用简单的内置功能。
Arrays.sort(pa) ; Arrays.sort(pa) ; Sorts the elements of the array of a primitive type into ascending order using their natural ordering.
使用其自然顺序将基本类型数组元素按升序排序。
Arrays.sort(pa, from, to); Arrays.sort(pa,from,to); Sorts the elements pa[from]...pa[to-1] of a primitive type.
对基本类型的元素pa [from] ... pa [to-1]进行排序。 into ascending order.
升序。
Arrays.sort(oa); Arrays.sort(OA); Sorts the elements of the array of an object type into ascending order, using the order defined by Comparable interface, which defines the compareTo method.
使用Comparable接口定义的顺序将对象类型的数组的元素按升序排序,该接口定义了compareTo方法。 Note that many Java classes such as String (but not StringBuffer), Double, BigInteger, etc implement Comparable.
请注意,许多Java类(例如String(但不是StringBuffer),Double,BigInteger等)实现Comparable。
Arrays.sort(oa, from, to); Arrays.sort(oa,from,to); Sorts the elements of the array, in the range from...to of an object type into ascending order.
按对象类型从...到范围的升序对数组中的元素进行排序。
Arrays.sort(oa, comp) ; Arrays.sort(oa,comp) ; Sorts the elements of the array of an object type into ascending order, using the Comparator comp.
使用Comparator comp将对象类型的数组的元素按升序排序。
Arrays.sort(oa, from, to, comp); Arrays.sort(oa,from,to,comp); Sorts the elements of the array, in the range from...to of an object type into ascending order using the Comparator comp.
使用Comparator comp将对象类型从...到范围的数组元素按升序排序。
import java.util.Arrays;
public class Dblsrt {
//========================================================= main
public static void main(String[] args) {
//... 1. Sort strings - or any other Comparable objects.
String[] names = {"Zoe", "Alison", "David"};
Arrays.sort(names);
System.out.println(Arrays.toString(names));
//... 2. Sort doubles or other primitives.
double[] lengths = {120.0, 0.5, 0.0, 999.0, 77.3};
Arrays.sort(lengths);
System.out.println(Arrays.toString(lengths));
}
}
Output: 输出:
[Alison, David, Zoe]
[0.0, 0.5, 77.3, 120.0, 999.0]
Compliments of, http://www.leepoint.net/notes-java/data/arrays/70sorting.html 赞扬http://www.leepoint.net/notes-java/data/arrays/70sorting.html
For a more detailed look, http://www.theparticle.com/javadata2.html 有关更详细的信息, 请访问http://www.theparticle.com/javadata2.html
It depends on your performance needs. 这取决于您的性能需求。 For your use, you might go for a
SortedSet
implementation such as TreeSet and write a Comparator for your Point class (or for an existing one such as Point2D ) that sorts according to the element x value. 供您使用,您可以使用
SortedSet
实现(例如TreeSet),并为Point类(或现有的Point2D之类 )编写一个Comparator ,该Comparator根据元素x值进行排序。 This will let you insert and retrieve elements in O(log(n))
. 这将使您可以插入和检索
O(log(n))
元素。
Note that if you make a lot of insertions, you should not use an Array
, because n insertions in the middle of an Array
has a n^2
cost. 请注意,如果您进行大量插入操作,则不应使用
Array
,因为在Array
中间插入n次插入会产生n^2
开销。 If you traverse the data a lot more than you update it, though, an Array
can make sense. 但是,如果遍历数据比更新数据多得多,则
Array
很有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.