これから変換するにはどうすればよいですか:
ArrayList<int[]>
-に-
int[]
?
例
private ArrayList<int[]> example = new ArrayList<int[]>();
に
private int[] example;
例えばArrayList({1,2,3},{2,3,4}) to {1,2,3,2,3,4}
この問題の (少し) 厄介な部分は、開始する前に、出力配列がどれだけ大きくなければならないかを調べなければならないことです。したがって、解決策は次のとおりです。
これをコーディングするつもりはありません。自分でコーディングできる必要があります。そうでない場合は、できるようになる必要があります...自分でやろうとすることによって。
入力と出力の型が異なる場合は、サード パーティのライブラリを使用したより適切なソリューションがあった可能性があります。しかし、あなたが使用しているという事実は、あなたint[]
を助けるために既存のライブラリを見つける可能性を低くします.
これに関するクイッククックブック:各配列の要素数を数え、すべての要素を保持する配列を作成し、要素をコピーします:)
import java.util.ArrayList;
// comentarios em pt-br
public class SeuQueVcConsegue {
public static void main(String[] args) {
ArrayList<int[]> meusNumerosDaSorte = new ArrayList<int[]>();
meusNumerosDaSorte.add(new int[]{1,2,3});
meusNumerosDaSorte.add(new int[]{4,5,6});
// conta os elementos
int contaTodosOsElementos = 0;
for( int[] foo : meusNumerosDaSorte){
contaTodosOsElementos += foo.length;
}
// transfere os elementos
int[] destinoFinal = new int[contaTodosOsElementos];
int ponteiro = 0;
for( int[] foo : meusNumerosDaSorte){
for( int n : foo){
destinoFinal[ponteiro] = n;
ponteiro ++;
}
}
// confere se esta correto :)
for(int n : destinoFinal){
System.out.println(n);
}
}
}