fork download
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.stream.Stream;
  5.  
  6. class TesteArray {
  7. public static void main(String[] args) {
  8. String[] arrayA = {"A0", "A1", "A2", "A3"};
  9. String[] arrayB = {"B0", "B1", "B2"};
  10.  
  11. // Primeira forma.
  12. String[] arrayC1 = new String[arrayA.length + arrayB.length];
  13. for (int i = 0; i < arrayA.length; i++) {
  14. arrayC1[i] = arrayA[i];
  15. }
  16. for (int i = 0; i < arrayB.length; i++) {
  17. arrayC1[i + arrayA.length] = arrayB[i];
  18. }
  19. System.out.println(Arrays.toString(arrayC1));
  20.  
  21. // Segunda forma.
  22. String[] arrayC2 = new String[arrayA.length + arrayB.length];
  23. System.arraycopy(arrayA, 0, arrayC2, 0, arrayA.length);
  24. System.arraycopy(arrayB, 0, arrayC2, arrayA.length, arrayB.length);
  25. System.out.println(Arrays.toString(arrayC2));
  26.  
  27. // Terceira forma.
  28. List<String> lista = new ArrayList<>();
  29. lista.addAll(Arrays.asList(arrayA));
  30. lista.addAll(Arrays.asList(arrayB));
  31. String[] arrayC3 = lista.toArray(new String[0]);
  32. System.out.println(Arrays.toString(arrayC3));
  33.  
  34. // Quarta forma.
  35. String[] arrayC4 = Stream.concat(Stream.of(arrayA), Stream.of(arrayB)).toArray(String[]::new);
  36. System.out.println(Arrays.toString(arrayC4));
  37. }
  38. }
  39.  
Success #stdin #stdout 0.22s 33716KB
stdin
Standard input is empty
stdout
[A0, A1, A2, A3, B0, B1, B2]
[A0, A1, A2, A3, B0, B1, B2]
[A0, A1, A2, A3, B0, B1, B2]
[A0, A1, A2, A3, B0, B1, B2]