/** * Returns a 0-length array or collection instead of NULL * * @author Liu Xiangfeng * */ Public class Shop { PrivateList<object> objectList;//0 Length array constants are passed to the ToArray method to indicate the expected return type Private Final Staticobject[] Empty_object_array =Newobject[0]; Public Shop(list<object> objectList) {Super(); This. objectList = objectList; }/** * * @return An array containing all of the objects in the shop */ PublicObject[]getobjects() {//If the collection is empty, he will use a 0-length input array--otherwise, this practice never assigns an array of 0 lengths returnObjectlist.toarray (Empty_object_array); }/** * * @return A copy of collection */ PublicList<object>getstringlist() {if(Objectlist.isempty ()) {returnCollections.emptylist (); }Else{return NewArraylist<object> (objectList); } }/** * Test method * * @param args */ Public Static void Main(string[] args) {list<object> newobjectlist = collections.emptylist ();//Newobjectlist = new arraylist<object> ();Object object =NewObject ();//Newobjectlist.add (object);Shop shop =NewShop (newobjectlist);//Do not need to determine if the value of shop.getobjects () is empty if(Arrays.aslist (Shop.getobjects ()). Contains (object)) {System.out.println ("Jolly good, just the thing"); } }}
Returns a 0-length array or collection instead of null--effective Java reading notes