Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The code you have post here is for any type of array not only <code>int</code> and for more than 2 arrays to merge. (unlimited)<br> <code>T</code> stand for a generic Type. see <a href="http://%20download.oracle.com/javase/tutorial/java/generics/index.html" rel="nofollow">Generics in Java</a>.</p> <p>You can use only the <code>System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)</code></p> <ul> <li><code>src</code> the source array.</li> <li><code>srcPos</code> starting position in the source array.</li> <li><code>dest</code> the destination array.</li> <li><code>destPos</code> starting position in the destination data.</li> <li><code>length</code> the number of array elements to be copied.</li> </ul> <p>First make an array which has the length of both arrays.</p> <pre><code>public merge(int[] x, int[] y) { int[] merged = new int[x.length + y.length] </code></pre> <p>Than copy with <code>System.arraycopy</code> the first array <code>x</code> into <code>merged</code> at <code>0</code>.<br> And copy with <code>System.arraycopy</code> the 2nd array <code>y</code> into <code>merged</code> at <code>x.length</code></p> <p><strong>But</strong> it doesn't eliminate duplicates. </p> <p>For no duplicates you can use a <a href="http://download.oracle.com/javase/6/docs/api/java/util/Set.html" rel="nofollow">Set</a>. But we have to work with <code>Integer</code> not <code>int</code>. <br> So we have to convert to <code>Integer</code> an back to <code>int</code>. like:</p> <pre><code>HashSet&lt;Integer&gt; set = new HashSet&lt;Integer&gt;(); for( int i : x ) { set.add( Integer.valueOf( i ) ); } for( int i : y ) { set.add( Integer.valueOf( i ) ); } int[] merged = new int[set.size()]; int i = 0; for( Integer value : set ) { merged[i++] = value.intValue(); } </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. This table or related slice is empty.
    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