View Javadoc

1   package cz.cuni.amis.utils;
2   
3   /**
4    * Merge arrays.
5    * <p><p>
6    * Solution from http://forum.java.sun.com/thread.jspa?threadID=202127&messageID=676603
7    * 
8    * @author ik
9    */
10  public class ArrayMerger {
11      
12      /** Creates a new instance of ArrayMerger */
13      public ArrayMerger() {
14      }
15      
16      /**
17       * Merge multiple arrays.
18       */
19      @SuppressWarnings ("unchecked")
20      public static <T> T[] merge(T[]... arrays) {
21          int count = 0;
22          for (T[] array : arrays) {
23              count += array.length;
24          }
25          // create new array
26          T[] rv = (T[]) new Object[count];// Array.newInstance(arrays[0][0].getClass(),count);
27  
28          int start = 0;
29          for (T[] array : arrays) {
30              System.arraycopy(array,0,rv,start,array.length);
31              start += array.length;
32          }
33          return (T[]) rv;
34      }
35  
36  }