sunfruit[請訪問http://www.fruitres.cn]

          --我相信JAVA能走得更遠 QQ:316228067

          [原創]用JAVA寫的整形動態數組

              --sunfruit

              用java實現了整形數字的動態數組


          JDK版本
                  1.3.1
              功能
                  實現了添加整數到動態數組中,JDK(1.5以下)不提供整形類型的集合,比如ArrayList這樣的集合不允許添加整數,
                  但是在編程過程中會遇到需要整形的動態數組的情況,所以這個類實現了這樣的功能
                 
              歡迎大家提意見,交流
             
              代碼如下:
          /**
           * Title: 整形動態數組
           * Description: 實現了整形數字的動態添加
           * Copyright: Copyright (c) 2003
           * Company: LingTu
           * @author cuijiang
           * @version 2.0
           */
          public class DynArrayInt {
           /**
            * 原始數組
            */
           private int[] data_All;

           /**
            * 計數器(數組長度)
            */
           private int size_count;

           /**
            * 構造器,初始長度默認為10
            */
           public DynArrayInt() {
            this(10);
           }

           /**
            * 構造器,設置數組的初始長度
            *
            * @param iniSize  int 數組的初始長度
            */
           public DynArrayInt(int iniSize) {
            data_All = new int[iniSize];
           }

           /**
            * 添加數據,調用checkAdd(int i)
            * @param i   int 一個整形數字
            */
           public void addInt(int i) {
            //判斷是否增長
            this.checkAdd(size_count + 1);
            //賦值
            data_All[size_count++] = i;
            //添加時數組長度加一
           }

           /**
            * 添加數字,判斷是否增長
            * @param i   int 一個整形數字
            */
           private void checkAdd(int i) {
            //獲得原來的大小
            int star = data_All.length;
            //判斷是否增長
            if (i > star) {
             int starData[] = data_All;
             //設定增長大小
             int endall = star * 2;
             data_All = new int[endall];
             System.arraycopy(starData, 0, data_All, 0, size_count);
            }
           }

           /**
            * 獲取數據
            * @param i    int 索引號
            * @return int
            */
           public int getInt(int i) {

            if (i < 0 || i >= size_count) {
             throw new IndexOutOfBoundsException("超出最大或最小索引值,無法取得數據");
            } else {
             return data_All[i];
            }
           }

           /**
            * 獲取數據轉換成字符串模式
            * @param i  int 索引號
            * @return String
            */
           public String getIntToString(int i) {

            if (i < 0 || i >= size_count) {
             throw new IndexOutOfBoundsException("超出最大或最小索引值,無法取得數據");
            } else {
             return String.valueOf(data_All[i]);
            }
           }

           /**
            * 刪除數據
            * @param j int 一個要刪除的整數        
            */
           public void remove(int j) {
            for (int i = 0; i < size_count; i++) {
             if (data_All[i] == j) {
              System.arraycopy(data_All, i+1, data_All, i, size_count-i-1); // 復制數據
              --size_count;
              return;
             }
            }
           }

           /**
            * 刪除數據
            * @param j int 一個要刪除的索引        
            */
           public void removeIndex(int j) {
            if (j < 0 || j >= size_count) {
             throw new IndexOutOfBoundsException("超出最大或最小索引值,無法刪除數據");
            } else {
             System.arraycopy(data_All, j + 1, data_All, j, size_count -j- 1); // 復制數據
             --size_count;
             return;
            }
           }

           /**
            * 獲取大小
            * @return int 獲得數組長度
            */
           public int getSize() {
            return size_count;
           }

           /**
            * 獲取數組對象
            * @return int[] 獲得數組對象
            */
           public int[] getAllInt() {
            int[] starData = new int[size_count];
            System.arraycopy(data_All, 0, starData, 0, size_count);
            return starData;
           }

           /**
            * 獲得數組對象,String格式
            * @return String[] 獲得數組的對象
            */
           public String[] getAllIntToString() {
            int[] tempint = getAllInt();
            String[] starData = new String[tempint.length];
            for (int i = 0; i < starData.length; i++) {
             starData[i] = String.valueOf(tempint[i]);
            }
            return starData;
           }

           /**
            * 刪除全部內容
            */
           public void removeAll() {
            data_All = new int[10];
            size_count = 0;
           }
          }

          posted on 2006-02-19 17:33 sunfruit 閱讀(1414) 評論(0)  編輯  收藏 所屬分類: JAVA SE & EE

          主站蜘蛛池模板: 岫岩| 镇康县| 屯昌县| 双江| 酉阳| 潜江市| 银川市| 彩票| 五原县| 武冈市| 资阳市| 时尚| 兴城市| 方山县| 康保县| 安康市| 泽普县| 高要市| 天门市| 满洲里市| 滁州市| 枣庄市| 门头沟区| 泊头市| 阜宁县| 图片| 永州市| 嘉义市| 郓城县| 静宁县| 天门市| 台江县| 松江区| 兖州市| 福海县| 尼勒克县| 清镇市| 东城区| 连江县| 万载县| 华安县|