473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

strange behavior of ArrayList get() method

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!
Jul 17 '05 #1
3 9636
This part of the code is fine (though I would use an Iterator instead). The
problem is probably in your code to create the list. Perhaps you could post
that?

--
--
Jim McMaster
mailto: ji**********@comcast.net
"b83503104" <b8*******@yahoo.com> wrote in message
news:73**************************@posting.google.c om...
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!

Jul 17 '05 #2
Hi,
the code 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!
"Jim McMaster" <ji**********@comcast.net> wrote in message news:<vM********************@comcast.com>...
This part of the code is fine (though I would use an Iterator instead). The
problem is probably in your code to create the list. Perhaps you could post
that?

--
--
Jim McMaster
mailto: ji**********@comcast.net
"b83503104" <b8*******@yahoo.com> wrote in message
news:73**************************@posting.google.c om...
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!

Jul 17 '05 #3
b83503104 wrote:
Hi,
the code 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!


The above code is missing the declaration of test_point. Based on what
I see above, I would bet it is a instance variable.

The problem is that you keep adding the same instance of test_point to
the list. You modify the instance and add it again. But the list only
keeps a reference to the instance, so it only has a reference to the
same object in every slot.

(Perhaps you are used to C++ and std::vector. If I remember correctly,
vector will make a copy of everything added to it. If you are coming
from a C++ background, a useful analogy is to think of all Java
variables (except primitives) as pointers to objects.)

What you want is to make a new instance of point2D every time:

public void mouseDragged(MouseEvent evt)
{
point2D 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 );
}

HTH,
Ray
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: john | last post by:
Hi,All Gurus: It is kind of complicated, please bear with me and let me know if you have any questions. Thanks a lot in advance. John I have a csharp method, using emit to dynamically generate...
1
by: Jamus Sprinson | last post by:
Before I continue, I'm going to begin by saying I'm not by any means an expert- I've been using .NET with C# for about 4 months now, and basically just learning by example and docs. A game...
0
by: CalPARK InterNetStation | last post by:
Could anyone explain why the difference of behavior betweein Case1 and Case2 occurs? Microsoft Visual C#.NET 55607-652-0000007-18218 Windows Forms Application 1. put a TabControl on a Form...
10
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
3
by: Arnold Schrijver | last post by:
I wrote a program that draws items to the screen and maintains a set of Offset values. There was a bug in the code, because objects were positioned wrongly. While debugging I found some peculiar...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
2
by: almurph | last post by:
H ieveryone, Can you help me please? I am trying to sort a hashtable but get the error: "Cannot implicity convert type void to System.Collections.ArrayList" I am doing the following: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.