Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Short answer:</strong> </p> <p>change</p> <pre><code>this.visited = onetimevisit; </code></pre> <p>to</p> <pre><code>this.visited = new ArrayList (onetimevisit); </code></pre> <p><strong>Longer answer:</strong></p> <p><code>ArrayLists</code> are not necessarily <code>static</code>. I think you're incorrectly inferring that the <code>ArrayList</code> must somehow have been set to static from the fact that there is only one copy of the <code>ArrayList</code> 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 <code>ArrayList</code>, for example), you're passing a <em>reference</em> 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 <code>this.visited</code> and <code>onetimevisit</code> are references that come to point to the same object in memory.</p> <p>On the other hand, <code>ArrayList</code> 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 <code>ArrayList</code>, which seems to be what you want. It is worth noting that an <code>ArrayList</code> 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 <code>ArrayList</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload