java合并数组的几种方法,stream流合并数组、打印二维数组?

package cc.ash;

import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Array;
import java.util.Arrays;

public class ArrayConcat {


    public static void main(String[] args) {

        int[] a = {1,2,3};
        int[] b = {4,5,6};
        concatArray(a,b);
    }
    public static void concatArray(int [] a, int [] b) {

        //org.apache.commons.lang3.ArrayUtils中方法
        int[] all = ArrayUtils.addAll(a, b);

        //通过Array的newInstance生成一个合并长度的数组,再通过System中的arraycopy()方法copy
        Object newInstance = Array.newInstance(int.class, a.length + b.length);
        System.arraycopy(a, 0, newInstance, 0, a.length);
        System.arraycopy(b, 0, newInstance, a.length, b.length);


        //通过Arrays中copyOf方法将某一个作为基础,扩展所需要的长度
        int[] copyOf = Arrays.copyOf(b, a.length + b.length);
        //再通过System中的arraycopy()方法copy
        System.arraycopy(a, 0, copyOf, b.length, a.length);

    

    }
}

二、方法总结

参考链接:

https://www.cnblogs.com/jpfss/p/9181443.html

https://www.jb51.net/article/160480.htm

三、stream合并数组

        String [] a = {"a1", "a2"};
        String [] b = {"b1", "b2", "b3"};
        String[] strings = Stream.concat(Stream.of(a), Stream.of(b)).peek(System.out::println).toArray(String[]::new); //测试输出

四、stream打印二维数组

public static void main(String[] args) {

        int [][] ary2 = {{1,2}, {3,4}};
        Arrays.stream(ary2)
//                .flatMap(Stream::of)
                .map(Arrays::toString)
//                .peek(log::info).count();
                .forEach(log::info);
//                .forEach(System.out::println);
    }