how do i initialize a new array out of known arrays?
e.g.
char[] word1 = new char[]{'H', 'e', 'l', 'l', 'o'};
char[] word2 = new char[]{'W', 'o', 'r', 'l', 'd'};
char[] newArray;
so how do i initialize newArray out of word1 and word2?
thanks.
how do i initialize a new array out of known arrays?
e.g.
char[] word1 = new char[]{'H', 'e', 'l', 'l', 'o'};
char[] word2 = new char[]{'W', 'o', 'r', 'l', 'd'};
char[] newArray;
so how do i initialize newArray out of word1 and word2?
thanks.
Create the new array and copy the values using System.arraycopy()
.
char[] word1 = new char[]{'H', 'e', 'l', 'l', 'o'};
char[] word2 = new char[]{'W', 'o', 'r', 'l', 'd'};
char[] newArray = new char[word1.length + word2.length];
System.arraycopy(word1, 0, newArray, 0, word1.length);
System.arraycopy(word2, 0, newArray, word1.length, word2.length);
Use this code to initialize newArray:
char[] newArray = ArrayUtils.addAll(word1, word2);
This should do it.