Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Generic Question in Java

Options
  • 24-11-2007 8:53pm
    #1
    Closed Accounts Posts: 636 ✭✭✭


    Hi. So I am writing a generic class. I want to create an array of a generic type. I don't know what type this array will contain but it should work for all types. Is this possible?


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Do you mean, that other classes will inherit from this 'generic' abstract class?
    If so, you should be able to create an ArrayList of generic type containing classes that inherit from generic.


  • Moderators, Technology & Internet Moderators Posts: 1,335 Mod ✭✭✭✭croo


    A generic question with a Generics answer ...
    http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    NADA wrote: »
    Hi. So I am writing a generic class. I want to create an array of a generic type. I don't know what type this array will contain but it should work for all types. Is this possible?

    Not sure what you mean "array of generic type"

    But I think maybe you mean..
    import java.util.Set;
    import java.util.TreeSet;
    
    public class SampleGen <MyClass>{
    	
    	public static void main(String[] args) {
    		SampleGen x = new SampleGen();
    	}
    	
    	public void createSetFromClass() { 
    		Set<MyClass> s = new TreeSet<MyClass>();
    	}
    }
    

    I think having an array of any Type defeats the purpose of Generics. You could create one but you couldn't add/remove without casting. But to do it.

    <? extends Object>


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Just to add to that. if you are using Autoboxing, your better off picking the main top level class.

    For example.
    List <Object> list = new LinkedList <Object> ();
    
    list.add(1);
    list.add("test");
    list.add(true);
    

    If you used List <? extends Object> will cause a compiler error as the autoboxing won't be able to determine the type.


  • Subscribers Posts: 4,076 ✭✭✭IRLConor


    Are you asking is it possible to do something like:
    public class MyGenericClass<T>
    {
        public MyGenericClass() {
            T[] genericArray = new T[10];
        }
    }
    

    if so, the answer is no.

    Section 15.10 of the Java Language Specification version 3 says:
    The rules above imply that the element type in an array creation expression cannot be a parameterized type, other than an unbounded wildcard.

    the best thing to do if you want an "array" is to use a java.util.List like so:

    public class MyGenericClass<T>
    {
        public MyGenericClass() {
            List<T> genericList = new ArrayList<T>();
        }
    }
    


  • Advertisement
Advertisement