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