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...

1
  • Possible duplicate of this and that. You can take a look there for answers. Commented May 13, 2013 at 9:10

4 Answers 4

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]);
}
Sign up to request clarification or add additional context in comments.

4 Comments

+1 - this is part of the Apache Commons Lang Library(commons.apache.org/proper/commons-lang/%5D)
Does Java provide something natively without the need for external libs?
I have included code to do it without using any external libs. check it out.
@user2377039 Another way in my answer if you don't want to use ext lib, and don't want to create a separate util function either.
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. )

Comments

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;
}

Comments

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]);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.