如何用java语言创建不定长动态数组
3个回答
展开全部
public class intArray {
private int theSize=0;;
private int a_int[]={};
public intArray(int a[]){
this.a_int=a;
this.theSize=a.length;
}
public intArray(){
this.theSize=0;
}
public int getSize(){
return this.theSize;
}
public int getAt(int nIndex){
return this.a_int[nIndex];
}
public void SetAt(int nIndex , int newElement){
this.a_int[nIndex] =newElement;
}
public void add(int nIndex){
int old[] = this.a_int;
this.a_int= new int[this.theSize+1];
for(int i =0;i<old.length;i++){
a_int[i]= old[i];
}
if(this.theSize>0){
a_int[this.theSize]=nIndex;
}else{
a_int[0]=nIndex;
}
this.theSize++;
}
public static void main(String args[]){
intArray array = new intArray();
array.add(1);
array.add(2);
array.add(3);
array.add(4);
array.add(5);
for(int i = 0;i<array.getSize();i++){
System.out.println(array.getAt(i));
}
}
}
再给个任意类型的
public class myArray <AnyType>{
private int theSize=0;
private AnyType theItem[]=null;
public myArray(AnyType a[]){
this.theItem=a;
this.theSize=a.length;
}
public myArray(){
}
public int getSize(){
return this.theSize;
}
public AnyType get(int nIndex){
return this.theItem[nIndex];
}
public void Set(int nIndex , AnyType newElement){
this.theItem[nIndex] =newElement;
}
public void add(AnyType newVal){
AnyType old[]=this.theItem;
this.theItem= (AnyType[]) new Object[theSize+1];
if(theSize!=0){
for(int i =0;i<old.length;i++){
this.theItem[i]= old[i];
}
}
this.theItem[this.theSize]=newVal;
this.theSize++;
}
public static void main(String args[]){
myArray<String> array = new myArray<String>();
array.add("1");
array.add("2");
array.add("3");
array.add("a");
array.add("b");
array.add("c");
for(int i =0;i<array.theSize;i++){
System.out.println(array.get(i));
}
}
}
private int theSize=0;;
private int a_int[]={};
public intArray(int a[]){
this.a_int=a;
this.theSize=a.length;
}
public intArray(){
this.theSize=0;
}
public int getSize(){
return this.theSize;
}
public int getAt(int nIndex){
return this.a_int[nIndex];
}
public void SetAt(int nIndex , int newElement){
this.a_int[nIndex] =newElement;
}
public void add(int nIndex){
int old[] = this.a_int;
this.a_int= new int[this.theSize+1];
for(int i =0;i<old.length;i++){
a_int[i]= old[i];
}
if(this.theSize>0){
a_int[this.theSize]=nIndex;
}else{
a_int[0]=nIndex;
}
this.theSize++;
}
public static void main(String args[]){
intArray array = new intArray();
array.add(1);
array.add(2);
array.add(3);
array.add(4);
array.add(5);
for(int i = 0;i<array.getSize();i++){
System.out.println(array.getAt(i));
}
}
}
再给个任意类型的
public class myArray <AnyType>{
private int theSize=0;
private AnyType theItem[]=null;
public myArray(AnyType a[]){
this.theItem=a;
this.theSize=a.length;
}
public myArray(){
}
public int getSize(){
return this.theSize;
}
public AnyType get(int nIndex){
return this.theItem[nIndex];
}
public void Set(int nIndex , AnyType newElement){
this.theItem[nIndex] =newElement;
}
public void add(AnyType newVal){
AnyType old[]=this.theItem;
this.theItem= (AnyType[]) new Object[theSize+1];
if(theSize!=0){
for(int i =0;i<old.length;i++){
this.theItem[i]= old[i];
}
}
this.theItem[this.theSize]=newVal;
this.theSize++;
}
public static void main(String args[]){
myArray<String> array = new myArray<String>();
array.add("1");
array.add("2");
array.add("3");
array.add("a");
array.add("b");
array.add("c");
for(int i =0;i<array.theSize;i++){
System.out.println(array.get(i));
}
}
}
展开全部
如果是String型其实你可以先把要放入数组中的的元素放进一个StringBuffer里,用分隔符分割,在需要的时候转化成String在用.split()方法分割放进String[]数组里,这样快,需要的代码也不多,int型的话,可以用同样的方法实现,只是数据类型转化可能会影响速度。数组好像不支持动态变量的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java.util.Arrays
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询