Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No you can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size. When it does you'll have to allocate a new one and copy the data from the old to the new:</p> <pre><code>int oldItems[] = new int[10]; for (int i=0; i&lt;10; i++) { oldItems[i] = i+10; } int newItems[] = new int[20]; System.arraycopy(oldItems, 0, newItems, 0, 10); oldItems = newItems; </code></pre> <p>If you find yourself in this situation, I'd highly recommend using the Java Collections instead. In particular <a href="http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html" rel="noreferrer"><code>ArrayList</code></a> essentially wraps an array and takes care of the logic for growing the array as required:</p> <pre><code>List&lt;xClass&gt; mysclass = new ArrayList&lt;xClass&gt;(); myclass.add(new xClass()); myclass.add(new xClass()); </code></pre> <p>Generally an <code>ArrayList</code> is a preferable solution to an array anyway for several reasons. For one thing, arrays are mutable. If you have a class that does this:</p> <pre><code>class Myclass { private int items[]; public int[] getItems() { return items; } } </code></pre> <p>you've created a problem as a caller can change your private data member, which leads to all sorts of defensive copying. Compare this to the List version:</p> <pre><code>class Myclass { private List&lt;Integer&gt; items; public List&lt;Integer&gt; getItems() { return Collections.unmodifiableList(items); } } </code></pre>
    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.
    3. VO
      singulars
      1. This table or related slice is empty.
    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