2

I wanna use an ArrayList for my Code but it should contain different types of Objects. So it should be sth like this:

 switch (foo) {
        case "foo1":
            list=new ArrayList<foo1>();
            break;
        case "foo2":
            list = new ArrayList<foo2>();
            break;

I know the posted Code does not work but is it possible to make sth similar.

And before you ask there should be multiple actions performed on this List but i dont wont to write everything again just with a different name for the List.

public class foo1 {}

public class foo2 {}

    import java.util.ArrayList;
public class work{
    public static void main(String[] args){
    ArrayList<?> list;
     switch (args[0]) {
            case "foo1":
                list=new ArrayList<foo1>();
                break;
            case "foo2":
                 list= new ArrayList<foo2>();
                break;
            default:
                System.out.println("Some Error Text");
                return;

        }
     list.add(new foo1());
    }
}
user3657850
  • 490
  • 1
  • 6
  • 14
  • It is possible, but probably not very useful. It looks like [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Can you describe how would you like to use reference to such lists? – Pshemo May 20 '14 at 18:19
  • the posted code will work in Java 7 – nikis May 20 '14 at 18:19
  • If `list` is defined outside of your `switch` as `List> list`, I don't see any reason why it wouldn't work. – Makoto May 20 '14 at 18:20

1 Answers1

5

What you're asking for is entirely pointless because of type erasure. Type parameters cease to exist at run time, so the thing you get from new ArrayList<Foo1> is exactly the same as the thing you get from new ArrayList<Foo2>.

In other words, type parameters are information for the compiler only. And with the code that you're working with, the compiler really only cares about the declared type of list, not the type that you supply when you instantiate it.

Of course, your code will compile if you've declared

ArrayList<?> list;

or

List<?> list;

or you can narrow it down if Foo1 and Foo2 have a common supertype (other than Object) - say they are both subtypes of Foo, you can write

List<? extends Foo> list;

but why would you bother?

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Thank you that worked, but now i cant use the methodes from ArrayList. I get a "incompatible types: ArrayList can not be converted to ArrayList...". Do i have to cast it to the right type everytime? – user3657850 May 20 '14 at 18:40
  • Can you give an example of a line of code that gives you that error? – Dawood ibn Kareem May 20 '14 at 18:49
  • So i created a List with the wildcard and intialized it with different generic types in a switch case but when i want to use something like fooList.add(foo1); it gives me the error with . – user3657850 May 20 '14 at 19:00
  • It's not clear to me what you mean. Can you edit the code into the question so that I can see it, and maybe try to reproduce it in my own IDE? – Dawood ibn Kareem May 20 '14 at 19:02
  • i added a short part but it should reproduce my problem. – user3657850 May 20 '14 at 19:24
  • Yeah, I see the problem. Basically, when you do an `add`, the compiler wants to check that you're adding the right type of object, but it has no way of doing that. This is closely related to the issue that I answered at http://stackoverflow.com/q/20037561/1081110 and there isn't an easy answer. Is there any way you can design this differently? – Dawood ibn Kareem May 20 '14 at 21:44