tags:

views:

6087

answers:

5

Hello, I'd like to set up a multidimensional list. For reference, I am working on a playlist analyzer.

I have a file/file-list, which my program saves in a standard list. One line from the file in each list entry.

I then analyze the list with regular-expressions to find specific lines. Some of the data/results from the lines needs to be put into a new multidimensional list; since I don't know how many results/data I'll end up with, I can't use a multidimensional array.

Here is the data I want to insert:

List
(
    [0] => List
        (
            [0] => Track ID
            [1] => Name
            [2] => Artist
            [3] => Album
            [4] => Play Count
            [5] => Skip Count

        )
    [1] => List
        (
And so on....

Real Example:

List
(
    [0] => List
        (
            [0] => 2349
            [1] => The Prime Time of Your Life
            [2] => Daft Punk
            [3] => Human After All
            [4] => 3
            [5] => 2

        )
    [1] => List
        (

So yeah, mlist[0][0] would get TrackID from song 1, mlist[0][0] from song 2 etc.

But I am having huge issues creating a multidimensional list. So far I have come up with

List<List<string>> matrix = new List<List<string>>();

But I haven't really had much more progress :(

Any help?

+16  A: 

Well you certainly can use a List<List<string>> where you'd then write:

List<string> track = new List<string>();
track.Add("2349");
track.Add("The Prime Time of Your Life");
// etc
matrix.Add(track);

But why would you do that instead of building your own class to represent a track, with Track ID, Name, Artist, Album, Play Count and Skip Count properties? Then just have a List<Track>.

Jon Skeet
Hmm, I am honestly not sure how to do that!I looked at setting up a class for playlist handling alone, but I guess that is a better idea.
CasperT
Also, wouldn't it require knowing how many tracks I will eventually create/store?
CasperT
No, because the List<Track> is still dynamically sized. You'd parse the data for one track, create a new instance of Track, add it to the List<Track> and then parse the next one, etc.
Jon Skeet
caspert, the List<T> keeps track of the amount of objects it stores for you. You can access the amount by calling List<T>.Count
Spoike
A: 

Listen to what Jon Skeet tells ;)

Create your own class name Track with all the track variables (id, name, artist etc) And store that in a List. Dont make it complex by storing info in a array without any knowledge what the value is, they are all strings.

PoweRoy
+8  A: 

As Jon Skeet mentioned you can do it with a List<Track> instead. The Track class would look something like this:

public class Track {
    public int TrackID { get; set; }
    public string Name { get; set; }
    public string Artist { get; set; }
    public string Album { get; set; }
    public int PlayCount { get; set; }
    public int SkipCount { get; set; }
}

And to create a track list as a List<Track> you simply do this:

var trackList = new List<Track>();

Adding tracks can be as simple as this:

trackList.add( new Track {
    TrackID = 1234,
    Name = "I'm Gonna Be (500 Miles)",
    Artist = "The Proclaimers",
    Album = "Finest",
    PlayCount = 10,
    SkipCount = 1
});

Accessing tracks can be done with the indexing operator:

Track firstTrack = trackList[0];

Hope this helps.

Spoike
If you want to be really savvy, Track could also be a struct. ;)
Spoike
Not with the definition you've given... Structs should have an instance size of less than 16bytes...
Ian
Thanks for the demonstration!
CasperT
@Ian: Hmm. I wasn't aware of that. Quickly checked MSDN doc for that and it turns out structs need to be less than 16 bytes. Thanks for pointing it out.
Spoike
@Spoike: They don't _need_ to be, it's just a recommendation. It doesn't really matter that much; choose struct if you need value semantics otherwise just class. If you don't know, use a class.
Jason
A: 
caspert,

Jon is right. And no it doesn't require knowing how many you will store, as a .NET list does not have a size unless you specify one, so you can keep adding to them.

Create a new class:

public class Track
{
   public Track()
   {

   }

   public String TrackName { get; set;}
   public String Artist { get; set; }
}

etc.. Then using:

List<Track> tracks = new List<Track>();
Track tempTrack = new Track();
tempTrack.TrackName = "Consuming Fire";
tempTrack.Artist = "Tim Hughes";
tracks.Add(tempTracK);

Means you don't need any 2D arrays and is way more flexible...

Ian
A: 

Hello, I found this thread handy. Im filling the content of an XML file into a List. After this is done I look inside and see four entries (containing 6 string variables) which is correct but they are all the same value as the last entries inserted. Can anybody shed some light on this please?

private void GetXMLDatabaseEntries()
    {
        if (m_myXmlFile != null)
        {
            myDbCredList = new List<Database>();
            var setupDatabases = from e in m_myXmlFile.Descendants("setupDatabases")
                                 where e.Attribute("ID").Value == "10"
                                 select e;
            foreach (var entry in setupDatabases)
            {
                IEnumerable<XElement> myElements = entry.Elements();

                foreach (XElement el in myElements)
                {
                    mySetDatabase.m_DbKey = el.Element("itemKey").Value;
                    mySetDatabase.m_DbValue = el.Element("itemValue").Value;
                    mySetDatabase.m_DbType = el.Element("itemType").Value;
                    mySetDatabase.m_DbBackupPath = el.Element("itemBackupPath").Value;
                    mySetDatabase.m_DbUsername = el.Element("itemUsername").Value;
                    mySetDatabase.m_DbPassword = el.Element("itemPassword").Value;
                    myDbCredList.Add(mySetDatabase);
                }
            }
        }
        m_listNo = myDbCredList.Count;
    }

I get all four entries hold the same values...

Thank you

Michael Kearney

Michael Kearney
look at XmlSerializer http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx and ask this as a question instead of an unrelated answer.
Matthew Whited
You need to create your own question
Isaac Cambron