0
I have this code problem, it's due tomorrow and I do not know how to do it
Write a typed class GenericList<T> that holds a list of elements of type T. Store the elements of the list in a fixed-capacity array that is specified as a parameter to the class's constructor. Add methods to add an element, access an element by index, remove an element by index, insert an element at a specified position, clear the list, search for an element by value, and redefine the ToString() method. For now all I could do is create the class and add the array but I do not know how to specify the array as a parameter and add the methods, could somebody help? public class GenericList<T> { ArrayList myList = new ArrayList(); }
2 Antworten
+ 4
If you don't know how to use generic type parameters, you should review this article. It has very good example codes also, which is very similar to what you will need to implement
https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/generics
+ 1
Generic Classes
class Stack<T> {
int index=0;
T[] innerArray = new T[100];
public void Push(T item) {
innerArray[index++] = item;
}
public T Pop() {
return innerArray[--index];
}
public T Get(int k) { return innerArray[k]; }
}
https://www.sololearn.com/learning/1080/2694/5600/1