[原創(chuàng)]用JAVA寫的整形動態(tài)數(shù)組
--sunfruit用java實現(xiàn)了整形數(shù)字的動態(tài)數(shù)組
JDK版本
1.3.1
功能
實現(xiàn)了添加整數(shù)到動態(tài)數(shù)組中,JDK(1.5以下)不提供整形類型的集合,比如ArrayList這樣的集合不允許添加整數(shù),
但是在編程過程中會遇到需要整形的動態(tài)數(shù)組的情況,所以這個類實現(xiàn)了這樣的功能
歡迎大家提意見,交流
代碼如下:
/**
* Title: 整形動態(tài)數(shù)組
* Description: 實現(xiàn)了整形數(shù)字的動態(tài)添加
* Copyright: Copyright (c) 2003
* Company: LingTu
* @author cuijiang
* @version 2.0
*/
public class DynArrayInt {
/**
* 原始數(shù)組
*/
private int[] data_All;
/**
* 計數(shù)器(數(shù)組長度)
*/
private int size_count;
/**
* 構造器,初始長度默認為10
*/
public DynArrayInt() {
this(10);
}
/**
* 構造器,設置數(shù)組的初始長度
*
* @param iniSize int 數(shù)組的初始長度
*/
public DynArrayInt(int iniSize) {
data_All = new int[iniSize];
}
/**
* 添加數(shù)據(jù),調用checkAdd(int i)
* @param i int 一個整形數(shù)字
*/
public void addInt(int i) {
//判斷是否增長
this.checkAdd(size_count + 1);
//賦值
data_All[size_count++] = i;
//添加時數(shù)組長度加一
}
/**
* 添加數(shù)字,判斷是否增長
* @param i int 一個整形數(shù)字
*/
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);
}
}
/**
* 獲取數(shù)據(jù)
* @param i int 索引號
* @return int
*/
public int getInt(int i) {
if (i < 0 || i >= size_count) {
throw new IndexOutOfBoundsException("超出最大或最小索引值,無法取得數(shù)據(jù)");
} else {
return data_All[i];
}
}
/**
* 獲取數(shù)據(jù)轉換成字符串模式
* @param i int 索引號
* @return String
*/
public String getIntToString(int i) {
if (i < 0 || i >= size_count) {
throw new IndexOutOfBoundsException("超出最大或最小索引值,無法取得數(shù)據(jù)");
} else {
return String.valueOf(data_All[i]);
}
}
/**
* 刪除數(shù)據(jù)
* @param j int 一個要刪除的整數(shù)
*/
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); // 復制數(shù)據(jù)
--size_count;
return;
}
}
}
/**
* 刪除數(shù)據(jù)
* @param j int 一個要刪除的索引
*/
public void removeIndex(int j) {
if (j < 0 || j >= size_count) {
throw new IndexOutOfBoundsException("超出最大或最小索引值,無法刪除數(shù)據(jù)");
} else {
System.arraycopy(data_All, j + 1, data_All, j, size_count -j- 1); // 復制數(shù)據(jù)
--size_count;
return;
}
}
/**
* 獲取大小
* @return int 獲得數(shù)組長度
*/
public int getSize() {
return size_count;
}
/**
* 獲取數(shù)組對象
* @return int[] 獲得數(shù)組對象
*/
public int[] getAllInt() {
int[] starData = new int[size_count];
System.arraycopy(data_All, 0, starData, 0, size_count);
return starData;
}
/**
* 獲得數(shù)組對象,String格式
* @return String[] 獲得數(shù)組的對象
*/
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 閱讀(1415) 評論(0) 編輯 收藏 所屬分類: JAVA SE & EE