views:

41

answers:

2

What is the best way to create this data structure: Key - field1- field2 - ... fieldn. So every record can have a key and then multiple values following that key .. like for eg: key is the employee id and fields are name age salary .. So basically something like a database table but want to do it using a data structure such as hashtable .. But hashtable allows you to add only key,value pair .. i guess i need key,value1,value2 etc ..

A: 

I case the data structure is a Database:

  • Table 1: Keys {Id, Name}
  • Table 2: Values {KeyId, Value}

I case you mean a programming language:

public class Key{
    public List(Of T) Values
}

and than you can use a List of Keys.

Robert
A: 

I you want to have a single variable and not cross refrence, you can have a nested array and hash table, or any combination of those depending on your use. You didn't specify language, so in Python you could do something like this:

 [ {"name":"Bob", "age":20},  {"name":"Joe", "age":21} ]

and the index would be employeeID, or you could have an array of employee dictionaries

[{ "ID":1, "info":{"name":"Bob", "age":20}}, {"ID":2, "info":{"name":"Joe", "age":21}} ]

This hasn't been verified for syntax mistakes.

Iuvat