因工作的需要,在从事 .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;  
}           
    本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处。
 
                    
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号