我有一个带有 Type 的数组B[]
。我有一个构造函数A(B b)
。
我想A[]
使用它们创建一个带有 Type 的数组。
我可以使用 for 语句来做到这一点,但我想知道是否有更方便(更短)的方式来做到这一点。(语言语法、静态方法或其他)
public class A {
public A(B b) {
A.a = B.a;
A.b = B.b * B.c;
...
}
}
public class B {
...
}
public class C {
public static B[] getBs() {
B[] bs;
...
return bs;
}
}
void process() {
// From here
B[] bs = C.getBs();
A[] as = new A[bs.length];
for (int i=0; i<bs.length; i++) {
as[i] = new A(bs[i]);
}
// To here
}