Reputation: 4952
I have a method that accepts a subtype of Button2. This method does some calculations and creates buttons to be place in an ArrayList so that they are arranged in a graphical way. Here's my code:
public void createButtonArray(ArrayList<? extends Button2> buttonList,
int xLength, int yLength, int width, int height, int inset) {
// The total size for the inset, or spaces between buttons
int totalInset = (xLength - 1) * inset;
// The total height = height of buttons + size of insets
int totalHeight = totalInset + 5 * height;
// ... More calculations
The it comes to this. I don't know how to say this following line because the compiler gives me syntactical errors. How do I create a button that's a subtype of Button2?
Button2<?> button = new Button2(xpos, ypos, width, height);
buttonList.add(button);
counter++;
I've also tried this:
buttonList.add(new <? extends Button2>(xpos,ypos,width,height));
which also gives me an error. How can I create this button to add to my generic ArrayList?
Upvotes: 0
Views: 130
Reputation: 2824
You cannot add any objects (except null) into ArrayList<? extends Button2>
, but you can pass just ArrayList<Button2>
to your function and then do buttonList.add(new Button2(xpos,ypos,width,height))
.
Upvotes: 1