Use of generic arrays in Java

Source: Internet
Author: User

 PackageCom.srie.testjava;Importjava.util.ArrayList;Importjava.util.List; Public classTestclassdefine3<t, SextendsT> {     Public Static voidMain (string[] args) {//this will have warning;Testclassdefine3<string, string>[] tcd3 =NewTestclassdefine3[12]; TestClassDefine3<integer, integer> t1i =NewTestclassdefine3<integer, integer>(); TestClassDefine3<string, string> t2s =NewTestclassdefine3<string, string>(); //tcd3[0] = t1i;//This type does not match, there will be compile errors;Tcd3[0] = T2s;//this can be added;List<String> slist =NewArraylist<string>(); Slist.add (A); Slist.add ("B"); @SuppressWarnings ("Unchecked") List<string>[] Slistarr =NewArraylist[12]; slistarr[0] =slist; //System.out.println (Slistarr);         for(list<string>List:slistarr) {            if(List! =NULL) {                 for(String string:list) {System.out.println (string); }            }        }    }}

Post-compilation results:

 PackageCom.srie.testjava;ImportJava.io.PrintStream;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List; Public classTestclassdefine3<t, SextendsT>{   Public Static voidMain (string[] args) {list[] arrayOfList1; Testclassdefine3[] TCD3=NewTestclassdefine3[12]; TestClassDefine3 t1i=NewTestClassDefine3 (); TestClassDefine3 T2s=NewTestClassDefine3 (); tcd3[0] =T2s; List slist=NewArrayList (); Slist.add (A); Slist.add ("B"); List[] Slistarr=NewArraylist[12]; slistarr[0] =slist; intj = (ArrayOfList1 = slistarr). length; for(inti = 0; I < J; ++i) {List List =Arrayoflist1[i]; if(List! =NULL)         for(Iterator localiterator = List.iterator (); Localiterator.hasnext ();) {String string =(String) localiterator.next ();        System.out.println (string); }    }  }}

Use of generic arrays in Java

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.