New characteristics of j2se5.0 generis

Source: Internet
Author: User
J2se








The following procedure is easy to understand, and it is easy to understand the generis feature by comparing two programs.



Oldlist.java







/**



* @author Roson



*



* 2005-4-11



*/



Package collection;







Import java.util.*;







public class Oldlist {







private void Testlist ()



{



List list=new ArrayList ();



List.add (New String ("helloworld!"));



List.add (New String ("goodbye!"));



List.add (New Integer (95));



Printlist (list);



}







private void Printlist (List list)



{



Iterator I=list.iterator ();



while (I.hasnext ())



{



String item= (String) i.next ();



System.out.println ("Item:" +item);



}



}







public static void Main (string[] args) {



Oldlist old=new oldlist ();



Old.testlist ();



}



}







Can be compiled, but will error when performing to the third print.



















Newlist.java







/**



* @author Roson



*



* 2005-4-11



*/



Package collection;







Import java.util.*;







public class NewList {







private void Testnew ()



{



List<string> list=new arraylist<string> ();



List.add (New String ("helloworld!"));



List.add (New String ("goodbye!"));



List.add (New Integer (95)); There's no extra plastic.



Printlist (list);



}







private void Printlist (List list)



{



Iterator<string> I=list.iterator ();



while (I.hasnext ())



{



String Item=i.next ();



System.out.println ("Item:" +item);



}



}



public static void Main (string[] args) {



NewList list=new newlist ();



List.testnew ();



}



}











If you add a comment here, you will not be able to compile it and not be wrong in the run-time times. It is helpful to check out errors in advance.















Oldloop.java







/**



* @author Roson



*



* 2005-4-11



*/



Package collection;







Import java.util.*;







public class Oldloop {







public static void Main (string[] args) {



List list=new ArrayList ();



List.add (New String ("helloworld!"));



List.add (New String ("goodbye!"));



For (iterator I=list.iterator (); I.hasnext ();)



{



String item= (String) i.next ();



System.out.println ("Item:" +item);



}



}



}











Newloop.java



/**



* @author Roson



*



* 2005-4-11



*/



Package collection;







Import java.util.*;







public class Newloop {







public static void Main (string[] args) {



List<string> list=new arraylist<string> ();



List.add (New String ("helloworld!"));



List.add (New String ("goodbye!"));



for (String str:list)//It's a lot simpler than it used to be.



{



System.out.println ("Item:" +str);



}



}



}



From the above two programs can be easily seen, the second is much simpler:







Discuss:



Here only take list as an example, of course there are other, such as Hashmaps,linklists and other collection.



There are a lot of improvements in j2se5.0, and there's not much to say. However, you can refer to the English document J2SE5 Nutshell and J2se5. At first, I still feel generis bad, but see the JBSS useful to also feel simple and then see the file, really very convenient oh. You may try!






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.