zoukankan      html  css  js  c++  java
  • java 中byte[] 数组的合并

    因工作的需要,在从事 .Net 的开发中接触到了 Java, 虽然在大学的时候学过一段Java 编程,但并没有在实际的工作中使用过, Java 和 .Net的C#语法很相似,都是面向对象的,感觉在语法上只有些细微的差异,这里主要介绍以下,将两个数组合并成的操作,废话不多说,直接上代码:

        //System.arraycopy()方法
        public static byte[] byteMerger(byte[] bt1, byte[] bt2){  
            byte[] bt3 = new byte[bt1.length+bt2.length];  
            System.arraycopy(bt1, 0, bt3, 0, bt1.length);  
            System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);  
            return bt3;  
        } 
        
        // 使用两个 for 语句
        //java 合并两个byte数组  
        public static byte[] byteMerger(byte[] bt1, byte[] bt2){  
            byte[] bt3 = new byte[bt1.length+bt2.length];  
    	int i=0;
            for(byte bt: bt1){
             bt3[i]=bt;
    	 i++;
    	}
            
    	for(byte bt: bt2){
    	  bt3[i]=bt;
    	  i++;
    	}
            return bt3;  
        } 
    
        // 使用ArrayList方法
        //java 合并两个byte数组  
        public static byte[] byteMerger(byte[] bt1, byte[] bt2){  
            List result = new ArrayList();
            result.addAll(bt1);
    	result.addAll(bt2);
            return result.toArray();  
        } 
    
    // 使用 Arrays.copyOf() 方法,但要在 java6++版本中
    public static  String[] concat(String[] first, String[] second) {  
      String[] result = Arrays.copyOf(first, first.length + second.length);  
      System.arraycopy(second, 0, result, first.length, second.length);  
      return result;  
    }           
    
        
    
    // 使用ArrayUtils.addAll(Object[], Object[])方法,在包apache-commons中
    public static  String[] concat(String[] first, String[] second) {  
      String[] both = (String[]) ArrayUtils.addAll(first, second); 
      return result;  
    }           
    
  • 相关阅读:
    H5页面富文本展示图片之间存在空隙
    如何关闭eslint语法校验
    K8S service详解
    vim添加或删除多行注释
    K8S 弹性伸缩
    Load高问题排查
    Dockerfile CMD和ENTRYPOINT
    nginx配置
    sed命令
    Pod排错指南
  • 原文地址:https://www.cnblogs.com/wisdo/p/5074434.html
Copyright © 2011-2022 走看看