因工作的需要,在从事 .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; } |
本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步