- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15773
|
- [轉貼]Java String array out of existing String array
- Java String array out of existing String array
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...
--------------------------------------------------------------------------------
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]);
}
--------------------------------------------------------------------------------
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. )
--------------------------------------------------------------------------------
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;
}
--------------------------------------------------------------------------------
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]);
}
原文出處:Java String array out of existing String array - Stack Overflow 冷日補充:
冷日用的方法就是『system.arraycopy』!
簡單好用又不需要額外的 Lib!
|