1

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);
3
  • 1
    Is this an assignment? Why are you supposed to use an array? If you need to pass the list elsewhere as an array, you can just call the list's ToArray() method Commented Apr 15, 2016 at 19:09
  • "The array size shall always be the same as the amount of Contacts that are stored", is that a requirement? It's not possible to have one array that changes size. You can create new arrays: as a Contact is added you allocate a brand new array that is 1 element larger than the previous one you had allocated, or a new one that is 1 element smaller when removing, and etc.. Or you start with a single array that is very large then do a bunch of work to figure out where you want to stick contacts as they are added and removed. You have to have one or the other case, not both. Commented Apr 15, 2016 at 19:11
  • id est. I got ahead of myself and simply used list when I saw what the user shall be able to do, but I quickly got the feedback that I have to use array for storage. And yeah, that quote was a requirement :/ Commented Apr 15, 2016 at 19:12

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I sort of get this, sort of not. The Copy part as I understand it: Copying of array "a" will begin directly after "deleteIndex"; which here shall be the index of the contact that the user wants to delete. But how shall deleteIndex be set so it actually is [the index of] the contact the user wants to delete? VS will tell me that "deleteIndex" doesn't exist in the context. I'm imagining some code like int deleteIndex = ____; //declare that deleteIndex is the index "item" has in the array "storagelist"?? Resizing the array works smoothly, it's Copying (thus, deleting) correctly that gets me.
@mbksr Yeah, you will have to set deleteIndex to whatever the actual index is of the Contact you want deleted. To find the index you could use a for loop, or you could just use Array.IndexOf()
0

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.

2 Comments

Is there a built-in function for keeping track of the number of elements (contacts) in the array, or how would this be done? Another few hours of reading up on it and trying has given me nothing. :/
You would keep track of it yourself, this is very simple to implement, set a counter to 0 anytime you add an element you increment the counter, anytime you remove from the array you decrement the counter. One thing to keep in mind is boundaries. Removing from an empty list shouldn't give you a -1.
0

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.

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.