Creating an own collection class in c# [closed]

怎甘沉沦 提交于 2019-12-13 23:52:26

问题


I am a new c# developer and im on the learning curve on c#. Now, i have an very small project where I should write my own generic list class for storing data. Im not allowed to use any of the .NET collection classes. My class should have methods for adding/removing items and properties for the number of items from the list. Since im not allowed to use any of the .NET colection classes, my list should be stored in an array in my class.

So, what have I done so far? Since I have no Idea on how to start, I just wrote down the methods that will be used to get a vision of what I should do next:

public class myList
{
    public void addItem()
    {

    }

    public void removeItem()
    {

    }
}

Now, Im pretty lost, what should I do next?


回答1:


I suggest that you read up on how to use arrays on MSDN: http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx. Generally it is bad form to ask the community to essentially write the code for you, but here are some pointers:

  • Write a constructor to initialize the list with an array of objects, e.g. public myListClass(object[] items)
  • Your addItem class should pass the object you are trying to add, so: public void addItem(object newItem)
  • Your removeItem should have a couple of different overloads so that you can determine what you want to remove. e.g. remoteItem(int itemIndex), removeItem(string itemKey) or removeItem(object item)
  • You will probably want to store the current size of the array as a private variable and increment this by 1 each time you add an item. You then declare a new array of the larger size and copy the original items into it. Copy the new item last.
  • You may also want a sort method, but this will depend entirely on how complex the objects are that will be stored in the array.
  • You probably don't have to reduce the size of the array when removing items though you may find it necessary to count the number of items etc. If you don't reduce the size of the array every time an item is removed be sure to take this into account when adding items (since it may already be large enough).
  • Write a count() method that returns the number of items in the array - use this to check for null values if you don't reduce the array's size when removing items.

There are a ton of other considerations, not least of which is using generics to allow multiple types to be stored. I suggest a little reading around the topic: http://msdn.microsoft.com/en-gb/library/512aeb7t.aspx

Good luck!




回答2:


You'll need to store the items in your "List" in an array (a private member of your class).

The problem is that an array has a fixed size (you can't just add items to it). So you need to give it some initial size and remember how "full" it is. When you need more space, you need to create a new, larger array and copy items over from the original one.




回答3:


here you go, not particularly elegant but a starting point none the less and it doesn't use .net collection classes

public class Program
{
    public static void Main()
    {
        myList<int> lst = new myList<int>();
        lst.addItem(10);
        lst.addItem(20);
        lst.addItem(30);

        foreach (var i in lst.getItems())
        {
            Console.WriteLine(i);
        }

        lst.removeItem(20);

        foreach (var i in lst.getItems())
        {
            Console.WriteLine(i);
        }

        Console.ReadLine();

    }
}


public class myList <T>
{
    private T[] items = new T[0];

    public void addItem(T item)
    {
        Array.Resize(ref items, items.Count() + 1);
        items[items.Count()-1] = item;
    }

    public void removeItem(T item)
    {
        items[Array.IndexOf(items, item)] = items[items.Count() - 1];
        Array.Resize(ref items, items.Count() -1);
    }

    public IEnumerable<T> getItems()
    {
        return items;
    }
}



回答4:


A simple collection isn't that hard to implement, but you would need to make the following:

An array to hold the data. Using generics, it could be something like private T[] data = new T[10];.
This will of course limit the size to 10, but this can be changed :-)

Generic methods. Something like public T GetItem(int index){...}

Also if you know how, it might be great to have an indexer. You can google that :-)




回答5:


You need to implement a linked list in C#.

Check this out: http://csharp-algos.blogspot.com/2010/09/blog-post.html

This is the standard way of doing this without any .NET libraries



来源:https://stackoverflow.com/questions/15063366/creating-an-own-collection-class-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!