i'm trying to come up with an add method for a generic class i created. generally the object i create will be arrays, of type integer double or string.
i dont know how to go about making the add method to have stuff added to the array... heres my code so far...
/**
The MyList class holds X and Y coordinates. The data type
of the coordinates is generic.
*/
public class MyList<T extends Number>
{
private T xCoordinate; // The X coordinate
private T yCoordinate; // The Y coordinate
/**
Constructor
@param x The X coordinate.
@param y The Y coordinate.
*/
public MyList(T x, T y)
{
xCoordinate = x;
yCoordinate = y;
}
/**
The setX method sets the X coordinate.
@param x The value for the X coordinate.
*/
//INSERT INFO HERE
//PLEASE HELP!!!
public void add(T[] array, int i)
{
int[] newArray = new int[array.length];
System.arraycopy(array, 0, newArray, 0, array.length);
newArray[newArray.length - 1] = i;
return newArray;
}
}
I keep getting a compiler error... with this code above does it meet the requirements of my assignment:
ASSIGNMENT:
Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which accepts a parameter of type T. When an argument is passed to the method, it is added to the ArrayList.