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