posts - 197,comments - 38,views - 83万

因工作的需要,在从事 .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;  
    } 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 使用两个 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; 
}

 

1
2
3
4
5
6
7
8
// 使用ArrayList方法
//java 合并两个byte数组 
public static byte[] byteMerger(byte[] bt1, byte[] bt2){ 
    List result = new ArrayList();
    result.addAll(bt1);
result.addAll(bt2);
    return result.toArray(); 
}

 

1
2
3
4
5
6
// 使用 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; 
}          

 

1
2
3
4
5
// 使用ArrayUtils.addAll(Object[], Object[])方法,在包apache-commons中
public static  String[] concat(String[] first, String[] second) { 
  String[] both = (String[]) ArrayUtils.addAll(first, second);
  return result; 
}          

 

posted on   wisdo  阅读(78049)  评论(1编辑  收藏  举报
努力加载评论中...
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示