Summary of generic array creation in Java

Source: Internet
Author: User

In Java, you can declare a generic array , and you cannot create an array by directly using t[] tarr=new t[10] , and the simplest way is through array.newinstance (classtype,int Size) is the way to create an array such as the following program.

 Public classarraymaker<t> {

PrivateClass<t> type;

Public Arraymaker (class<t> type) {
this.type = type;
}

@SuppressWarnings ("Unchecked")
t[] Createarray (int size) {
return (t[]) array.newinstance (type, size);
}

List<t> CreateList () {
return NewArraylist<t> ();
}

/** * @paramargs*/
Public Static voidMain (string[] args) {
/** Even though kind is stored as class<t>, erasure means the It is actually just being stored as a Class, with
* No parameter. So, if you don't some thing with it, as in creating an array, array.newinstance () doesn ' t
* Actually has the type information that ' s implied in kind; So it cannot produce the specific result, which
* Must therefore be cast, which produces a warning so you cannot satisfy.
*/
Arraymaker<type> AM2 =NewArraymaker<type> (Type.class);
System.out.println (Arrays.aslist (Am2.createarray (10)));
System.out.println (Arrays.aslist (Am2.createlist ()));
}
}
classType {
@Override
PublicString toString () {
return"Type";
}
}

Program One: This program mainly shows that in the use of generic arrays are prone to problems, because the book for the description of the program is more detailed, so only to reference the program.

Class Generic<t> {

}

Public class Arrayofgeneric {

public static void Main (string[] args) {

Generic<integer>[] Genarr;

Genarr = (generic<integer>[]) new generic[2];

System.out.println (Genarr);

}

}

Program Two: This program is mainly described in the execution of the program, the type information of the generic array will be erased, and in the course of the operation of the array type has and only object[], if we cast to the t[] type, although at compile time will not have an exception to produce, However, the runtime will have classcastexception thrown.

 Public classArrayofgeneric2<t> {         Publict[] TS;  PublicArrayOfGeneric2 (intsize) {TS= (t[])NewObject[size]; }            PublicT Get (intindex) {              returnTs[index]; }                 Publict[] Rep () {returnts; }                Public voidSetintindex, T t) {Ts[index]=T; }                Public Static voidMain (string[] args) {
Arrayofgeneric2<string> aog2 =NewArrayofgeneric2<string> (10); Object[] Objs=Aog2.rep (); System.out.println (OBJS); /*Would throw classcastexception*/string[] STRs = Aog2.rep ();//system.out.println (STRs); } }

Program Three: The main description in the object by using object[] to save the data, the resulting object is that it can be held by the object is converted between T and object, but when the design to the array of conversion, still will be reported classcastexception

 Public classArrayofgeneric3<t>{object[] ts;  PublicARRAYOFGENERIC3 (intsize) {TS=NewObject[size]; }            PublicT Get (intindex) {            return(T) Ts[index]; }             Publict[] Rep () {return(t[]) TS; }             Public voidSetintindex, T t) {Ts[index]=T; }             Public Static voidMain (string[] args) {
Arrayofgeneric3<integer> aog2 =NewArrayofgeneric3<integer> (10); Object[] Objs=Aog2.rep (); for(inti = 0; I < 10; i++) {Aog2.set (I, I); System.out.println (Aog2.get (i)); } integer[] STRs=Aog2.rep (); System.out.println (STRs); } }

Program Four: is a relatively perfect solution to a generic array

 Public classArrayofgeneric4<t>{t[] ts;  PublicARRAYOFGENERIC4 (class<t> type,intsize) {            /*To solution array of generic key code!*/TS=(t[]) array.newinstance (type, size); }              PublicT Get (intindex) {               returnTs[index]; }             Publict[] Rep () {returnts; }         Public voidSetintindex, T t) {Ts[index]=T; }          Public Static voidMain (string[] args) {
Arrayofgeneric4<integer> aog2 =NewArrayofgeneric4<integer> (Integer.class, 10); Object[] Objs=Aog2.rep (); for(inti = 0; I < 10; i++) {Aog2.set (I, I); System.out.println (Aog2.get (i)); } Try{integer[] STRs=Aog2.rep (); System.out.println ("User array.newinstance to create generci of Array is successful!!!!!"); } Catch(Exception ex) {ex.printstacktrace (); } } }

Please see the original http://developer.51cto.com/art/201202/317813.htm

Summary of generic array creation 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.