Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good data structure to use for holding two values?

For example, I have in my application a list of a type what has a persons name as it's name and holds two values. The name of the type is the persons name and the type contains only their age and number of std's.

My first idea was to make a class of Persons with Age and NumStds properties where Age and NumStds is required in the constructor and the create a List which I can add to.

class Person
{
    public string Name { get; set; }
    public int NumSTDs { get; set; }
    public int Age { get; set; }

    public Person(string name, int age, int stds)
    {
        Name = name;
        Age = age; 
        NumSTDs = stds; 
    }
}

static void Main(string[] args)
{
    List<Person> peoples = new List<Person>();
    peoples.Add(new Person("Julie", 23, 45)); 
}

I was just wondering if there is a data structure where I could just refer to the elements in the List<> by their Name and have the properties attached to them come along for the ride. Like I could say

people.Remove(Julie) 
like image 555
Crafty Helen Avatar asked Dec 11 '11 03:12

Crafty Helen


People also ask

What type of data structure can hold many values?

An array can hold a collection of integers, floating-point numbers, stings or even other arrays. Stack. A stack stores a collection of items in the linear order that operations are applied. This order could be last in, first out (LIFO) or first in, first out (FIFO).

What are the 2 main types of data structures?

Basically, data structures are divided into two categories: Linear data structure. Non-linear data structure.

Which data structure is best for storing large data?

To store huge data donot use arrays where implementation is easy but insertion and deletion is very difficult. Hence use dynamic like linked list where insertion and deletion is easy. Implement your problem using linked list where the members of linked list are arrays of 100 characters size.


2 Answers

It sounds like your are looking for a Dictionary.

Dictionary<string, Person> peoples = new Dictionary<string, Person>();
Person oPerson = new Person("Julie", 23, 45); 
peoples.Add(oPerson.Name, oPerson); 

Another option is System.Collections.ObjectModel.KeyedCollection. This takes a little more work to implement, but can be useful.

To make this work, create a collection class for person and override the GetKeyForItem method:

public class PersonCollection : System.Collections.ObjectModel.KeyedCollection<string, Person>
{
    protected override string GetKeyForItem(Person item)
    {
        return item.Name;
    }
}

Then you can add items to the collection as in your example:

PersonCollection peoples = new PersonCollection();
peoples.Add(new Person("Julie", 23, 45));

Then to remove the item:

peoples.Remove("Julie");
like image 178
competent_tech Avatar answered Nov 15 '22 06:11

competent_tech


Have a look at the KeyedCollection<TKey, TValue> Class.

KeyedCollection<TKey, TValue> Class

Provides the abstract base class for a collection whose keys are embedded in the values.

You need to derive your own collection class from this abstract class, e.g.

class PersonCollection : KeyedCollection<string, Person>
{
    protected override string GetKeyForItem(Person item)
    {
        return item.Name;
    }
}

Example:

static void Main(string[] args)
{
    var peoples = new PersonCollection();
    var julie = new Person("Julie", 23, 45)
    peoples.Add(julie);

    people.Remove(julie);
    //  - or -
    people.Remove("Julie");
}

Note that the Name property of your Person class should be immutable (read-only).

like image 28
dtb Avatar answered Nov 15 '22 07:11

dtb