Java generics: A comparative example of a ArrayList with generics and no generics, and a famous and deleted quote

Source: Internet
Author: User

Package Arraylist;import Java.util.arraylist;import Java.util.collection;import Java.util.iterator;public class famousquotes {private static ArrayList listoffamousquotes;private static arraylist<string> Listoffamousquotestypechecked;public static void Main (string[] args) {famousquotes app = new Famousquotes (); SYSTEM.OUT.PRINTLN ("No reference to generics add three people to say three famous sayings"); App.bulidlist (); App.printlist (); System.out.println ("======================="); System.out.println ("Refer to generics add three people say three famous sayings"); App.bulidcheckedlist (); App.printcheckedlist (); System.out.println ("========================"); SYSTEM.OUT.PRINTLN ("No generic version of the deletion way to designate the famous saying"); String strauthor = "Winston Churchill"; System.out.println ("Delete this person said the famous words and then output to see if there is no such person said" +strauthor); App.expurgate (Listoffamousquotes, strauthor); App.printlist (); System.out.println ("There is a generic version of the deletion way to designate the famous saying"); System.out.println ("Delete this person said the famous words and then output to see if there is no such person said" +strauthor); App.expurgatecheckedlist ( Listoffamousquotestypechecked, Strauthor); App.printcheckedlist ();} No generics add three people say three famous words void Bulidlist () {ListoffaMousquotes = new ArrayList (); Listoffamousquotes.add ("Where There is love there is Life-mahatma Gandhi"); Listoffamousquot Es.add ("A joke is a very serious Thing-winston Churchill"), Listoffamousquotes.add ("In the end,everything is a gag-charl IE Chaplin ");} There are generics added three people say three famous words void Bulidcheckedlist () {listoffamousquotestypechecked = new arraylist<string> (); Listoffamousquotestypechecked.add ("Where There is love there is Life-mahatma Gandhi"); Listoffamousquotestypechecked.add ("A joke is a very serious thing-winston Churchill"); Listoffamousquotestypechecked.add ("In the end,everything is a Gag-charlie Chaplin");} No generic output Three people say three famous words void Printlist () {Iterator listiterator = Listoffamousquotes.iterator (); while (Listiterator.hasnext ()) {String quote = (string) listiterator.next (); System.out.println (quote);}} There are generic outputs three people say three famous words void Printcheckedlist () {iterator<string> quoteiterator = Listoffamousquotestypechecked.iterator (); while (Quoteiterator.hasnext ()) {String quote = Quoteiterator.next (); SyStem.out.println (quote);}} No generics delete void Expurgate (Collection c,string strauthor) {for (Iterator i=c.iterator (); I.hasnext ();) {if ((String) I.next ( ). Contains (Strauthor)) {I.remove ();}}} There is a generic type of delete void Expurgatecheckedlist (collection<string> c,string strauthor) {for (iterator<string> i = C.iterator (); I.hasnext ();) {if (I.next () contains (Strauthor)) {I.remove ();}}}

Java generics: A comparative example of a ArrayList with generics and no generics, and a famous and deleted quote

Related Article

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.