简体   繁体   中英

Store string data using an array (C#)

A task that I can't seem to solve, even after hours and hours of trying.

Basically, I have a phonebook that takes input from the user: name and number (both string type), which becomes a Contact. I'm supposed to store the Contact in an Array , and the user shall both be able to add and also delete data (Contact) from the array, via the methods Create and Delete.

I made an own Repository class to handle the data (Contact also has an own little class), but I used List to store the data, so I could simply use Add and Remove, so my code looks like this:

public class Repository
{
    List<Contact> storagelist;

    public Repository() {
        storagelist = new List<Contact>();
    }


    public void Create(Contact item) //Adds the item to the list
    { 
        storagelist.Add(item);
    }

    public bool Delete(Contact item) //Removes the item 
    {
        if (!storagelist.Contains(item)) 
            return false;
        storagelist.Remove(item);
        return true;
    }

}

What I am looking for, is how do exactly this, have these 2 features of adding and removing a Contact, but store the data in an Array instead.

Since arrays (to my knowledge) has to have a fixed, pre-defined size I have no idea how it could be used in exactly the same way as the List. The array size shall always be the same as the amount of Contacts that are stored, but how can this be done when an array's size is fixed??

So, how to create an array, that always has the same size as the amount of Contacts that are stored, and how to Add and Remove to/from this array?

Help is very much appreciated!

EDIT: Thanks for all responses! Every answer was helpful in the process (Omar and person66 in particular!). I solved the Removal by "moving" the entire array after the delete-element, to 1 index lower, and finally resizing the array to be smaller. Like so:

int deleteIndex = Array.IndexOf(storagelist, item);

        for (int index = deleteIndex + 1; index < storagelist.Length; index++)
        {
            storagelist[index - 1] = storagelist[index];
        }

        Array.Resize(ref storagelist, storagelist.Length - 1);

You are right in that array sizes are fixed. You can, however, use Array.Resize() to create a new array of the specified size with all the current array data. So for adding you would resize to 1 larger and add the new contact at the end. For removing you will have to use a loop to shift all the elements in the array past the one being removed back one spot, then resize it to be 1 smaller.

EDIT: A simpler option for removing would be to use Array.Copy() :

Array.Copy(a, deleteIndex + 1, a, deleteIndex, a.Length - (deleteIndex + 1));
Array.Resize(ref a, a.Length - 1);

A list is a much better solution to this problem, I don't know why you would ever want to use an array for this.

A List just ends up using an array for it's storage anyway. The way a list works is it is initializes an array with a certain amount of storage then if it's capacity is exceeded it recreates an array with a larger size and copies the elements back. You could try this approach, except you'd just be recreating what a list does.

The other option is just declare an arbitrarily large array of 100,000 elements or so. A number which you know will not be exceeded.

For size you can write your own function which keeps track of the number of contacts in the array.

You can use a generic list. Under the hood the List class uses an array for storage but does so in a fashion that allows it to grow effeciently.

Take a look at this link for more details, it can be helpfull.

var contacts = new[]
{
    new {  Name = "Foo", Phone = "9999999999" },
    new {  Name = "Bar", Phone = "0000000000"   }

};

You can create an array of anonymous object and then use linq to delete objects from array. You can create a new object and insert into anonymous object variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM