double 型の 2 つの配列を 1 つの配列に結合する 1 行のコードのような簡単な方法はありますか?
そうでない場合、それを行う最も簡単な方法は何ですか?
ありがとう
あなたはおそらくSystem.arrayCopy()メソッドを探しています
Apace commons を自由に使用できる場合は、これが解決策です。
または、次のように書くこともできます。
public static double[] unite(double[]... arrays)
{
int length = 0;
for(double[] array: arrays)
length += array.length;
double[] united = new double[length];
int pos = 0;
for(double[] array: arrays) {
System.arraycopy(array, 0, united, pos, array.length);
pos += array.length;
}
return united;
}
public static void main(String... args) {
double[] d1 = {0.1, 0.2};
double[] d2 = {0.3, 0.4, 0.5};
double[] d3 = {0.6, 0.7, 0.8, 0.9};
double[] d4 = {};
double[] d5 = {1.0};
double[] united = unite(d1, d2, d3, d4, d5);
System.out.println(Arrays.toString(united));
}
この種のものにはGoogle Guavaライブラリをお勧めできません