Add, delete, modify, query for Java collections

Source: Internet
Author: User
Tags addall

Sub-class

1  Public classKecheng2 {3 4      PublicString ID;//declaring variable ID for a course5      PublicString name;//declaring variable The name of the course6     7      PublicKecheng (String id,string name)8     {9          This. id=ID;Ten          This. name=name; One          A     } -}

Experimental class

1 ImportJava.lang.reflect.Array;2 Importjava.util.ArrayList;3 Importjava.util.Arrays;4 ImportJava.util.Iterator;5 Importjava.util.List;6 7  Public classDemo18 {9 Ten      PublicList KC;//declaring objects KC belongs to the list type One      A      PublicDemo1 () -     { -          This. kc=NewArrayList ();//equivalent to list kc=new ArrayList (); the     } -      Public Static voidMain (string[] args) -     { -Demo1 Demo1 =NewDemo1 (); +Demo1.add ();//Add -Demo1.show ();//Check +         //Demo1.delall ();//Delete ADemo1.set ();//Modify atSystem.out.println ("====== deleted, modified ======"); -         //demo1.zqshow ();//Check -Demo1.itshow ();//Check -     } -  -     //The addition of the collection: in      Public voidAdd () -     { toKecheng k1=NewKecheng ("1", "Calligraphy appreciation" ); +Kecheng k2=NewKecheng ("2", "Wilderness Survival") ); -Kecheng k3=NewKecheng ("3", "Anime appreciation" ); theKecheng[] K4={K1,K2};//create an array, equivalent to kecheng[] k4={new Kecheng ("1", "Calligraphy Appreciation"), New Kecheng ("2", "Wilderness Survival")} *Kc.add (K1);//Individual Additions $ Kc.add (K2);Panax NotoginsengKc.add (0, K3);//the individual prescribed position is added, and the original set is postponed.  -Kc.addall (Arrays.aslist (K4));//The addition of the array. Array, using the class arrays to directly adjust the static Aslist method inside, returns the list type.  theKc.addall (0, Arrays.aslist (K4));//array Specifies the location to add. The original set is postponed.  +     } A      the     //Lookup Collection Contents: For Loop +      Public voidShow () -     { $          for(inti = 0; I < kc.size (); i++) $         { -Kecheng k= (Kecheng) kc.get (i);//use Kc.get (subscript) To set the object of the class in the collection, strongly to the type of the class, -System.out.println (k.id+ "," +k.name ");//You use the object of the class to fetch the properties of the class.  the         } -     }Wuyi      the     //Lookup Collection Contents: Enhanced for Loop -      Public voidzqshow () Wu     { -          for(Object OBJ:KC) About         { $Kecheng k1= (Kecheng) obj;//use the enhanced for loop to iterate through the objects of the class in the collection, strongly to the class type, -System.out.println (k1.id+ "," +k1.name ");//You use the object of the class to fetch the properties of the class.  -         } -     } A      +     //Lookup Collection Contents: iterator provided by the collection the      -      Public voiditshow () $     { theIterator It=kc.iterator ();//must have this step! If written directly as Kc.iterator (). Hasnext (), the wireless loop prints the first one.  the          while(It.hasnext ())//use the above object it to tune the iterator method.  the         { theKecheng it1 = (Kecheng) it.next ();//use the above object it to get the iterator method. Iterates through the objects of the class in the collection, strongly to the type of the class.  -System.out.println (it1.id+ "," +it1.name ");//You use the object of the class to fetch the properties of the class.  in         } the     } the      About     //Modify Set the      Public voidset () the     { theKc.set (0,NewKecheng ("2", "Wilderness Survival"));//new to replace the object at the specified position.  +     } -      the     //Delete RemoveBayi      the      Public voiddel () the     { -Kecheng k2= (Kecheng) kc.get (6);//You can only delete objects that are picked from the collection example! Instead of a new Object!!!  -Kc.remove (K2);//The first occurrence of the object in the collection can only be deleted, regardless of the location of the object to which it was transferred.  the     } the      the     //Delete RemoveAll the      -      Public voidDelall () the     { theKecheng[] k2={(Kecheng) Kc.get (5), (Kecheng) Kc.get (6)};//You can only delete objects that are picked from the collection example! Instead of a new Object!!!  theKc.removeall (Arrays.aslist (K2));//all the objects in the collection will be deleted!!! 94     } the      the      the}

Add, delete, modify, query for Java collections

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.