Question: Im giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C#

Im giving you code for a Class called GenericArray, which is an array that takes a generic object type.

As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file.

And a program that will do some basic stuff with it

Generic Array List What I want you to do today:

Create methods for the GenericArray,

Easy(ish):

public void Append (T, value) {

// this should add T to the array at the next available index.

// to do this you should probably change the class to keep track of the current index, and then only append items to the array at the current index. Easy Enough.

}

public void PrintAll(){

//for every element in the array, just print the [index] and the value

// assume that the object has some way to be displayed (so you dont have to do anything to make this work if the generic type are integers, floats or strings etc.

// so something like System.Console.WriteLine(getItem(i)); should work to print the value.

}

Moderate:

public bool Find(T value)

{

// given some value, search the array and see if it exists, if it does return true, if not return false.

}

Change the Grow method to your own implementation, where you create a new array (double the size of the previous one) and copy all of the previous elements over

Private void Insert (T value)

{

//Insert should *insert* a value at an index by shifting all elements down by one

// Make sure to check if the array is big enough first, and if not, grow it

}

Advanced:

Change printAll to return a string, and then have the main program print that String

public string printAll(){

// now, rather than the list itself being responsible for printing, it should return a string (formatted somehow) that should be printable

}

this is the code i will provide you

public class GenericArray { private T[] array;

public GenericArray(int size) { array = new T[size + 1]; } public T getItem(int index) { return array[index]; } public void setItem(int index, T value) { if (index >= array.Length) Grow(array.Length * 2);

array[index] = value; } public void Grow(int newsize) { Array.Resize(ref array, newsize); } } } class Program { static void Main(string[] args) { GenericArray array2; int numelements = 5; array2 = new GenericArray(numelements);

for (int i = 0; i < numelements; i++) { array2.setItem(i, i * 2); } for (int i = 0; i <= numelements; i++) { Console.WriteLine(array2.getItem(i)); } Console.ReadLine(); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!