0

I know, We can copy or append elements of array. But, I have around 100 elements in an array. Is there any other way available so I can append array to first array.

Consider I have this two arrays.

 String name1[]={"abc", "def", "ghi"};
 String name2[]={"jkl", "mno", "pqr"};

I want to append name2 array at the end of name1.

Please help me.

would be grateful for help.

1

3 Answers 3

3

Guava provides Arrays.concat(T[], T[], Class<T>).

The reason for the Class parameter, FYI, is because generic arrays have a distinct tendency to get upcast. If you did Arrays.concat(Integer[], Long[]), did you want an Object[] back? Or a Number[]? Guava makes you specify so there's no ambiguity...and because all of the alternatives can lead to unpredictable ClassCastExceptions at runtime.

(Disclosure: I contribute to Guava.)

Sign up to request clarification or add additional context in comments.

3 Comments

Good point about upcasting... The implementation I wrote as an answer here will break only at runtime if you provide an Integer and a Long array...
Yes, that's correct; it will break. (Conversely, Apache's ArrayUtils will just give you an Object[], which you might be able to cast with an unsafe warning, but that is unsafe: it might lead to unpredictable ClassCastExceptions later.)
Added a warning in my answer. Thanks for clarifying.
2

You will have to create a new array.

An easy implementation using generics and not using any external library:

public static void main(String[] args) {
    String [] a1 = { "a", "b" };
    String [] a2 = { "c", "d", "e", "f" };

    System.out.println(Arrays.toString(append(a1, a2)));
}

public <K> K[] append(K[] a1, K[] a2) {
    K[] a1a2 = Arrays.copyOf(a1, a1.length + a2.length);
    for (int i = a1.length; i < a1a2.length; i++) {
        a1a2[i] = a2[i - a1.length];
    }
    return a1a2;
}

OBS: As Louis Wasserman comments in his answer, Java will upcast the arrays, which can be a problem. For example, if you provide a Long[] and an Integer[] to the append method above, it will compile but you will get a java.lang.ArrayStoreException at run-time!!

Comments

1

You will have to create a new array. Because the length of arrays is fixed.

 String[] list = new String[name1.length+name2.length]

You could loop around the two arrays and add each element to the new array

You could also use apache commons lang library

 String[] both = ArrayUtils.addAll(first, second);

3 Comments

I just checked, but ArrayUtils returns Object[] not String array...Please correct me, If I am going wrong.
I might be wrong. But arrays can be casted. (Just be careful with this)
Be very careful with the casted array -- it won't behave quite like a normal String[].

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.