zoukankan      html  css  js  c++  java
  • 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);
        }
  • 相关阅读:
    uva 10900
    uva 11181
    Codeforces Round #256 (Div. 2) Multiplication Table
    UVALive 3977
    LA 4384
    Linear Regression
    Hadoop InputFormat浅析
    codeforces 432D Prefixes and Suffixes
    php学习小记2 类与对象
    php学习小记1
  • 原文地址:https://www.cnblogs.com/foolash/p/11770247.html
Copyright © 2011-2022 走看看