我们都知道,由于Array(数组)通常意义上讲只是一个单纯的线性序列,又基于Native,凭此它的效率历来便号称Java中最高。所以通常我们也都承认Java中效率最高的存储方式就是使用数组。但是,由于数组初始化后大小固定,索引不能超出下标,缺少灵活的扩展功能等原因,使得很多人放弃了数组的使用, 转而使用Collection,List,Map,Set等接口处理集合操作。
诚然在Java中使用集合类可以极大的简化我们的代码编写量。但是,有时明明只是缓存一些线性数据,还偏偏有人要使用HashMap保存,系统为此付出了不必要的内存损耗。如果是通常的程序还没有什么,当应用在高并发或者加载高内存消耗对象时(如Image图像)无意义的频繁使用集合类将极易引发OutOfMemoryException。
我们很清楚,以List接口实现的集合类中,ArrayList内部运算是基于Array的,所以他继承了Array的优势,非常适合索引取值和存储线性数据(Vector虽然也是基于Array的,但毁在大量的synchronized上……所以很多情况下等于废了……)。但它不适合插入数据和删除数据,因为每插入或删除一次就会产生一次大量数组内容Copy的操作。而LinkedList正好与ArrayList相反,它比较适合与插入删除操作,不适合于索引取值,因为它不可以像数组一样根据索引值直接就可以定位元素的地址,而需要从头至尾一个一个的来数位置。
其实这也是当然的,但凡接触过数据结构的都会知道,任何存储结构都是有其局限性的,没有也不可能有所有方面都完美的存储方式。我们所能做的,只是尽可能使用特定范围内最有效的解决方案,而对此什么解决方法最有效呢?
一般情况下,考虑到效率与类型检查等问题,应该尽可能使用数组,所以我个人比较推荐的方式就是根据需要基于数组定制集合类。
说到这里可能很多人以开发周期及稳定、通用性为借口而直接使用JDK或第三方集合类(或者COPY代码|||)。其实就我个人认为,这有些因噎废食了,确实有时存储对象比较复杂,自己的集合类性能无法保障。但在大多数项目中,这种情况并不存在,我们完全有能力根据需求构造集合以避免不必要的资源占用及进行相应优化。而某些人往往只是见Hibernate等框架返回个List便有样学样的自己也List和Map到底,干脆忘了Arrays.asList等方法是为什么而存在的。
本来JDK提供给我们Arrays.asList方法和Collection.toArray方法就是为了集合类和数组优势互补之用,以此成为数组和Collection等集合类间的桥梁,只要有这两种方法Array和Collection就可以相互转换。那么,我们有什么理由枉费可以利用的资源而不用呢?
事实上,我们只要基本掌握Arrays类和Reflect这两个有力的武器,操作数组处理持久对象根本就是张飞吃豆芽——小菜一碟。
下面,我抛砖引玉的写些代码举例:
TestBean.java(测试用实体类)
packageorg.loon.framework.db.test.util;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/***//**
*<p>
*Title:LoonFramework
*</p>
*<p>
*Description:
*</p>
*<p>
*Copyright:Copyright(c)2007
*</p>
*<p>
*Company:LoonFramework
*</p>
*
*@authorchenpeng
*@email:ceponline@yahoo.com.cn
*@version0.1
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
publicclassTestBean...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Stringname;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
intid;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicintgetId()...{
returnid;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicvoidsetId(intid)...{
this.id=id;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicStringgetName()...{
returnname;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicvoidsetName(Stringname)...{
this.name=name;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
ArrayUtil.java(用于Array的增、删、改、查等操作)
packageorg.loon.framework.db.test.util;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
importjava.io.Serializable;
importjava.lang.reflect.Array;
importjava.util.Arrays;
importjava.util.Collection;
importjava.util.List;
importjava.util.Random;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/***//**
*<p>
*Title:LoonFramework
*</p>
*<p>
*Description:ArrayUtil,数组操作工具类
*</p>
*<p>
*Copyright:Copyright(c)2007
*</p>
*<p>
*Company:LoonFramework
*</p>
*
*@authorchenpeng
*@email:ceponline@yahoo.com.cn
*@version0.1
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
publicclassArrayUtilimplementsSerializable...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*
*/
privatestaticfinallongserialVersionUID=8057374625909011982L;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//缓存数组对象
privateObjectobjArray;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//数组长度
privateintsize=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//缓存数组对象实体类型
privateStringobjType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
finalstaticprivateRandomrand=newRandom();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
privatestaticArrayUtilinstance=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*直接注入Collection
*
*@paramcollection
*@return
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicstaticArrayUtilgetInstance(Collectioncollection)...{
returngetInstance(collection.toArray());
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*直接注入对象数组
*
*@paramarray
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicstaticArrayUtilgetInstance(Objectarray)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(instance==null)...{
instance=newArrayUtil(array);
}
returninstance;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*注入类产生指定大小对象数组
*
*@paramclazz
*@parammaxSize
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicstaticArrayUtilgetInstance(Classclazz,intmaxSize)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(instance==null)...{
instance=newArrayUtil(clazz,maxSize);
}
returninstance;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
privateArrayUtil()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*注入对象数组产生指定大小对象数组
*
*@paramclazz
*@parammaxSize
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
privateArrayUtil(Classclazz,intmaxSize)...{
//转为指定大小对象数组
Objectarray=(Object[])Array.newInstance(clazz,
maxSize);
//初始化
init(array);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*直接注入对象数组
*
*@paramarray
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
privateArrayUtil(Objectarray)...{
init(array);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
privatevoidinit(Objectarray)...{
//检查是否数组对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(!(arrayinstanceofObject[]))...{
thrownewIndexOutOfBoundsException("Notobjectarrays!");
}
//缓存数组对象
objArray=array;
//缓存实体类型
objType=array.getClass().getComponentType().getSimpleName();
//缓存数组长度
size=Array.getLength(objArray);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*返回指定对象索引位置
*
*@paramobj
*@return
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicintget(Objectobj)...{
//检查是否合法对象
checkObject(obj);
Object[]object=(Object[])objArray;
for(inti=0;i<size;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(object[i]==obj)...{
returni;
}
return-1;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*返回指定索引位置对象
*
*@paramindex
*@return
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicObjectget(intindex)...{
checkIndex(index);
returngetObjectArray()[index];
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*加载对象在指定位置
*
*@paramobj
*@paramindex
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicvoidadd(Objectobj,intindex)...{
//检查索引是否越界
checkIndex(index);
//检查是否合法对象
checkObject(obj);
Object[]objTemp=(Object[])objArray;
objTemp[index]=obj;
//copy临时数组到objArray
System.arraycopy(objTemp,0,objArray,0,objTemp.length);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*加载对象
*
*@paramobj
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicvoidadd(Objectobj)...{
//类型检查
checkObject(obj);
//累加
next();
//临时缓存旧数组数组
Object[]objTemp=newObject[size];
//加载对象
objTemp[size-1]=obj;
//copy
System.arraycopy(objArray,0,objTemp,0,Array.getLength(objArray));
//转换
objArray=objTemp;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*删除指定索引位置数组数据
*
*@paramindex
*@return
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicObjectremove(intindex)...{
//检查索引是否越界
checkIndex(index);
Object[]objTemp=(Object[])objArray;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//重新构建objArray
intj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if((j=size-index-1)>0)...{
System.arraycopy(objTemp,index+1,objTemp,index,j);
}
//减少size
back();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
returnobjTemp[index];
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicbooleancontains(Objectobj)...{
Object[]objTemp=(Object[])objArray;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for(inti=0;i<size;i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(hash(objTemp[i])==hash(obj))...{
returntrue;
}
}
returnfalse;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicObject[]sub(intstartIndex,intendIndex)...{
//验证索引范围
checkIndex(startIndex);
checkIndex(endIndex);
intover=endIndex-startIndex;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(over<0)...{
thrownewIndexOutOfBoundsException("Indexbeyondtheendoftheborder!");
}
Object[]objTemp=(Object[])objArray;
Object[]objs=(Object[])Array.newInstance(objArray.getClass().getComponentType(),
over);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for(inti=startIndex;i<endIndex;i++)...{
objs[i-1]=objTemp[i-1];
}
returnobjs;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicvoidclear()...{
Object[]objTemp=(Object[])objArray;
//清空数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for(inti=0;i<size;i++)...{
objTemp[i]=null;
size=0;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*删除指定的对象实体
*
*@paramobj
*@return
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicbooleanremove(Objectobj)...{
//检查是否合法对象
checkObject(obj);
Object[]object=(Object[])objArray;
for(inti=0;i<size;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(object[i]==obj)...{
remove(i);
returntrue;
}
returnfalse;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*混淆数组元素
*
*@return
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicvoidmixElements()...{
mixElements(objArray);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*检查数组内元素是否为空
*
*@return
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicbooleanisEmpty()...{
return(size==0);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*转为list
*
*@return
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicListgetList()...{
returnArrays.asList((Object[])objArray);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*减少size
*
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
privatevoidback()...{
size--;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*增加size
*
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
privatevoidnext()...{
size++;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*检查索引是否溢出
*
*@paramindex
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
privatevoidcheckIndex(intindex)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(index>=size||index<0)...{
thrownewIndexOutOfBoundsException("Index"+index
+"outofbounds!");
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*检查对象类型
*
*@paramobj
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
privatevoidcheckObject(Objectobj)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(objinstanceofObject[])...{
thrownewIndexOutOfBoundsException("Notloadingarrays!");
}
Stringtype;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(!objType.equals(type=obj.getClass().getSimpleName()))...{
thrownewIndexOutOfBoundsException("Notthis"+type
+"typeofloading!");
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*扩充数组对象
*
*@paramobj
*@parami
*@paramflag
*@return
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
staticpublicObjectexpand(Objectobj,inti,booleanflag)...{
intj=Array.getLength(obj);
Objectobj1=Array.newInstance(obj.getClass().getComponentType(),j
+i);
System.arraycopy(obj,0,obj1,flag?0:i,j);
returnobj1;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*扩充数组对象
*
*@paramobj
*@parami
*@paramflag
*@return
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
staticpublicObjectexpand(Objectobj,inti)...{
returnexpand(obj,i,true);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*随机返回数组内容
*
*@paramobj
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
staticpublicvoidmixElements(Objectobj)...{
inti=Array.getLength(obj);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for(intk=0;k<i;k++)...{
intj=getRandom(k,i-1);
Objectobj1=Array.get(obj,j);
Array.set(obj,j,Array.get(obj,k));
Array.set(obj,k,obj1);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
staticpublicRandomgetRandomObject()...{
returnrand;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
staticpublicintgetRandom(inti,intj)...{
returni+rand.nextInt((j-i)+1);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
privateinthash(Objectobj)...{
inth=obj.hashCode();
h+=~(h<<9);
h^=(h>>>14);
h+=(h<<4);
h^=(h>>>10);
returnh;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicinthashCode()...{
returnhash(objArray.getClass());
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicintsize()...{
returnsize;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/***//**
*反回当前数组对象
*
*@return
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicObject[]getObjectArray()...{
return(Object[])objArray;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
publicstaticvoidmain(String[]args)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*TestBean[]tb=newTestBean[3];
for(inti=0;i<tb.length;i++){
tb[i]=newTestBean();
tb[i].setName("name"+i);
tb[i].setId(i);
}
//直接载入已有数组对象
ArrayUtilarrayUtil=ArrayUtil.getInstance(tb);
TestBeantb1=newTestBean();
//arrayUtil.add(tb[0]);
arrayUtil.remove(tb[0]);
//arrayUtil.remove(tb[2]);
System.out.println(arrayUtil.contains(tb1));
System.out.println(((TestBean)arrayUtil.get(0)).getName());
System.out.println(arrayUtil.size());
//打乱数组
arrayUtil.mixElements();
for(inti=0;i<arrayUtil.size();i++){
System.out.println(((TestBean)arrayUtil.get(i)).getName());
}*/
//生成TestBean的数组实例,初始容量为5
ArrayUtilarrayUtil=ArrayUtil.getInstance(TestBean.class,5);
TestBeant=newTestBean();
t.setName("test");
//在数组载入t的实例
arrayUtil.add(t,0);
TestBeant1=newTestBean();
t1.setName("test1");
arrayUtil.add(t1,1);
arrayUtil.add(t,2);
arrayUtil.add(t,3);
arrayUtil.add(t,4);
//会自动增加数组容量
arrayUtil.add(t);
//显示索引5数据
System.out.println(((TestBean)arrayUtil.get(5)).getName());
//截取索引1-3,显示1数据
System.out.println(((TestBean)arrayUtil.sub(1,3)[1]).getName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
通过例子我们可以发现,只是简单的几行代码,就令Array发挥出我们需要借助他人集合类才能完成的功能。而这些,却本来是我们也可以轻易做到的。
所以本人在此呼吁,坚持尽量使用数组,从我做起,从每个项目做起~~~
分享到:
相关推荐
Java array 数组用法源码集,比如测试数组、划分成绩等级、从方法中返回数组、二分查找法、求二维数组的长度、锯齿数组、处理二维数组等,相信通过这些基础的数组操作实例,会对你了解Java中的数组有很好的帮助作用...
学习 C# 中的 数组 Array数组
Unity3D教程:Array数组类的使用(一) Posted on 2013年07月25日 by U3d / Unity3D 基础教程 /被围观 69 次 U
Array数组工具包 1 1 1 1
Java 基础教程:数组和集合 Java 中的数组和集合是两个非常重要的概念,它们用于存储和操作数据...在实际编程中,根据具体需求选择合适的数据结构是非常重要的,希望本文对你理解和应用 Java 中的数组和集合有所帮助。
TIA博途中数组Array【】的基本使用方法详解
java中数组的各种用法,工程中以代码的形式,例举java中数组的应用,对初学者有帮助,大鸟就不要看了
JS中Array数组学习总结.docx
数组的概念 数组的申明和使用 Array类 ArrayList 泛型的使用 泛型的特征 泛型类、接口、方法的编写和使用 集合接口 枚举
JavaScript Array数组去重源码下载
javscript中json对象和Array数组的区别
对数组的总结和分析,通过这个结构记住它,在开发的过程中使用
javaScript:将Array数组分页处理,支持分页数据容错; js文件:Page4array分页处理工具类; 兼容版本ES6。 博文:https://blog.csdn.net/HTouying/article/details/88898258
数组应用——一维数组排序; 定义一组无序的整型数列,共10个数值,经过一定的处理,使该数组由大到小排列,并输出到屏幕上。具体要求如下: 定义int型数组arrayorder并赋初值为0,长度为10,以承载该数列; 使用...
浅析JavaScript中的array数组类型系统_.docx
Array数组7.16改.docx
Array数组7.14改.docx
数组作为方法返回值类型 学习目标 1 理解方法返回值类型是数组 引用类型的方法签名; 2 理解并学会数组作为方法返 回值类型的方法编写; 3 理解并熟知数组作为返回值 类型的方法调用。 方法的返回值类型 方法的...
任务2、显示一个整型数组中的所有元素 任务3、对数组元素求平均 任务4、将数组内容反向存储 1、使用随机数初始化一个整型数组 数组作为方法的形参 public static void init( ) int[] array …… 0 1 2 …… n-2...
java 数组(array)