开发者

Java data structure for handling 2D points, array, list or other?

开发者 https://www.devze.com 2023-02-28 03:38 出处:网络
I\'am a Ruby developer, thanks to the Android platform, I will give Java a chance. 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 po

I'am a Ruby developer, thanks to the Android platform, I will give Java a chance.

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. In Ruby I would do something like this:

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:

points.insert(3, point)

What is the best Java practice to accomplish this ?

Should开发者_JAVA技巧 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.


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. This will let you insert and retrieve elements in 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. If you traverse the data a lot more than you update it, though, an Array can make sense.


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.

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); Sorts the elements pa[from]...pa[to-1] of a primitive type. into ascending order.

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. Note that many Java classes such as String (but not StringBuffer), Double, BigInteger, etc implement Comparable.

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); Sorts the elements of the array of an object type into ascending order, using the Comparator 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.

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

For a more detailed look, http://www.theparticle.com/javadata2.html

0

精彩评论

暂无评论...
验证码 换一张
取 消