--sunfruit
用java實(shí)現(xiàn)了整形數(shù)字的動(dòng)態(tài)數(shù)組
JDK版本
1.3.1
功能
實(shí)現(xiàn)了添加整數(shù)到動(dòng)態(tài)數(shù)組中,JDK(1.5以下)不提供整形類型的集合,比如ArrayList這樣的集合不允許添加整數(shù),
但是在編程過(guò)程中會(huì)遇到需要整形的動(dòng)態(tài)數(shù)組的情況,所以這個(gè)類實(shí)現(xiàn)了這樣的功能
歡迎大家提意見(jiàn),交流
代碼如下:
/**
* Title: 整形動(dòng)態(tài)數(shù)組
* Description: 實(shí)現(xiàn)了整形數(shù)字的動(dòng)態(tài)添加
* Copyright: Copyright (c) 2003
* Company: LingTu
* @author cuijiang
* @version 2.0
*/
public class DynArrayInt {
/**
* 原始數(shù)組
*/
private int[] data_All;
/**
* 計(jì)數(shù)器(數(shù)組長(zhǎng)度)
*/
private int size_count;
/**
* 構(gòu)造器,初始長(zhǎng)度默認(rèn)為10
*/
public DynArrayInt() {
this(10);
}
/**
* 構(gòu)造器,設(shè)置數(shù)組的初始長(zhǎng)度
*
* @param iniSize int 數(shù)組的初始長(zhǎng)度
*/
public DynArrayInt(int iniSize) {
data_All = new int[iniSize];
}
/**
* 添加數(shù)據(jù),調(diào)用checkAdd(int i)
* @param i int 一個(gè)整形數(shù)字
*/
public void addInt(int i) {
//判斷是否增長(zhǎng)
this.checkAdd(size_count + 1);
//賦值
data_All[size_count++] = i;
//添加時(shí)數(shù)組長(zhǎng)度加一
}
/**
* 添加數(shù)字,判斷是否增長(zhǎng)
* @param i int 一個(gè)整形數(shù)字
*/
private void checkAdd(int i) {
//獲得原來(lái)的大小
int star = data_All.length;
//判斷是否增長(zhǎng)
if (i > star) {
int starData[] = data_All;
//設(shè)定增長(zhǎng)大小
int endall = star * 2;
data_All = new int[endall];
System.arraycopy(starData, 0, data_All, 0, size_count);
}
}
/**
* 獲取數(shù)據(jù)
* @param i int 索引號(hào)
* @return int
*/
public int getInt(int i) {
if (i < 0 || i >= size_count) {
throw new IndexOutOfBoundsException("超出最大或最小索引值,無(wú)法取得數(shù)據(jù)");
} else {
return data_All[i];
}
}
/**
* 獲取數(shù)據(jù)轉(zhuǎn)換成字符串模式
* @param i int 索引號(hào)
* @return String
*/
public String getIntToString(int i) {
if (i < 0 || i >= size_count) {
throw new IndexOutOfBoundsException("超出最大或最小索引值,無(wú)法取得數(shù)據(jù)");
} else {
return String.valueOf(data_All[i]);
}
}
/**
* 刪除數(shù)據(jù)
* @param j int 一個(gè)要?jiǎng)h除的整數(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); // 復(fù)制數(shù)據(jù)
--size_count;
return;
}
}
}
/**
* 刪除數(shù)據(jù)
* @param j int 一個(gè)要?jiǎng)h除的索引
*/
public void removeIndex(int j) {
if (j < 0 || j >= size_count) {
throw new IndexOutOfBoundsException("超出最大或最小索引值,無(wú)法刪除數(shù)據(jù)");
} else {
System.arraycopy(data_All, j + 1, data_All, j, size_count -j- 1); // 復(fù)制數(shù)據(jù)
--size_count;
return;
}
}
/**
* 獲取大小
* @return int 獲得數(shù)組長(zhǎng)度
*/
public int getSize() {
return size_count;
}
/**
* 獲取數(shù)組對(duì)象
* @return int[] 獲得數(shù)組對(duì)象
*/
public int[] getAllInt() {
int[] starData = new int[size_count];
System.arraycopy(data_All, 0, starData, 0, size_count);
return starData;
}
/**
* 獲得數(shù)組對(duì)象,String格式
* @return String[] 獲得數(shù)組的對(duì)象
*/
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;
}
/**
* 刪除全部?jī)?nèi)容
*/
public void removeAll() {
data_All = new int[10];
size_count = 0;
}
}