我需要有以下声明:
private static String[] BASE = new String[] { "a", "b", "c" };
private static String[] EXTENDED = BASE + new String[] { "d", "e", "f" };
Run Code Online (Sandbox Code Playgroud)
第一行声明一个包含三个(或更多)字符串值的字符串数组.第二行应声明一个字符串数组,其中包含BASE中的所有字符串值,然后添加三个(或更多)字符串值.
这可能吗?如果是的话......怎么样?
如果您使用的是Java 8,那么它就是一个简单的单行程序:
给出你的问题的两个数组如下:
private static String[] BASE = new String[] { "a", "b", "c" };
private static String[] EXTENSION = new String[] { "d", "e", "f" };
Run Code Online (Sandbox Code Playgroud)
解决方案是:
String[] EXTENDED = Stream.concat(Arrays.stream(BASE), Arrays.stream(EXTENSION))
.toArray(String[]::new);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |