循环只发射一次

时间:2016-04-14 04:22:42

标签: c# loops for-loop foreach

大家好我正在阅读包含播放列表的文件。 (mp3文件位置的行)

我目前有格式(名称; c:\ song.mp3; c:\ song.mp3; c:\ song.mp3; c:\ song.mp3等)

文件中有很多行。

我已尝试过两个foreach循环和for循环尝试解决此问题(如下所示)

string[] lines = File.ReadAllLines("playlists.txt");
MessageBox.Show(lines.Count().ToString());

for (int y = 0; y <= lines.Count(); y++)
{
     string[] res = lines[y].Split(';');
     for (int x = 0; x <= res.Count(); x ++)
     {
          if (x == 0) { currentPlaylist = new Playlist(res[x]); }
          else { currentPlaylist.Add(new MP3(res[x])); }
     }
}

但由于某种原因,它只会循环一次(我用foreach取代了外部循环,结果相同。

即使消息框中显示的lines.Count()显示的数字大于1

我敢肯定,一旦解决这个问题,它必定是基本错误,但我失去了

由于

编辑*这个文件不知道这会有什么帮助...

Library;C:\Users\Blah\Desktop\Iphone Music\Queens of the Stone Age - If I Had A Tail.mp3

playlist1;C:\Users\Blah\Desktop\Iphone Music\Red Hot Chili Peppers - Can t Stop.mp3;C:\Users\Blah\Desktop\Iphone Music\Red Hot Chili Peppers - Otherside .mp3

playlist2;C:\Users\Blah\Desktop\Iphone Music\Red Hot Chili Peppers - Otherside .mp3;C:\Users\Blah\Desktop\Iphone Music\Foo Fighters - Best Of You.mp3

playlist3;C:\Users\Blah\Desktop\Iphone Music\Red Hot Chili Peppers - Otherside.mp3;C:\Users\Blah\Desktop\Iphone Music\Foo Fighters - The Pretender.mp3

playlist4;C:\Users\Blah\Desktop\Iphone Music\Foo Fighters - Everlong.mp3;C:\Users\Blah\Desktop\Iphone Music\Foo Fighters - My Hero.mp3;C:\Users\Blah\Desktop\Iphone Music\I Am Giant - City Limits.mp3

我把它放在代码中,以便更容易阅读

我唯一的问题是内循环只发射一次,我不确定为什么。

以下内容......

for (int x = 0; x <= res.Count(); x ++)
     {
          if (x == 0) { currentPlaylist = new Playlist(res[x]); }
          else { currentPlaylist.Add(new MP3(res[x])); }
     }
无论代码中的行数是多少,

都会导致外循环只触发一次,如果我删除了内循环,则外循环会循环预期的次数

4 个答案:

答案 0 :(得分:0)

据我所见,您的代码应抛出异常。您在循环条件中使用&lt; =但在x == res.Count()时无法访问res[x],因为索引必须介于0和Count-1之间。

尝试至少将<=替换为<

不要在数组上使用Count()。他们有.Length财产。

答案 1 :(得分:0)

假设您发布的内容是您正在阅读的文件,则每行包含mp3的单个文件位置。用&#39;&#39;分割每一行之后你可以放心地假设新数组的第二个条目是文件路径。

string[] lines = File.ReadAllLines("playlists.txt"); // 100 lines 

for (int y = 0; y < lines.Length; y++) loops through array index 0 to 99
{
     string[] res = lines[y].Split(';'); // lines[y] should be something like c:\song.mp3 ; c:\song.mp3 ; c:\song.mp3
     // res[0] = Library
     // res[1] = C:\Users\Blah\Desktop\Iphone Music\Queens of the Stone Age - If I Had A Tail.mp3
     currentPlaylist = new Playlist(res[0].Trim());
     currentPlaylist.Add(new MP3(res[1].Trim()));
}

或者如果每一行都有多个文件路径..

string[] lines = File.ReadAllLines("playlists.txt"); // 100 lines 

for (int y = 0; y < lines.Length; y++) loops through array index 0 to 99
{
     string[] res = lines[y].Split(';'); // lines[y] should be something like c:\song.mp3 ; c:\song.mp3 ; c:\song.mp3
     // res[0] = Library
     // res[1] = C:\Users\Blah\Desktop\Iphone Music\Queens of the Stone Age - If I Had A Tail.mp3
     currentPlaylist = new Playlist(res[0].Trim()); // res[0] is the playlist name right?
     for(int x = 1; x < res.Length; x++)
     {
         currentPlaylist.Add(new MP3(res[x].Trim()));
     }         
}

答案 2 :(得分:0)

根据您撰写的问题和评论,我认为您正在寻找类似的内容:

// This method read the file and populate a List of Playlist.
// Each PlayList contain a list of MP3 songs
public List<Playlist> GetPlayLists()
{
    // read all lines from the text file
    string[] lines = File.ReadAllLines(@"c:\Temp\playlists.txt");

    // declare a playlists variable to hold all the playlists and their songs
    var playlists = new List<Playlist>();

    // Loop through all the playlists (each line in the text file represents a playlist)
    for (int plIdx = 0; plIdx < lines.Length; plIdx++)
    {
        // split in order to fins all the MP3 songs
        string[] res = lines[plIdx].Split(';');

        // create a new playlist (its name is passed into the constructor)
        var playlist = new Playlist(res[0]);

        // loop the songs (starting from index 1 since index=0 is the playlist name)
        for (int songIdx = 1; songIdx < res.Length; songIdx++)
        {
            // Add to the playlist each song
            playlist.Add(new MP3(res[songIdx]));
        }
        playlists.Add(playlist);
    }

    return playlists;
}

// Play list class containing all the MP3 songs (each line in text file)
class Playlist
{
    public List<MP3> SongList { get; private set; }
    public string Name { get; private set; }

    public Playlist(string name)
    {
        Name = name;
        SongList = new List<MP3>();
    }

    public void Add(MP3 mp3)
    {
        SongList.Add(mp3);
    }
}

// MP3 Song class
class MP3
{
    public string Location { get; private set; }
    public MP3(string location)
    {
        Location = location;
    }
}

这是播放列表变量在填充playlists.txt文件后的样子:

enter image description here

执行以下步骤以使其正常工作。一旦工作,您将获得一个参考,以便您可以简单地将其合并到现有项目中。

创建新的控制台应用程序 你的课程:

class Program
{
    static void Main(string[] args)
    {
        // FileReader is the class that contains the method that read from the playlists.txt file
        var filereader = new FileReader();
        var playlists = filereader.GetPlayLists();
    }        
}

创建一个FileReader类并将GetPlayLists方法放入

class FileReader
{
    // This method read the file and populate a List of Playlist.
    // Each PlayList contain a list of MP3 songs
    public List<Playlist> GetPlayLists()
    {
        // .....
    }           
}

将其他类放入PlayList和MP3。

现在你应该能够毫无问题地运行它。

答案 3 :(得分:0)

试试这个,你不应该在内循环中初始化currentPlayList,因为它会在每次循环时重新初始化

$scope.saveData = function(tbaleName) {$scope.tablelist.push(tableName)}

你的外部循环实际上运行了不止一次,但是因为你重新初始化了播放列表,所以在外部循环结束时持久存在的唯一状态是最后一次迭代,这给出了只运行一次的外观。 p>