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

    Default Java Points help

    I need some help with java points, I need to add instances of points to an arraylist. Here is what i have.

    Java Code:
    package medianmedianline;
    import java.util.*;
    import java.awt.*;;
    public class Main {  
        public static void main(String[] args) {
             Point p = new Point();
             ArrayList <Point> point = new ArrayList();
             p.x = 5;
             p.y = 5;
             point.add(p);     
             p.x = 10;
             p.y = 10;
             point.add(p);
             System.out.println("Points: " + p);
        }
    }
    Unfortunately, the output is "Points: java.awt.Point[x=10,y=10]" where as i would like the output to "Points: [x=5,y=5], [x=10,y=10]" or something where i can pull the points out of the arraylist. and do with the values as i want.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Loop through the arraylist.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You are adding a reference to a point object, but you are adding the same reference each time you think you are adding a new point, and you are changing that reference's fields (x and y) each time you set x and y. The solution is to create a new Point every time you want to add a new point to the list.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Fubarable View Post
    That won't solve his reference problem.
    Didn't even see that, but his question seemed to me to be how to get the points out of the list i.e. the statement

    "where i can pull the points out of the arraylist. and do with the values as i want"

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

    Default

    Ok, so i would have to add a new point object every time, but, then i would have to add an instance of point for every point i want, not allowing me to actively add? Also, how would looping the array help me?

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because you seem to be wanting to "use the points" by getting a String representation of the arraylist which is completely contradictory to the purpose of having a list. You loop through the list working on each point one at a time.

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

    Default

    Oh, Masijade, i understand how to do all that, its not arraylist that is the problem here, its the points object. ive done arrays with many different variable types, but Point is new to me, but i need it, as im developing a math function. Im testing my concepts in a median-median line math function first though.

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

    Default

    Also, misijade, when i said why do i would i use a loop, it was more of a that wouldn't make sense comment then a literal question.

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    According to your question it would as your question, as worded, seemed to have more to do with the list and how to use it as it did with Point. In any case I wonder wha the getX, getY, and setLocation methods (clearly defined in the API docs) are for.

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

    Default

    Haha, well i would agree on poor wording. I assume, by the hinted question at the end there is what i should go look at? And please can you give me a a quick little definition/example for those? as i am not to savvy at reading the Docs or specs on defined methods.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Jcbconway View Post
    Ok, so i would have to add a new point object every time
    Yes.



    , but, then i would have to add an instance of point for every point i want, not allowing me to actively add?
    ???
    I don't know what you're saying here, but yes, you need a new instance of Point every time. This is not a Point issue per se, but is true for any object that is added to an ArrayList.

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, they seem pretty self-explanatory to me. In any case you will need to learn how to read the API docs eventually, and the Java API docs are, IMHO, some of the easiest to read and use and some of the best organised of any language. See for yourself. Or, just the Point Class.

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Ok, so i would have to add a new point object every time, but, then i would have to add an instance of point for every point i want, not allowing me to actively add?

    What is it to "actively add"? (real question)

    If you want output describing a whole bunch of points then you have to put a whole bunch of references to points into a collection. And those points will have to be created.

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

    Default

    Ok, my wording is messing up my statements here. haha, If arraylist list1 had nothing in it and i printed it on the screen it would be [] blank. now if i say list1.add(5), list1.add(6), list1.add(7), the output would be [5,6,7] element 0 would be 5, element 1 would be 6, and element 2 would be 7. Now, i could loop and input where the user can enter as many values into the array list as they want. I would like to basically do the same things with points.

  16. #16
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    So do it. Have the user enter two numbers, call new Point and call add. The "print" output will be as shown in the first post as that is how the toString method of List is defined. If you want something else, then loop through the list and create the output yourself (write a method for that or extend ArrayList, or some other List implementation, and redefine the toString method).

  17. #17
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Jcbconway View Post
    Haha, well i would agree on poor wording. I assume, by the hinted question at the end there is what i should go look at? And please can you give me a a quick little definition/example for those? as i am not to savvy at reading the Docs or specs on defined methods.

    each point you add to your arraylist must exist as an object. the following code show this for two points


    Java Code:
    public class Main {
    	public static void main(String[] args) {
    		Point p1 = new Point();
    		Point p2 = new Point();
    		ArrayList<Point> point = new ArrayList();
    		p1.x = 5;
    		p1.y = 5;
    		point.add(p1);
    		p2.x = 10;
    		p2.y = 10;
    		point.add(p2);
    
    		for (Point p : point) {
    			System.out.println("x = " + p.x + "\ty = " + p.y);
    		}
    	}
    }

    you can also add a point to arraylist by

    point.add(new Point(20, 20));

    good luck!

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

    Default

    AH! so, i could just do, point.add(new Point(x,y)); where the user enters x, and y, and just add it to the the arraylist, so i wouldn't need an instance of point each time. or would i still need to?

  19. #19
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    That is creating and adding the pont at the same time (or, rather, on the same line of code, as the Point will still be created first, then added into the list you're just doing it in one line instead of two). It is still a new Point as attested to by the new keyword.
    Last edited by masijade; 10-26-2010 at 11:19 PM.

  20. #20
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The process is the same.

    Adding integers: The user enters strings, you do some work to obtain int values, finally you call add() and the list becomes populated.

    Adding points: The user enters strings, you do some work to obtain Point instances, finally you call add() and the list becomes populated.

    -----

    Although you don't have to worry about it, when you add int values as in your example, the list actually gets populated with references to Integer instances. So the two processes are not analogues, but identical.

Page 1 of 2 12 LastLast

Similar Threads

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