• Post Reply Bookmark Topic Watch Topic
  • New Topic

Vector object being overwritten when adding new element  RSS feed

 
jm rajan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you start with sharing your code? so that it becomes clear, what you are trying yo do?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

[Added code tags - see UseCodeTags]
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Created new instances of the array in the loop instead of the entire object, which solved my problem.
Thanks for the help.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!