Elad92
Elad92

Reputation: 2491

Generics error in array list

I'm creating an application and I have a FragmentPager that I use. To use FragmentPager I must support it with a FragmentPagerAdapter. I have an interface that I've built the called Nameable and I want to create an array list that you can put there Fragments that implements the Nameable interface.

I've tried those ways but it gives me an error, and I don't really know why because it's a java thing and there are a lot of examples like this over the internet.

This is what I've tried:

private ArrayList<Fragment extends Nameable> mFragmentList;

The error:

Syntax error on token "extends", , expected

Other thing (which is not so right way to do it but I tried it to)

private ArrayList<? extends Fragment & Nameable> mFragmentList;

The error:

Syntax error on token "&", , expected

Why does this statement give me an error? I'm pretty sure that's the right way to do it.

Thanks, Elad.

Upvotes: 1

Views: 244

Answers (1)

millimoose
millimoose

Reputation: 39960

This should be enough:

private ArrayList<Fragment> mFragmentList;

extends and super in generics signatures are used to constrain type parameters, not actual types. That Fragment implements Nameable is already defined in the Fragment class, you don't need to repeat that everywhere.

Upvotes: 3

Related Questions