1

Hi I have these three static objects that I would like to combine programmatically. For example:

private static final String[] DESEASE = new String[] {
    "Alcoholism", "Animal Desease", "Brain Desease", "Demantia", "Movement Disorder"
};

private static final String[] GENE = new String[] {
    "A1CF", "A2LD1", "A2M", "AA06", "AA1"
};

private static final String[] GEO = new String[] {
    "GSE14429", "GSE4226", "GSE8632", "GS9134", "GSE8641"
};

I do not want to iterate. Want to do something of the type:

String[] resultList = DESEASE resultList.addAll(GENE).addAll(GEO);
4
  • What would you like the Strings in resultList to look like? Do you want resultList to be a 15-element array with all the strings, or do you want a 5-element array with the corresponding strings combined in some way? Commented Mar 20, 2014 at 23:30
  • resultList will go as a String Array as parm to another method, but can be converted in between. Commented Mar 20, 2014 at 23:32
  • No Just the 15 elements in a single array Commented Mar 20, 2014 at 23:33
  • List<String> list = new ArrayList<>(); Collections.addAll(list,DESEASE); Collections.addAll(list,GENE); Collections.addAll(list,GEO); Commented Mar 20, 2014 at 23:36

5 Answers 5

1

I don't think there's a really neat way to do that. This will work, without using third-party libraries, though not exactly pretty:

private static final String[] resultList;
static {
    List<String> tmpList = new ArrayList<String>(Arrays.asList(DESEASE));
    tmpList.addAll(Arrays.asList(GENE));
    tmpList.addAll(Arrays.asList(GEO));
    resultList = tmpList.toArray(new String[tmpList.size()]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could try using Collection, which offers an almost literal version of the example you give:

List<String> data = new ArrayList<String>();
data.addAll(Arrays.asList(DESEASE));
data.addAll(Arrays.asList(GENE));
data.addAll(Arrays.asList(GEO));
String[] resultList = data.toArray(new String[data.size()]);

Which essentially creates a new Collection and adds everything to it before converting it back to an array.

Or you could do it using new arrays, and the System.arraycopy method:

String[] resultList = new String[DESEASE.length + GENE.length + GEO.length];
System.arraycopy(DESEASE, 0, resultList, 0, DESEASE.length);
System.arraycopy(GENE, 0, resultList, DESEASE.length, GENE.length);
System.arraycopy(GEO, 0, resultList, DESEASE.length + GENE.length, GEO.length);

Which creates a new array and copies each sub array into the required slots.

I haven't actually tested which is faster but System.arraycopy out-sources to a native method and doesn't have as much object creation so I would wager at that one.

Comments

1

Here are two solutions (Collections and System.arraycopy()):

Collections:

    List<String> allAsList = new ArrayList<>();
    allAsList.addAll(Arrays.asList(DESEASE));
    allAsList.addAll(Arrays.asList(GENE));
    allAsList.addAll(Arrays.asList(GEO));
    System.out.println(allAsList);

ArrayCopy:

    String[] allAsArray = new String[DESEASE.length + GENE.length + GEO.length];
    System.arraycopy(DESEASE, 0, allAsArray, 0, DESEASE.length);
    System.arraycopy(GENE, 0, allAsArray, DESEASE.length, GENE.length);
    System.arraycopy(GEO, 0, allAsArray, DESEASE.length + GENE.length, GEO.length);
    System.out.println(Arrays.asList(allAsArray));

Comments

0

You can use ObjectArrays.concat(T[],T[],Class<T>) from Google Guava, and then use nested calls:

import static com.google.common.collect.ObjectArrays.concat;
...
final String [] resultList = concat(DESEASE, concat(GENE, GEO, String.class), String.class);

However, lists are more flexible.

Comments

0

How about this way :

public String[] combineArray (String[] ... strings) {
    List<String> tmpList = new ArrayList<String>();
    for (int i = 0; i < strings.length; i++)
        tmpList.addAll(Arrays.asList(strings[i]));
    return tmpList.toArray(new String[tmpList.size()]);
}

And you can pass as many arguments as you want :)

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.