Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Trying to create a generic array

Reply
Thread Tools

Trying to create a generic array

 
 
lbrtchx@gmail.com
Guest
Posts: n/a
 
      05-01-2008
I am getting an "incompatible types" compile time error, even though
to me (and apparently to the compiler too) these are just three forms
to refer to the same class:

1) ai.class;
2) Class.forName("ai");
3) ((Class.forName("ai")).newInstance()).getClass();

Here is the actual code example:

// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
import java.util.*;
import java.lang.reflect.*;

class ai{
String a;
int i;
}

class gnrx10{
gnrx10(){}

public <T> T[] createTypeAr(Class<T> Tp, int iSz){
T[] TpAr = (T[])Array.newInstance(Tp, iSz);
return(TpAr);
}
}

public class gnrx10Test{
public static void main(String args[]){
gnrx10 g = new gnrx10();

// __ this works fine!
String[] aSAr = g.createTypeAr(String.class, 4);
System.out.println("// __ aSAr.length: |" + aSAr.length + "|");

ai[] aiAr = g.createTypeAr(ai.class, 16);
System.out.println("// __ aiAr.length: |" + aiAr.length + "|");
// __
try{

System.out.println(ai.class);
System.out.println(Class.forName("ai"));
System.out.println(((Class.forName("ai")).newInsta nce()).getClass());

// __ this doesn't!
/*
Class K = ai.class;
System.out.println(K);
aiAr = g.createTypeAr(K, ;
System.out.println("// __ aiAr.length: |" + aiAr.length + "|");

gnrx10Test.java:47: incompatible types
found : java.lang.Object[]
required: ai[]
aiAr = g.createTypeAr(K, ;
^
*/

}catch(ClassNotFoundException KNFX){ KNFX.printStackTrace(); }
catch(InstantiationException InstX){ InstX.printStackTrace(); }
catch(IllegalAccessException IlgAxX){ IlgAxX.printStackTrace(); }
}
}
 
Reply With Quote
 
 
 
 
lbrtchx@gmail.com
Guest
Posts: n/a
 
      05-01-2008
OK, all these cases work fine:

Class<KAI> K00 = KAI.class;

Class<KAI> K02 = (Class<KAI>)Class.forName("KAI");

Class<? extends KAI> K04 = (Class<KAI>)Class.forName("KAI");

Class<? extends KAI> K06 = (Class<? extends
KAI>)Class.forName("KAI");

But what I am really trying to do is just do it based on a string
declaring the name of the class, namely "KAI", without using type
declarations "Class<KAI>"-like in the code

1) Say you gain some info about a DTO you will use, then

2) you use reflection to actually make such an object, say "KAI"

3) you save then the class in a reachable place and load a second
class which will be the one using "KAI" objects

How could you do that?

lbrtchx
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot create a generic array of <type> Warren Tang Java 8 10-21-2011 03:05 AM
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Why can't I create a generic array? Andrea Desole Java 3 09-09-2006 05:10 AM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM



Advertisments