将播放列表轨迹分配给3D阵列

时间:2017-02-12 00:20:05

标签: c# arrays multidimensional-array spotify

所以我正在使用Spotify的API进行实验,并遇到了我曾经遇到过的最复杂的问题。我有Genre Textfiles,例如“寒意”或“众议院”。在这些文件中,我有Spotify-Playlists的链接。这些行中的第一行代表我对这种类型的播放列表,例如:

Maker   Model   No Sold(,000s)
Ford    Kuga    35
Ford    Focus   47
Ford    Ka          31
Ford    Fiesta      68
Ford    Mondeo      55
Ford    S-Max       34
Ford    Galaxy      23
Nissan  Leaf        28
Nissan  Micra       31
Nissan  Note            43
Nissan  Pulsar      23
Nissan  Juke            57
Nissan  Qashqai     62
Nissan  X-Trail         38
Honda   Jazz            24
Honda   Civic           32
Honda   HRV         33
Honda   CRV         29
Honda   Accord          30
Honda   NSX         15
Toyota  Aygo            44
Toyota  Auris           45
Toyota  Avensis         35
Toyota  Prius           32
Toyota  Rav4            29
Toyota  Land Cruiser    14
Citroen C1          40
Citroen C3  25
Citroen C4  46
Citroen DS3 35    
Citroen DS4 31
Citroen DS5 25    
Audi    A1  23
Audi    A3  47
Audi    A4  30
Audi    A6  20
Audi    A8  18
BMW 1 Series    36
BMW 2 Series    20
BMW 3 Series    53
BMW 4 Series    21
BMW 5 Series    27
BMW 6 Series    24
BMW 7 Series    16

现在,由于这些链接指向播放列表,因此这些播放列表包含曲目。现在我想从那些随机播放列表中获取所有曲目并将其装入我的(过滤和添加曲目不是问题,只是让你理解)。 现在我想我可以创建一个3D数组来处理这些曲目,例如:

myplalyist-ID 
random1-ID 
random2-ID ...

我希望你理解我的意思,我希望能够通过以下内容访问播放列表:

1D: genre1    - genre2    - genre3
2D: playlist1 - playlist2 - playlist3
3D: tracks1   - tracks2   - tracks3

所以我的方法如下:

foreach(PlaylistTrack track in array[genre][playlist])
    // filter & add "track"

非常感谢任何帮助,因为我完全迷失了! :)

编辑: 这就是我的尝试:

//PlaylistTrack is the type in which Spotify stores a track within a Playlist
private List<PlaylistTrack>[,,] playlistTracks;//3D???
//this is to store the amount of playlists within a genre
private int[] playlistArray;
//int to save the amount of genre-files
private int fileCount;

//-----------------METHOD:

private void getTracks()
{
    DirectoryInfo dir = new DirectoryInfo(this.path);//directory where genres are stored

    this.fileCount = 0;

    foreach (var file in dir.GetFiles("*.txt"))
    {
        if (file.Name != "My Tracks.txt" && file.Name != "Tracks.txt")//getting all genre textfiles
        {
            this.fileCount++;
        }
    }

    this.playlistArray = new int[this.fileCount];

    //i know using the foreach over and over is kinda bad and not preofessional,
    //but i don't use c# on a daily base and i didn't knew how to get it done otherwise

    int count = 0;
    foreach (var file in dir.GetFiles("*.txt"))
    {
        if (file.Name != "My Tracks.txt" && file.Name != "Tracks.txt")
        {
            int count2 = 0;
            if (File.ReadAllText(file.FullName) != "")
            {
                using (StreamReader sr = new StreamReader(file.FullName))
                {
                    string line = "";
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line != "")
                        {
                            count2++;
                        }
                    }
                }

            }
            this.playlistArray[count] = count2;
            count++;
        }
    }

    for (int i = 0; i < this.fileCount; i++)
        this.playlistTracks[i] = new List<PlaylistTrack>[this.playlistArray[i]]();
    //here i'm stuck, how would i initialize the array, so it can holds a bunch of PlaylistTrack Items in "3rd row", 
    //accessable through [genre][playlist]
}

错误发生,我在代码中标记了它。错误消息是:

  

“System.Collections.Generic.KeyNotFoundException”异常有   发生在mscorlib.dll中。附加信息:指定的密钥   未在字典中指定。

(大致翻译自德语)

1 个答案:

答案 0 :(得分:1)

由于每个播放列表可以包含不同数量的歌曲,因此您不需要固定大小的3D矩阵([,,,]),而是需要一组数组([][][])。您可以在this question中阅读它们之间的区别。

话虽这么说,你可以使用PlayListTrack[][][]达到你想要的效果。 您可以使用var allTracks = new PlayListTrack[amountOfGenres][][];之类的内容,然后将allTracks的每一行初始化为PlayListTrack[][],其大小是与该行匹配的类型的播放列表数量。最后,您可以将每个播放列表初始化为PlayListTrack[],其大小是给定播放列表的歌曲数量。

无论如何,我建议您查看Dictionary<TKey, TValue>类,它允许您将唯一键(例如,类型的ID)映射到值(例如,播放列表) ,这可能是字典本身。)

然后,您可以拥有类似的内容:

// Lists of tracks, indexed by their genre id (first string) and
// their playlist id (second string)
var allTracks = new Dictionary<string, Dictionary<string, List<PlayListTrack>>>();

// Examples

// Getting a genre, which is a Dictionary<string, List<PlayListTrack>>
var allJazzPlayLists = allTracks["jazz"];

// Getting a list of songs, which is a List<PlayListTrack>
var songs = allTracks["hip-hop"]["west side"];