Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31
  1. #21
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    haha! thank you, you understood the question, it was much more simple then i was making it. heres the new code!

    Java Code:
    package medianmedianline;
    import java.util.*;
    import java.awt.*;;
    public class Main {  
        public static void main(String[] args) {
             Point p = new Point();
             int count = 0;
             int x = 0;
             int y = 0;
             ArrayList <Point> point = new ArrayList();
    
             while(count <= 10){
             x = count;
             y = count;
             point.add(new Point(x,y));
             count++;
            }
    
             System.out.println("Points: " + point);
        }
    }
    the output is "Points: [java.awt.Point[x=0,y=0], java.awt.Point[x=1,y=1], java.awt.Point[x=2,y=2]...." Haha thank you guys (: im sure.... actually i know ill be back here for help!

  2. #22
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    How about this....
    Java Code:
    ArrayList<Point> myPoints=new ArrayList<Point>();
    Point p;
    for(int i=0;i<someValue;i++){
    // get two points and store in variables x and y
    p=new Point(x,y);
    myPoints.add(p);
    System.out.println("["+myPoints.get(i).getX()+", "+myPoints.get(i).getY()+"]");
    }

  3. #23
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Marshy, isnt that kind of doing the same thing? haha, this may be related to your snippet of code or not, but, is there a way to pull all of the x variables from the arraylist, and sort the arrylist by X acceding order, so like, arralyist: [[1,2],[3,2],[2,2]], after sorting it would look like [[1,2],[2,2],[3,2]]. haha, this is easy with other variables but the Collections.sort(point); method, doesn't work. or at least it doesn't the way i have it written.

  4. #24
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    haha yeah I posted without realising you got it. Hmm maybe extending Point and implementing Comparable

  5. #25
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Haha ok, i will need help with a few things. i may be able to do it, if i could return the x's of the points in the arraylist to int's. Being able to do that would also help me with a lot of other things.

  6. #26
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    why dont you use the ArrayList's get() method to get a Point and then use .getX()

    Java Code:
    ArrayList<Point> myPoints=new ArrayList<Point>(Arrays.asList(new Point(2,2),new Point(3,3)));
    
    int x=(int)myPoints.get(0).getX();

  7. #27
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Ah geez haha, i keep forgetting ordering operations today, yes, then i can accomplish that task for getting x without needing to give the point a reference name.

  8. #28
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You may want a collection of x-coords for some other reason, but you don't need them for sorting the points.

    Use the version of sort() that takes a Comparator argument.

    Implementing Comparable makes sense if there is a natural ordering in your particular case. But it may equally be the case that you want to sort based on different criteria: x-coord, y-coord, distance from the origin, angle from the horizon etc. You would have to do a bit of study on Comparators but, once mastered, they are very flexible.

  9. #29
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Yea, my research has told me this, I notices that there is natural order, and another type of ordering. is natural order, order only based on like variables? Allso, i need equal elements to be sorted in my case.

  10. #30
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    "Natural order" doesn't really have a meaning! It's ordering ... that is natural.

    Show sizes have a natural ordering: from little to big. But shoes themselves can be ordered in all sorts of ways: price, style, age of intended foot etc. In Java code these may or may not be associated with instance variables (that's an implementation detail, whereas being comparable in a certain way is a property of the type of thing regardless of how it is implemented.) And some instance variables may represent things that have no natural ordering.

    As I suggested in my other post Point is more like Shoe than it is like Weight. There doesn't seem to me to be any one way of comparing Point instances that is more natural than all others. (And my example was chosen to illustrate the fact that a trivial change in coordinate system might result in quite different ordering requirements.)

  11. #31
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Ok, i understand. Now how do organize my list? ive used the sort meathod alot, but its the comparable sort thats the problem, please explain how i would use it in my code example on my other post?

Page 2 of 2 FirstFirst 12

Similar Threads

  1. drawing points with mouse
    By josephdcoleman in forum New To Java
    Replies: 4
    Last Post: 03-03-2009, 12:39 AM
  2. Drawing points on a JPanel
    By josephdcoleman in forum New To Java
    Replies: 6
    Last Post: 02-25-2009, 03:47 PM
  3. Percentage/decimal points?
    By Exhonour in forum New To Java
    Replies: 6
    Last Post: 01-16-2009, 10:35 PM
  4. Replies: 0
    Last Post: 11-25-2008, 09:24 AM
  5. Replies: 2
    Last Post: 08-24-2008, 12:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •