otc
otc

Reputation: 754

Combine Java string arrays into one object

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

Upvotes: 1

Views: 135

Answers (5)

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 :)

Upvotes: 0

steffen
steffen

Reputation: 16948

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

Upvotes: 1

Sinkingpoint
Sinkingpoint

Reputation: 7624

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.

Upvotes: 1

janos
janos

Reputation: 124646

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

Upvotes: 1

Lyubomyr Shaydariv
Lyubomyr Shaydariv

Reputation: 21115

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.

Upvotes: 0

Related Questions