I am currently learning the Generics and I got a task where I have to make an Array class with T type-parameter and an array data member and some methods (setItem, getItem, visitor, condition and addAll). I have problem with the addAll method:
public class Array<T> {
private T[] array;
public Array(T[] array){
this.array = array;
}
public void setItem(int i, T item){
if (i < 0 || i > array.length) {
System.out.println("There is no this value in the array!");
}
array[i] = item;
}
public T getItem(int i){
if (i < 0 || i > array.length) {
System.out.println("There is no this item in the array!");
}
return array[i];
}
public <E> boolean addAll(Collection<? extends E> c){
boolean modified = false;
for (E e : c){
if (c.add(e)){
modified = true;
}
}
return modified;
}
}
The NB doesn’t accept the e
in add method. I cannot figure out why…. If I use T type-parameter instead of E in the method (public boolean addAll(Collection<? extends T>
c){} ) the situation is the same. I’ve got message that Incompatible types: E cannot be converted to CAP#1, where E is a type-variable, and CAP#1 is a fresh type-variable. What am I doing wrong?
My 2nd problem is with Array class used an abstract Condition class and it has 6 subclasses. The andCondition, orCondition and so on is OK, but the greaterCondition and doubleCondition do not work. I know where the problem is coming from, but I could not find a solution for that. First of all I used only <T>
after classname and than try the following <T extends Number>
, but no change:
public abstract class Condition<T> {
public abstract boolean condition(T item);
}
public class doubleCondition<T extends Number> extends Condition<T> {
public DoubleCondition (){
}
@Override
public boolean condition(T item) {
if (item % 2 == 0){
return true;
}
return false;
}
I’ve got message: bad operand types for binary operator %
, first type: T, second type: int, where T is a type-variable.
What should I do with type-parameters or with boolean condition method that I be able to check that the item in the parameters can be divide with 2 without left, so it is double/couple.
And the greaterCondition class:
public class greaterCondition<T extends Number> extends Condition<T> {
private T border;
public (T border){
this.border = border;
}
@Override
public boolean condition(T item) {
return item > border;
}
}
Here NB does not handle the >
operator.
Any bit of help is appreciated!