声明两个字符串数组,一个在Java中扩展另一个
我需要有以下声明:声明两个字符串数组,一个在Java中扩展另一个,java,arrays,Java,Arrays,我需要有以下声明: private static String[] BASE = new String[] { "a", "b", "c" }; private static String[] EXTENDED = BASE + new String[] { "d", "e", "f" }; 第一行声明了一个包含三个(或更多)字符串值的字符串数组。 第二行应该声明一个字符串数组,其中包含来自BASE的所有字符串值,然后添加三个(或更多)字符串值 这可能吗?如果是。。。怎么做?不是那样,不。你可
private static String[] BASE = new String[] { "a", "b", "c" };
private static String[] EXTENDED = BASE + new String[] { "d", "e", "f" };
第一行声明了一个包含三个(或更多)字符串值的字符串数组。
第二行应该声明一个字符串数组,其中包含来自BASE的所有字符串值,然后添加三个(或更多)字符串值
这可能吗?如果是。。。怎么做?不是那样,不。你可以使用:
private static String[] EXTENDED = new String[BASE.length + 3];
static {
System.arraycopy(BASE, 0, EXTENDED, 0, BASE.length);
EXTENDED[BASE.length] = "d";
EXTENDED[BASE.length + 1] = "e";
EXTENDED[BASE.length + 2] = "f";
}
或者编写一个方法来连接两个数组,然后使用以下命令调用它:
private static String[] BASE = new String[] { "a", "b", "c" };
private static String[] EXTENDED =
ArrayUtils.concat(BASE, new String[] { "d", "e", "f" });
我不知道在JRE中有这样的方法,但编写或使用streams API并不困难。如果您使用的是Java 8,它是一个简单的单行程序: 考虑到您的问题的两个数组,如下所示:
private static String[] BASE = new String[] { "a", "b", "c" };
private static String[] EXTENSION = new String[] { "d", "e", "f" };
解决办法是:
String[] EXTENDED = Stream.concat(Arrays.stream(BASE), Arrays.stream(EXTENSION))
.toArray(String[]::new);
Apache Commons Lang库中有一个解决方案:
String[]both=ArrayUtils.addAll(firstArray,secondArray) 不依赖Java 8,这里有另一个解决方案:
String[] BASE = new String[] { "a", "b", "c" };
String[] EXT = new String[] { "d", "e", "f" };
String[] CONCAT = Arrays.copyOf (BASE, BASE.length + EXT.length);
System.arraycopy(EXT, 0, CONCAT, BASE.length, EXT.length);
不久前,我创建了一个
joinedaray
类,这可能会有所帮助。如果您不想经常这样做,这有点过分,但是如果您在代码中经常这样做,您可能会发现它很有用
它实际上并没有将数组连接到一个新的数组中-它实际上提供了一个Iterable
,然后可以在连接的数组中进行迭代
public class JoinedArray<T> implements Iterable<T> {
final List<T[]> joined;
@SafeVarargs
public JoinedArray(T[]... arrays) {
joined = Arrays.<T[]>asList(arrays);
}
@Override
public Iterator<T> iterator() {
return new JoinedIterator<>(joined);
}
private class JoinedIterator<T> implements Iterator<T> {
// The iterator across the arrays.
Iterator<T[]> i;
// The array I am working on.
T[] a;
// Where we are in it.
int ai;
// The next T to return.
T next = null;
private JoinedIterator(List<T[]> joined) {
i = joined.iterator();
a = i.hasNext() ? i.next() : null;
ai = 0;
}
@Override
public boolean hasNext() {
while (next == null && a != null) {
// a goes to null at the end of i.
if (a != null) {
// End of a?
if (ai >= a.length) {
// Yes! Next i.
if (i.hasNext()) {
a = i.next();
} else {
// Finished.
a = null;
}
ai = 0;
}
if (a != null) {
if (ai < a.length) {
next = a[ai++];
}
}
}
}
return next != null;
}
@Override
public T next() {
T n = null;
if (hasNext()) {
// Give it to them.
n = next;
next = null;
} else {
// Not there!!
throw new NoSuchElementException();
}
return n;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported.");
}
}
public static void main(String[] args) {
JoinedArray<String> a = new JoinedArray<>(
new String[]{
"Zero",
"One"
},
new String[]{},
new String[]{
"Two",
"Three",
"Four",
"Five"
},
new String[]{
"Six",
"Seven",
"Eight",
"Nine"
});
for (String s : a) {
System.out.println(s);
}
}
}
公共类JoineArray实现了Iterable{
最后加入的名单;
@安全变量
公共联合阵列(T[]…阵列){
joined=Arrays.asList(数组);
}
@凌驾
公共迭代器迭代器(){
返回新的Joineditor(已加入);
}
私有类Joineditor实现迭代器{
//遍历数组的迭代器。
迭代器i;
//我正在处理的阵列。
T[]a;
//我们在那里。
国际人工智能;
//下一个T将返回。
T next=null;
专用Joineditor(已加入列表){
i=joined.iterator();
a=i.hasNext()?i.next():空;
ai=0;
}
@凌驾
公共布尔hasNext(){
while(next==null&&a!=null){
//a在i的末尾变为null。
如果(a!=null){
//世界末日?
如果(ai>=a.length){
//是的!下一个我。
if(i.hasNext()){
a=i.next();
}否则{
//完成了。
a=零;
}
ai=0;
}
如果(a!=null){
如果(ai
//创建2个数组
字符串[]第一个=新字符串[]{“a”、“b”、“c”};
字符串[]秒=新字符串[]{“d”、“e”、“f”};
//创建一个列表
List templast=new ArrayList();
//然后在列表中添加两个数组
addAll(Arrays.asList(第一个));
addAll(Arrays.asList(第二个));
//然后将列表转换为数组
String[]finalStr=templast.toArray(新字符串[templast.size()]);
//就这样
您可能需要将数组.stream(扩展)
替换为stream.of(“d”、“e”、“f”)
。另一种选择:private static String[]EXTENDED=Stream.of(BASE,新字符串[]{“d”,“e”,“f”}).flatMap(Arrays::Stream).toArray(String[]::new)代码>虽然没有concat
@JonSkeet那么短,但你是对的。Java8仍然是相当新的,没有多少人对流API进行过最全面的探索。@Robinjonson:即使知道流API,我也会说它不是非常简单。(C#会稍微简单一些:BASE.Concat(EXTENDED.ToArray()
,但实际上这并没有显示问题中要求的内容,也就是说EXTENDED
将BASE
加上一些其他成员……你可能想编辑你的答案,使之与问题匹配。)@JonSkeet我认为这相当简单。但这是我在重业务逻辑中使用it大约6个月后的看法。尽管如此,我学得很快;)我编辑了我的答案,以便更好地符合他的例子和问题。谢谢你的意见。@Robinjonson:谢谢你的意见。实际上,我在这个项目中一直使用Java7,所以我不能使用Streams API。然而,由于我忘了提到这一点,我将用JonSkeet的评论来感谢您的回答。即使有了Java 8的改进,我仍然经常使用它……到目前为止,它是我最喜欢的通用库。在这种特殊情况下,您发布的代码比Java8单线解决方案更易于阅读,因此如果不考虑性能,我会选择您的解决方案。
// Create 2 Arrays
String[] first = new String[]{"a","b","c"};
String[] second = new String[]{"d","e","f"};
// Create a List
List<String> tempList = new ArrayList<String>();
// Then add both arrays in the List
tempList.addAll(Arrays.asList(first));
tempList.addAll(Arrays.asList(second));
// Then convert the List into array
String[] finalStr = tempList.toArray(new String[tempList.size()]);
// Thats it