1

so I have these four classes/interface:

  public class Box <S extends SomeClass> implements Comparable <Box<S>> {...}
  public interface SomeClass <T extends Comparable<T>> {...}
  public class ThisItem implements SomeClass {...}
  public class OtherItem implements SomeClass {...}

And I am trying to create a list of Box that holds a list of instances of ThisItem. I'm unsure as to why this is giving me an error.

  public ArrayList<ArrayList<Box>> variable = new ArrayList<ArrayList<Box>>();
  this.variable.add(new ArrayList<Box<ThisItem>>(5));
2
  • 2
    Make sure to include the exact error messages in questions. Commented May 10, 2013 at 0:17
  • 1
    Is it just me or do sometimes people just take generics too far? Commented May 10, 2013 at 0:22

5 Answers 5

5

Box is a generic class, so when use it just as Box, it is a raw type, which is different from Box<ThisItem>, which has a type parameter specified. This is similar to ArrayList<Box>, where Box it the type parameter.

Change this:

public ArrayList<ArrayList<Box>> variable = new ArrayList<ArrayList<Box>>();

To:

public ArrayList<ArrayList<Box<ThisItem>>> variable = new ArrayList<ArrayList<Box<ThisItem>>>();
Sign up to request clarification or add additional context in comments.

1 Comment

This gets rid of the error, however I need to instantiate the ArrayList, then either add ThisItem or OtherItem ArrayList to it in a constructor for a new class so I can add items of their respective type to it.
1

How do you think, would it be safe to let variable store list like ArrayList<Box<ThisItem>>?

If Java would let that happen then while getting that list from variable it would be cased to ArrayList<Box>. Because of that returned list would let you add any kind of Box object to that list originally ArrayList<Box<ThisItem>> was suppose to store only Box<ThisItem> object.

To get rid of that problem you should declare your variable as

public ArrayList<ArrayList<Box<ThisItem>>> variable
 = new ArrayList<ArrayList<Box<ThisItem>>>();

2 Comments

The problem is I dont know if the box needs to be of type ThisItem or OtherItem until I get into the constructor that requires arraylist of arraylists. So would there be a way to create an instance variable in the constructor w.o defining the variable outside it?
@TheGandhi Purpose of this error is to stop you from situation described in my answer, but if you are absolutely sure that you wont add incorrect object to incorrect lists, you can get rid of this error by casting your new list to raw type like variable.add((ArrayList)new ArrayList<Box<ThisItem>>(5));. But this way of coding is dangerous for type safety so don't use it unless you really have no other option left (in most cases people can redesign their projects to avoid this kind of situations).
1

ThisItem is a raw type of SomeClass; it's roughly the same as declaring it as implements SomeClass<Object>, so the compiler can't verify that it's appropriate to be used in that way.

Instead declare it as typed:

public class ThisItem implements SomeClass<SomeComparableClass> {...}

2 Comments

I'm not quite sure what you mean by <SomethingComparable>. Could you explain that please?
SomeClass is defined as ` public interface SomeClass <T extends Comparable<T>> , which means to implement it is a typed` interface (not a raw type) you must provide a type for the generic parameter, and that type is bounded by the Comparable type, so SomethingComparable is meant to be that type. I'll change its name then - see edit
0

What about this..

public ArrayList<ArrayList<Box>> variable = new ArrayList<ArrayList<Box>>();
this.variable.add(new ArrayList<Box<ThisItem>>(5));

to...

public ArrayList<ArrayList<Box>> variable = new ArrayList<ArrayList<Box>>();
ArrayList<Box<ThisItem>> tempInstance = new ArrayList<>();
tempInstance.add(new Box<ThisItem>()); //add new Boxes manually as you wish
this.variable.add(tempInstance);

1 Comment

Got an error, "Cannot covert ArrayList<?> to ArrayList<Box<ThisItem>>"
0

This should work:

public ArrayList<ArrayList<? extends Box>> variable = new ArrayList<ArrayList<? extends Box>>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.