【发布时间】:2016-04-16 21:14:01
【问题描述】:
我有一个包含 7 列的二维数组,我想按第一列搜索它。第一列是一年,第二列是一个月,然后是每个月的数据。这是一个例子。
2012 January 1 12 14 6 7
2012 February 7 8 19 2 8
2012 March 5 45 14 85 70
2012 April 8 23 17 89 67
因此,从 1980 年到 2015 年有好几年,每一年有 12 个月。然后,我想按年份搜索 2D 数组,以便一切正常。几乎就像对记录进行排序一样。我该怎么做呢?
谢谢
编辑:这是一个控制台应用程序
编辑:这里是有问题的二维数组;
namespace WeatherDatabaseBeta
{
class Program
{
//creates string arrays using the text files shown
public static string[] years = File.ReadAllLines(@"../../bin/Debug/WS1/Year.txt");
public static string[] months = File.ReadAllLines(@"../../bin/Debug/WS1/Month.txt");
public static string[] afs = File.ReadAllLines(@"../../bin/Debug/WS1/WS1_AF.txt");
public static string[] rains = File.ReadAllLines(@"../../bin/Debug/WS1/WS1_Rain.txt");
public static string[] suns = File.ReadAllLines(@"../../bin/Debug/WS1/WS1_Sun.txt");
public static string[] tmaxs = File.ReadAllLines(@"../../bin/Debug/WS1/WS1_TMax.txt");
public static string[] tmins = File.ReadAllLines(@"../../bin/Debug/WS1/WS1_TMin.txt");
static void Main(string[] args)
{
//changes console text to green
Console.ForegroundColor = ConsoleColor.Green;
//runs Forge_Array
Forge_Array();
Console.ReadKey();
}
public static void Forge_Array()
{
//sets the number of rows in the 2D array to the length of the years array
int arrayRows = years.Length;
//sets the number of columns to 7
int arrayColumns = 7;
//creates new 2D string array. the size uses the values prior to this
string[,] weather1 = new string [arrayRows, arrayColumns];
for(int i = 0; i < arrayRows; i++)
{
//fills the 2D array using the values from the previous arrays
//each array is used as a column in the 2D array
weather1[i, 0] = years[i];
weather1[i, 1] = months[i];
weather1[i, 2] = afs[i];
weather1[i, 3] = rains[i];
weather1[i, 4] = suns[i];
weather1[i, 5] = tmaxs[i];
weather1[i, 6] = tmins[i];
}
//creates strings for the titles of all of the columns in the array
string ytxt = "Year";
string mtxt = "Month";
string atxt = "Air Frost";
string rtxt = "Rain";
string stxt = "Sun";
string matxt = "Max Temp";
string mitxt = "Min Temp";
//gets the lenght of the 2D array
int rowLength = weather1.GetLength(0);
//writes out the titles for the columns for the array
Console.WriteLine("===============================================================================");
Console.WriteLine("| {0 , -4} || {1, -10} || {2, -9} || {3, -6} || {4, -6} || {5, -6} || {6, -6} |", ytxt, mtxt, atxt, rtxt, stxt, matxt, mitxt);
Console.WriteLine("===============================================================================");
//goes through the array and displays everything in the format shown below
for (int i = 0; i < rowLength; i++)
{
Console.WriteLine("| {0, -4} || {1, -10} || {2, -9} || {3, -6} || {4, -6} || {5, -8} || {6, -8} |", weather1[i, 0], weather1[i, 1], weather1[i, 2], weather1[i, 3], weather1[i, 4], weather1[i, 5], weather1[i, 6]);
}
Console.WriteLine("===============================================================================");
Console.ReadKey();
}
}
【问题讨论】:
-
你的意思是排序而不是搜索?
-
不,已经按年份排序了。我希望能够输入一年,然后显示所有 12 行,如果有意义的话。
-
这个问题不清楚。您提到您希望它“保持秩序”,但您也只想搜索。搜索不应该以任何方式改变排序,你为什么认为它会?你能更好地解释你的问题吗?
-
您能否添加一些代码,以便我们可以根据 C# 了解您是如何定义二维数组的?这可能会让人们给出一个快速的答案..
-
好吧,假设我输入了 1995 年。然后我希望它显示第一列中包含 1995 的行,然后在开始时显示包含 1995 的行中的数据。因此,搜索 1995 只会显示第一列中包含 1995 的行。编辑:justb 添加了二维数组的代码
标签: c# arrays multidimensional-array binary-search