1

I have a Java String array:

public static final String[] FIELDS_NAMES = { "Albert", "Berta", "Carl" };
public static final String[] FIELDS_NUMBERS = { "123", "456", "789" };

and would like to create a third constant out of the one I already have. Currently I do it by repeating everything:

public static final String[] FIELDS_ALL = { 
    "Albert", "Berta", "Carl", "123", "456", "789"
};

But what I really want is this:

public static final String[] FIELDS_ALL = {FIELDS_NAMES, FIELDS_NUMBERS};

Any idea how to do that in Java? Obviously I do not want to run any loops to shuffle things around...

RalfB
  • 563
  • 1
  • 7
  • 22
  • Possible duplicate of [this](http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java) and [that](http://stackoverflow.com/questions/4697255/combine-two-integer-arrays). You can take a look there for answers. – gkalpak May 13 '13 at 09:10

4 Answers4

2

One way to concatenate string arrays:

String[] FIELDS_ALL = ArrayUtils.addAll(FIELDS_NAMES, FIELDS_NAMES);

EDIT Note: as @Malachi has mentioned, This uses Apache Commons Lang Library

EDIT Without using any external libs:

you can use this generic method to do so:

String[] join(String[]... arrays) 
{
  // calculate size of target array
  int size = 0;
  for (String[] array : arrays) 
  {
    size += array.length;
  }

  // create list of appropriate size
  java.util.List list = new java.util.ArrayList(size);

  // add arrays
  for (String[] array : arrays) 
  {
    list.addAll(java.util.Arrays.asList(array));
  }

  // create and return final array
 return list.toArray(new String[size]);
}
codeMan
  • 5,730
  • 3
  • 27
  • 51
1

Although using Apache Commons Lang as suggested by other answer is the easiest, there are some way using Java built-in syntax which kind of does the work, without creating separate utility method.

public static final String[] FIELDS_ALL =
    new ArrayList<String>() {{
            addAll(Arrays.asList(FIELDS_NAMES));
            addAll(Arrays.asList(FIELDS_NUMBERS));
        }}.toArray(new String[0]);

(In case the syntax looks strange to other people: I am creating a anonymous class, which is a child class of ArrayList, and making use of initializer to call addAll to populate the content of the ArrayList. )

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
0

Use system.arraycopy:

String[] concatArray(String[] A, String[] B) {
   int aLen = A.length;
   int bLen = B.length;
   int cLen = aLen+bLen;
   String[] C = new T[cLen];
   System.arraycopy(A, 0, C, 0, aLen);
   System.arraycopy(B, 0, C, aLen, bLen);
   return C;
}
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
0

Can Use:

String[] FIELDS_NAMES = { "Albert", "Berta", "Carl" };
        String[] FIELDS_NUMBERS = { "123", "456", "789" };

        String[] FIELDS_ALL = new String[FIELDS_NAMES.length + FIELDS_NUMBERS.length] ;
        System.arraycopy(FIELDS_NAMES, 0, FIELDS_ALL, 0, FIELDS_NAMES.length);
        System.arraycopy(FIELDS_NUMBERS, 0, FIELDS_ALL, FIELDS_NAMES.length, FIELDS_NUMBERS.length);
        for (int j = 0; j < FIELDS_ALL.length; j++) {
            System.out.println(FIELDS_ALL[j]);
        }