J2ME数组的复制及连接操作

来源:未知 责任编辑:智问网络 发表时间:2013-11-04 19:49 点击:

J2ME数组的复制及连接操作
文章分类:移动开发

public class Arrays {
   /**
    * 构造函数私有,这样可以保证只能通过:类名.静态方法 或 类名.静态方法 来访问内部数据,
    * 而不可以通过创建本类的对象来进行访问
    */
   private Arrays() {
   }
 
   /**
    * 复制一个跟源byte数组一样的byte数组
    * @param rSource 源byte数组
    * @return 跟源byte[]数组一样的byte[]数组
    */
   static public byte[] copy(byte[] rSource) {
      byte[] aResult = new byte[rSource.length];
      System.arraycopy(rSource, 0, aResult, 0, aResult.length);
 
      return aResult;
   }
 
   /**
    * 复制一个跟源int数组一样的int数组
    * @param rSource 源int数组
    * @return 跟源int数组一样的int数组
    */
   static public int[] copy(int[] rSource) {
      int[] aResult = new int[rSource.length];
      System.arraycopy(rSource, 0, aResult, 0, aResult.length);
 
      return aResult;
   }
 
   /**
    * 比较两个byte数组的内容及长度是否相等.
    * @param a1 第一个byte数组
    * @param a2 第二个byte数组
    * @return 相等的话返回true,否则返回false
    */
   static public boolean equals(byte[] a1, byte[] a2) {
      if ( (a1 == null) || (a2 == null)) {
         return a1 == a2;
      }
 
      int nLength = a1.length;
 
      if (nLength != a2.length) {
         return false;
      }
 
      for (int i = 0; i < nLength; i++) {
         if (a1[i] != a2[i]) {
            return false;
         }
      }
 
      return true;
   }
 
   /**
    * 比较两个int数组的内容及长度是否相等.
    * @param a1 第一个int数组
    * @param a2 第二个int数组
    * @return 相等的话返回true,否则返回false
    */
   static public boolean equals(int[] a1, int[] a2) {
      if ( (a1 == null) || (a2 == null)) {
         return a1 == a2;
      }
 
      int nLength = a1.length;
 
      if (nLength != a2.length) {
         return false;
      }
 
      for (int i = 0; i < nLength; i++) {
         if (a1[i] != a2[i]) {
            return false;
         }
      }
 
      return true;
   }
 
   /**
    * 连接两个byte数组,之后返回一个新的连接好的byte数组
    * @param a1
    * @param a2
    * @return 一个新的连接好的byte数组
    */
   static public byte[] join(byte[] a1, byte[] a2) {
      byte[] result = new byte[a1.length + a2.length];
 
      System.arraycopy(a1, 0, result, 0, a1.length);
      System.arraycopy(a2, 0, result, a1.length, a2.length);
 
      return result;
   }
 
   /**
    * 连接两个int数组,之后返回一个新的连接好的int数组
    * @param a1
    * @param a2
    * @return 一个新的连接好的int数组
    */
   static public int[] join(int[] a1, int[] a2) {
      int[] result = new int[a1.length + a2.length];
 
      System.arraycopy(a1, 0, result, 0, a1.length);
      System.arraycopy(a2, 0, result, a1.length, a2.length);
 
      return result;
   }
}

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Java编程语言的八大优点
    • JVM对象生命周期详细介绍
    • Java平台上的CRM系统
    • Java 算数测试小程序
    • Command(命令模式)
    • Java 一个简单的画图程序
    • Java环境 使用Resin在NT环境下配置JSP环境
    • Java 日历的小程序
    • Java 统计代码的小工具
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1