Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <h3><a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/names.html#6.4.5" rel="nofollow noreferrer">JLS 6.4.5 The Members of an Array Type</a></h3> <p>The members of an array type are all of the following:</p> <ul> <li>The <code>public final field length</code>, which contains the number of components of the array (length may be positive or zero).</li> <li>The <code>public</code> method <code>clone</code>, which overrides the method of the same name in class <code>Object</code> and throws no checked exceptions. The return type of the clone method of an array type <code>T[]</code> is <code>T[]</code>.</li> <li>All the members inherited from class <code>Object</code>; the only method of <code>Object</code> that is not inherited is its <code>clone</code> method. </li> </ul> </blockquote> <p>Thus:</p> <pre><code>byte[] original = ...; byte[] copy = original.clone(); </code></pre> <p>Note that for array of reference types, <code>clone()</code> is essentially a <em><a href="https://en.wikipedia.org/wiki/Object_copying#Shallow_copy" rel="nofollow noreferrer">shallow copy</a></em>.</p> <p>Also, Java doesn't have multidimensional arrays; it has array of arrays. Thus, a <code>byte[][]</code> is an <code>Object[]</code>, and is also subject to <em>shallow copy</em>.</p> <h3>See also</h3> <ul> <li><a href="http://en.wikipedia.org/wiki/Object_copy" rel="nofollow noreferrer">Wikipedia/Object copy</a></li> <li><a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html" rel="nofollow noreferrer">Java Nuts and Bolts/Arrays</a></li> </ul> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/200387/deep-cloning-multidimensional-arrays-in-java"> Deep cloning multidimensional arrays in Java… ? </a></li> <li><a href="https://stackoverflow.com/questions/2589741/how-to-effectively-copy-an-array-in-java">How to effectively copy an array in java ?</a></li> <li><a href="https://stackoverflow.com/questions/419858/how-to-deep-copy-an-irregular-2d-array">How to deep copy an irregular 2D array</a></li> <li><a href="https://stackoverflow.com/questions/1564832/how-do-i-do-a-deep-copy-of-a-2d-array-in-java"> How do I do a deep copy of a 2d array in Java? </a></li> </ul> <hr> <h3>Other options</h3> <p>Note that <code>clone()</code> returns a <em>new</em> array object. If you simply want to copy the values from one array to an already existing array, you can use e.g. <a href="http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29" rel="nofollow noreferrer"><code>System.arraycopy</code></a>.</p> <p>There's also <a href="http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/util/Arrays.html#copyOf%28byte%5B%5D,%20int%29" rel="nofollow noreferrer"><code>java.util.Arrays.copyOf</code></a> that allows you to create a copy with a different length (either truncating or padding).</p> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/1697250/difference-between-various-array-copy-methods"> Difference between various Array copy methods </a></li> </ul>
 

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