C# 二维数组上的File.ReadAllLines方法
我将要创建一个媒体播放器应用程序,它将处理多个播放列表,当然还有多首歌曲。我已经为播放列表名称创建了一个字符串数组,它们存储在一个文本文件中。我使用C# 二维数组上的File.ReadAllLines方法,c#,xna,xna-4.0,C#,Xna,Xna 4.0,我将要创建一个媒体播放器应用程序,它将处理多个播放列表,当然还有多首歌曲。我已经为播放列表名称创建了一个字符串数组,它们存储在一个文本文件中。我使用文件.ReadAllLines方法加载它们,如下所示: 1darray = File.ReadAllLines("textfilename.txt"); 每个播放列表都有自己的ID,我有一个变量来存储播放列表的数量。 我想用同样的方法读取歌曲的路径,但它需要一个2D数组 2Darray[playlistID, songID] 与第一种方法一样,我
文件.ReadAllLines
方法加载它们,如下所示:
1darray = File.ReadAllLines("textfilename.txt");
每个播放列表都有自己的ID,我有一个变量来存储播放列表的数量。
我想用同样的方法读取歌曲的路径,但它需要一个2D数组
2Darray[playlistID, songID]
与第一种方法一样,我不必指定数组的每个元素,现在我需要指定playlid,并让File.ReadAllLines方法填充songID元素,但它的工作方式不是这样的:
2Darray[playlistID] = File.ReadAllLines("textfilename.txt");
int[][] jaggedArray = new int[4][];
[0, 1]
[5, 3, 2, 1]
[]
[9, 8, 7, 6, 5, 4, 2]
int[] element = jaggedArray[1];
int value = jaggedArray[1][2]; // equivalent to element[2]
int[,] multidimArray = new int[4, 4];
[0, 1, 2, 3,
5, 4, 3, 2,
0, 0, 0, 0,
9, 8, 7, 4]
int value = multidimArray[3, 2];
它表示[]内的索引数量错误;预期2
如何处理
希望你能理解我糟糕的英语 扫描目录以查找播放列表文件: 然后枚举找到的文件:
foreach (string filename in filenames)
{
// ...
}
使用每个文件名作为播放列表ID:
Dictionary<string, string[]> playlists;
获取特定播放列表的条目(此处为“dupestep.pl”的第四个):
从现在起,你应该能够实现任何你需要的
要提取播放列表列表,您可以提取键列表以将索引转换为文件名,反之亦然:
playlists.Keys
因此,基本上您可以使用字典>,将所有播放列表与各自的歌曲进行映射。当然,使用类和关系可以更好地做到这一点,但是为了保持简单,我们将使用第一个示例:
//get all playlists
string[] playLists = File.ReadAllLines("playLists.txt");
//storage
Dictionary<string, List<string>> playListsToSongsMap = new Dictionary<string, List<string>>();
//iterate the readed playlists
foreach (string playlist in playLists)
{
if (!playListsToSongsMap.ContainsKey(playlist))
{
playListsToSongsMap[playlist] = new List<string>();
}
//add songs to each playlist
playListsToSongsMap[playlist].AddRange(File.ReadLines("playListWithsongsPath"));
}
//get all playlits
List<string> allPlayLists = playListsToSongsMap.Keys.ToList();
//get all songs in a playlist
List<string> songsInPlaylist = playListsToSongsMap[allPlayLists[0]];
//获取所有播放列表
string[]playLists=File.ReadAllLines(“playLists.txt”);
//储藏
Dictionary playlistostosongsmap=新字典();
//迭代读取的播放列表
foreach(播放列表中的字符串播放列表)
{
如果(!playlistostosongsmap.ContainsKey(播放列表))
{
playlistosongsmap[playlist]=新列表();
}
//将歌曲添加到每个播放列表中
playlistoSongsMap[playlist].AddRange(File.ReadLines(“playlistoSongsPath”);
}
//得到所有的playlit
List allPlayLists=playlistostosongsmap.Keys.ToList();
//获取播放列表中的所有歌曲
List songsInPlaylist=播放列表歌曲映射[所有播放列表[0]];
虽然很多人都提出了解决问题的替代方案,但还没有人解释最初错误的含义。您需要了解的是,C#区分了所谓的交错数组和多维数组
锯齿状数组本质上是数组的数组。声明一个如下所示:
2Darray[playlistID] = File.ReadAllLines("textfilename.txt");
int[][] jaggedArray = new int[4][];
[0, 1]
[5, 3, 2, 1]
[]
[9, 8, 7, 6, 5, 4, 2]
int[] element = jaggedArray[1];
int value = jaggedArray[1][2]; // equivalent to element[2]
int[,] multidimArray = new int[4, 4];
[0, 1, 2, 3,
5, 4, 3, 2,
0, 0, 0, 0,
9, 8, 7, 4]
int value = multidimArray[3, 2];
这将创建一个包含四个元素的数组,这四个元素中的每一个都可以是任意大小的int
数组。这就是为什么这样的数组被称为“锯齿状”:一些元素可能比其他元素大。你可以想象它看起来像这样:
2Darray[playlistID] = File.ReadAllLines("textfilename.txt");
int[][] jaggedArray = new int[4][];
[0, 1]
[5, 3, 2, 1]
[]
[9, 8, 7, 6, 5, 4, 2]
int[] element = jaggedArray[1];
int value = jaggedArray[1][2]; // equivalent to element[2]
int[,] multidimArray = new int[4, 4];
[0, 1, 2, 3,
5, 4, 3, 2,
0, 0, 0, 0,
9, 8, 7, 4]
int value = multidimArray[3, 2];
注意锯齿状的边缘
根据声明,锯齿状数组的访问方式如下:
2Darray[playlistID] = File.ReadAllLines("textfilename.txt");
int[][] jaggedArray = new int[4][];
[0, 1]
[5, 3, 2, 1]
[]
[9, 8, 7, 6, 5, 4, 2]
int[] element = jaggedArray[1];
int value = jaggedArray[1][2]; // equivalent to element[2]
int[,] multidimArray = new int[4, 4];
[0, 1, 2, 3,
5, 4, 3, 2,
0, 0, 0, 0,
9, 8, 7, 4]
int value = multidimArray[3, 2];
多维数组的行为不同。多维数组声明如下:
2Darray[playlistID] = File.ReadAllLines("textfilename.txt");
int[][] jaggedArray = new int[4][];
[0, 1]
[5, 3, 2, 1]
[]
[9, 8, 7, 6, 5, 4, 2]
int[] element = jaggedArray[1];
int value = jaggedArray[1][2]; // equivalent to element[2]
int[,] multidimArray = new int[4, 4];
[0, 1, 2, 3,
5, 4, 3, 2,
0, 0, 0, 0,
9, 8, 7, 4]
int value = multidimArray[3, 2];
与锯齿状数组不同,多维数组指定两个维度的大小。这样一个数组中的所有元素都具有相同的大小。你可以想象它看起来更像这样:
2Darray[playlistID] = File.ReadAllLines("textfilename.txt");
int[][] jaggedArray = new int[4][];
[0, 1]
[5, 3, 2, 1]
[]
[9, 8, 7, 6, 5, 4, 2]
int[] element = jaggedArray[1];
int value = jaggedArray[1][2]; // equivalent to element[2]
int[,] multidimArray = new int[4, 4];
[0, 1, 2, 3,
5, 4, 3, 2,
0, 0, 0, 0,
9, 8, 7, 4]
int value = multidimArray[3, 2];
多维数组的访问方式也必须有所不同。与交错数组(实际上是数组的数组)不同,多维数组被视为单个内聚单元。这样做:
int[] element = multidimArray[3];
…没有任何意义,因为多维数组不是由更小的数组组成的。您只能访问数组中的单个值,操作如下:
2Darray[playlistID] = File.ReadAllLines("textfilename.txt");
int[][] jaggedArray = new int[4][];
[0, 1]
[5, 3, 2, 1]
[]
[9, 8, 7, 6, 5, 4, 2]
int[] element = jaggedArray[1];
int value = jaggedArray[1][2]; // equivalent to element[2]
int[,] multidimArray = new int[4, 4];
[0, 1, 2, 3,
5, 4, 3, 2,
0, 0, 0, 0,
9, 8, 7, 4]
int value = multidimArray[3, 2];
请注意,必须在单个[]
运算符中指定两个索引。可以说,这些是您想要的值的“坐标”
这就是你错误的根源。您有一个多维数组,您试图访问它的单个数组元素,这没有任何意义。要执行最初尝试执行的操作,需要使用锯齿数组 代码胜过文字。。向我们展示正确的代码以演示您的问题。