简体   繁体   中英

Initialize new array out of 2 known arrays

how do i initialize a new array out of known arrays?

eg

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM