1

I've searched but I did not find a good answer.

I've used an ArrayList object.I created an instance object, (example object X), I used that ArrayList as a parameter on constructor object X, but everytime I created an instance of object X, the ArrayList included the old values, didn't create a new ArrayList.

I need to use add method like arraylist. This is the code:

public DataPacket(int hop, int TTL, ArrayList onetimevisit){
        this.hop = hop;
        this.TTL = TTL;
        this.visited = onetimevisit;
 }

in other looping process, DataPacket will meet object NodeRandom:

public NodeRandom(int id){
        this.id = id;
}

then DataPacket will add the id of NodeRandom.

Is there an Object in Collection isn't static?

afni
  • 75
  • 2
  • 12

3 Answers3

4

I'll take a guess that your issue has to do with an incorrect assumption about how java passes objects as parameters in method calls. Check out this answer: Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
Orolo
  • 119
  • 7
1

If you want a new ArrayList() you have to create one, it won't do it automagically.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Short answer:

change

this.visited = onetimevisit;

to

this.visited = new ArrayList (onetimevisit);

Longer answer:

ArrayLists are not necessarily static. I think you're incorrectly inferring that the ArrayList must somehow have been set to static from the fact that there is only one copy of the ArrayList when you pass it in the way you've passed it. The thing to understand is that when you pass an object in Java (an ArrayList, for example), you're passing a reference to the object. A reference is something akin to a C-style pointer with the distinction that pointer arithmetic and such is not allowed. When you call a method and pass an object, the called method just gets a copy of the reference and not a copy of the object. Likewise, when you use the = operator to assign one object to another, you're only assigning the references to equal each other, and there is still only one copy of the object. In your code, both this.visited and onetimevisit are references that come to point to the same object in memory.

On the other hand, ArrayList has something that is somewhat akin to a copy constructor. This constructor, called in my sample code above, creates a shallow copy of the given ArrayList, which seems to be what you want. It is worth noting that an ArrayList does not copy the objects added to it (it stores references to them), so perhaps what you really need is to create copies of the objects as well. This would be done by calling their copy constructors (if they allow copying by providing such a constructor) before inserting them into the ArrayList.

Gravity
  • 2,696
  • 1
  • 20
  • 29
  • 1
    @afni: The objects _in_ the list are still shared, even after copy! If you have a list of users, and you pass a copy (using new ArrayList(oldUsers)), and you change the name of the user, all lists will see the updated name. – extraneon Jul 23 '11 at 17:27
  • @extraneon- yes. I've tried that, but I know the concept now and my mistake. – afni Jul 24 '11 at 19:13