strange behavior of ArrayList get() method

B

b83503104

Hi,

when I use .get() to read out my ArrayList, I always get the last
element. Below is part of my code:

for (int index = 0; index < my_list.size(); index++) {
point2D test_point;
test_point = (point2D)my_list.get( index );
System.out.println( index );
System.out.println( test_point.x );
System.out.println( test_point.y );
};

It turns out that the index correctly prints out (0,1,2,...), but the
x and y values are all the same -- being the last value I wrote into
ArrayList.

Need your help, thanks!
 
C

Christophe Vanfleteren

b83503104 said:
Hi,

when I use .get() to read out my ArrayList, I always get the last
element. Below is part of my code:

for (int index = 0; index < my_list.size(); index++) {
point2D test_point;
test_point = (point2D)my_list.get( index );
System.out.println( index );
System.out.println( test_point.x );
System.out.println( test_point.y );
};

It turns out that the index correctly prints out (0,1,2,...), but the
x and y values are all the same -- being the last value I wrote into
ArrayList.

Need your help, thanks!

The get(index) method works as you'd expect it. I'm more curious about the
code where you add the Points to the ArrayList.
 
J

John C. Bollinger

Christophe said:
b83503104 wrote:




The get(index) method works as you'd expect it. I'm more curious about the
code where you add the Points to the ArrayList.

Which very likely repeatedly modifies one Point and adds it to your
list, instead of creating new Points. The result is a list with the
_same object_ at every populated position. That object naturally has
whatever coordinates you set on it last.

Christophe is being careful by not being so specific, but this is a
fairly common mistake and I'm sure he also suspects what I just described.


John Bollinger
(e-mail address removed)
 
B

b83503104

Hi,
the code I used to create the list is here:

ArrayList my_list = new ArrayList();
...
public void mouseDragged(MouseEvent evt) {
test_point.x = evt.getX();
test_point.y = evt.getY();
my_list.add(test_point);
System.out.println( test_point.x );
System.out.println( test_point.y );
}

The "test_point" is an instance of this class:
class point2D {
int x;
int y;
}

and declared like this:
point2D test_point = new point2D();

From the println print out I see all test_point.x (generated when the
mouse is dragged) are correct. So I think the values added into the
ArrayList "should" also be correct. But that isn't true as I said in
my original post. Can you see a bug here?
Or does the problem come from test_point?
Thanks!
 
S

Sudsy

b83503104 said:
Hi,
the code I used to create the list is here:

ArrayList my_list = new ArrayList();
...
public void mouseDragged(MouseEvent evt) { test_point = new point2d();
test_point.x = evt.getX();
test_point.y = evt.getY();
my_list.add(test_point);
System.out.println( test_point.x );
System.out.println( test_point.y );
}

As John pointed out, and Christophe also probably surmised, you
have to create a new instance each time. The modified code above
should work for you. If you need to ask why then you don't have
a very good grounding in the fundamentals of Java. Best post
your next question to comp.lang.java.help.
 
R

Ryan Stewart

*fixed top post*
"John C. Bollinger" <[email protected]> wrote in message

Hi,
the code I used to create the list is here:

ArrayList my_list = new ArrayList();
...
public void mouseDragged(MouseEvent evt) {
test_point.x = evt.getX();
test_point.y = evt.getY();
my_list.add(test_point);
System.out.println( test_point.x );
System.out.println( test_point.y );
}

The "test_point" is an instance of this class:
class point2D {
int x;
int y;
}

and declared like this:
point2D test_point = new point2D();

From the println print out I see all test_point.x (generated when the
mouse is dragged) are correct. So I think the values added into the
ArrayList "should" also be correct. But that isn't true as I said in
my original post. Can you see a bug here?
Or does the problem come from test_point?
Thanks!
1) Please don't top post. 2) Please don't multipost. 3) The problem is what
John said. You've only created one point and stored it in the ArrayList
multiple times.
 
K

Karthik

b83503104 said:
Hi,
the code I used to create the list is here:

ArrayList my_list = new ArrayList();
...
public void mouseDragged(MouseEvent evt) {
test_point.x = evt.getX();
test_point.y = evt.getY();
my_list.add(test_point);
System.out.println( test_point.x );
System.out.println( test_point.y );
}

The "test_point" is an instance of this class:
class point2D {
int x;
int y;
}

and declared like this:
point2D test_point = new point2D();

From the println print out I see all test_point.x (generated when the
mouse is dragged) are correct. So I think the values added into the

That being the case, the point2D should take two args, to store the
x and y as generated by a mouse move event. But your code shows to be
just a empty constructor. That explains why you get the same value when
you get it.
Think abt having a 2-arg constructor for point2D . ( As a
convention, please have all classes starting with upper-case letter.
That makes it easier to read the code ).

HTH
 
R

Roedy Green

It turns out that the index correctly prints out (0,1,2,...), but the
x and y values are all the same -- being the last value I wrote into
ArrayList.
I suspect the problem is with the code that put them in.
 
R

Roedy Green

test_point.x = evt.getX();
test_point.y = evt.getY();
my_list.add(test_point);

this code should read

my_list.add( new TestPoint( evt.getX(), evt.getY() );

See http://mindprod.com/jgloss/codingconventions.html

Name your class with a capital letter and give it a constructor that
takes x,y

You want a different object for every point otherwise with only one
object you have only one x and one y in the unverse.
 
C

Chris Smith

I'll elaborate a bit more than some of the other responses. See below.
the code I used to create the list is here:

ArrayList my_list = new ArrayList();
...
public void mouseDragged(MouseEvent evt) {
test_point.x = evt.getX();
test_point.y = evt.getY();
my_list.add(test_point);
System.out.println( test_point.x );
System.out.println( test_point.y );
}

As everyone suspected, there is only *one* object of class point2D in
your code. You presumably create it at the beginning, and you keep
changing its properties. To provide a commonly useful metaphor, think
of objects as balloons, and references (like the variable test_point) as
ribbons that you use to hold on to the balloons. When you add an object
to a collection, you're essentially tying a new ribbon to the balloon,
so that the balloon is attached to the collection.

What you're doing is creating one balloon. Every time the mouse is
dragged, you are erasing the numbers you had written on this balloon and
replacing them with new numbers, and then tying another ribbon from this
*one* balloon to the collection. In the end, you still only have one
balloon, and what's written on it is the last set of numbers you wrote
(since you've erased everything else).

You do have a whole lot of ribbons tied to that one balloon, though.
When you ask the collection for its contents, it will hand you those
ribbons one by one; and each time you get a ribbon, you're going to
following it to the balloon it's attached to and read what's written on
that balloon. But since all the ribbons are attached to the same
balloon, you're seeing the same thing every time you do that.

Now, what you really want to do is, each time the mouse is dragged, blow
up a new balloon, write the new numbers on it, and tie a new ribbon from
the collection to that new balloon. To do that, you need to actually
create a new balloon, which you do with the "new" keyword.

test_point = new point2D();
test_point.x = evt.getX();
test_point.y = evt.getY();

Now you've written the information on a new balloon, which can be added
to the collection to make things work as you like.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
B

b83503104

Chris Smith said:
I'll elaborate a bit more than some of the other responses. See below.


As everyone suspected, there is only *one* object of class point2D in
your code. You presumably create it at the beginning, and you keep
changing its properties. To provide a commonly useful metaphor, think
of objects as balloons, and references (like the variable test_point) as
ribbons that you use to hold on to the balloons. When you add an object
to a collection, you're essentially tying a new ribbon to the balloon,
so that the balloon is attached to the collection.

What you're doing is creating one balloon. Every time the mouse is
dragged, you are erasing the numbers you had written on this balloon and
replacing them with new numbers, and then tying another ribbon from this
*one* balloon to the collection. In the end, you still only have one
balloon, and what's written on it is the last set of numbers you wrote
(since you've erased everything else).

You do have a whole lot of ribbons tied to that one balloon, though.
When you ask the collection for its contents, it will hand you those
ribbons one by one; and each time you get a ribbon, you're going to
following it to the balloon it's attached to and read what's written on
that balloon. But since all the ribbons are attached to the same
balloon, you're seeing the same thing every time you do that.

Now, what you really want to do is, each time the mouse is dragged, blow
up a new balloon, write the new numbers on it, and tie a new ribbon from
the collection to that new balloon. To do that, you need to actually
create a new balloon, which you do with the "new" keyword.

test_point = new point2D();
test_point.x = evt.getX();
test_point.y = evt.getY();

Now you've written the information on a new balloon, which can be added
to the collection to make things work as you like.

Thank you, the problem is fixed now. You have been very helpful in
explaining the basics.
Another question:
Every time the mouse is dragged, a new object (Point2D) is created.
Then I add it to my_list (an ArrayList). How do I take care of
deleting all the stuff I have created? Can I simply do .clear()
(method of ArrayList)? Or do I have to delete all the Point2D objects
manually?
Thanks!
 
C

Christophe Vanfleteren

b83503104 said:
Thank you, the problem is fixed now. You have been very helpful in
explaining the basics.
Another question:
Every time the mouse is dragged, a new object (Point2D) is created.
Then I add it to my_list (an ArrayList). How do I take care of
deleting all the stuff I have created? Can I simply do .clear()
(method of ArrayList)? Or do I have to delete all the Point2D objects
manually?
Thanks!

Just clear() the ArrayList. If you have no other references to the
Point2D's, they will get GC'd.

There is no way to delete an object in Java. Actually, you never can mess
with an Object directly, but only with references to objects.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top