This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Vector object being overwritten when adding new element
jm rajan
Greenhorn
Joined: Feb 29, 2012
Posts: 7
posted
0
Hi
I have created an Object objInfo containing the following members
public String srvName
public String[] input
public String[] output
I want to add different instances of objInfo in a Vector. But when I am trying to obtain the information in the Vector,
the data entered previously is being replaced by the most recent entries except in srvName.
I have used the new keyword.
Please help.
Thanks in advance
It's hard to say exactly what's happening without seeing your code, but from what you describe, it sounds like you're using a single objInfo object, changing its fields, and re-adding that same object to the Vector over and over. Note that when you pass an object to a method, such as vector.add(), there's no copy of the object made. The method just gets a copy of the reference to the same object.
As a side note, unless you are required to use Vector for some reason, it would be better to use ArrayList. So the overall loop would look something like this:
Jeff Verdegan wrote:It's hard to say exactly what's happening without seeing your code, but from what you describe, it sounds like you're using a single objInfo object, changing its fields, and re-adding that same object to the Vector over and over.
Or possibly, since srvName is not being replaced, you're creating new objects but reusing the same arrays (which are also objects). In other words the same problem, with the same type of solution, but in a slightly different place.
jm rajan
Greenhorn
Joined: Feb 29, 2012
Posts: 7
posted
0
Thanks for the replies.
I tried ObjInfo oi = new ObjInfo(); in the loop. That was not working.
My code is something like this....
ObjectInfo.java
Working.java
When I iterate the vector, I get the correct information in srvName field, but not for the others.
Yes, it looks like you're reusing the same array objects in every ObjectInfo object. So when you change the contents you're changing them for every instance. You need to create a new array for each new object.
Matthew Brown wrote:Yes, it looks like you're reusing the same array objects in every ObjectInfo object. So when you change the contents you're changing them for every instance. You need to create a new array for each new object.
And note that, as Matthew mentioned in his earlier post, this is exactly the same issue that I was describing, just in a different place.
One thing that's critical to understand when coding in Java, and that tends to be a problem for a lot of beginners, is the difference between a reference and the object that the reference points to, and, in cases like this, what exactly that means in terms of what one passes to methods, returns from methods, or assigns to variables.
One important side-aspect of that is that no object is ever copied--no new object ever created--unless there is code that explicitly does so. Simply passing something to or returning something form a method doesn't do that, nor does assigning to a variable.
jm rajan
Greenhorn
Joined: Feb 29, 2012
Posts: 7
posted
0
Created new instances of the array in the loop instead of the entire object, which solved my problem.
Thanks for the help.