【问题标题】:How do I binary search a 2D array but keep the rows in the same place?如何对二维数组进行二进制搜索,但将行保持在同一个位置?
【发布时间】: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


【解决方案1】:

如果是关于实现二进制搜索的作业,这不是答案。

在示例中,您可以看到如何非常轻松地过滤数据集。

但是,如果您想让您的生活更轻松地使用此类数据集,我建议使用面向对象的方法(我会打印这些横幅,但抱歉没有更多时间了)(示例编译,因此您应该能够复制它运行并使用它):

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SOSorting
{
    internal class Program
    {
        public static string[] years = { "1990", "1990", "1990", "1991", "1991", "1991", "1991", "1992", "1992" };// File.ReadAllLines(@"../../bin/Debug/WS1/Year.txt");

        public static string[] months = { "January", "February", "March", "January", "February", "March", "April", "January", "February" };// File.ReadAllLines(@"../../bin/Debug/WS1/Month.txt");

        public static string[] afs = { "5", "6", "7", "4", "2", "3", "7", "8", "1" }; //File.ReadAllLines(@"../../bin/Debug/WS1/WS1_AF.txt");

        public static string[] rains = { "50", "30", "40", "40", "25", "60", "30", "21", "21" };// File.ReadAllLines(@"../../bin/Debug/WS1/WS1_Rain.txt");

        public static string[] suns = { "85", "80", "81", "82", "80", "89", "90", "92", "75" };//File.ReadAllLines(@"../../bin/Debug/WS1/WS1_Sun.txt");

        public static string[] tmaxs = { "70", "60", "65", "65", "24", "24", "20", "75", "70" };//File.ReadAllLines(@"../../bin/Debug/WS1/WS1_TMax.txt");

        public static string[] tmins = { "10", "9", "8", "9", "10", "12", "12", "13", "21" }; //File.ReadAllLines(@"../../bin/Debug/WS1/WS1_TMin.txt");

        private 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];

            //create list of weather objects
            List<Weather> weatherList = new List<Weather>();

            for (int i = 0; i < arrayRows; i++)
            {
                weatherList.Add(new Weather(years[i], months[i], afs[i], rains[i], suns[i], tmaxs[i], 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("===============================================================================");
            Console.WriteLine("Filtered to specific year");
            foreach (var weather in weatherList.Where(x => x.Date.Year == 1991)) // of course you can parametrize it
            {
                Console.WriteLine(weather);
            }

            Console.WriteLine("===============================================================================");

            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("===============================================================================");
            Console.WriteLine("Not filtered");
            foreach (var weather in weatherList)
            {
                Console.WriteLine(weather);
            }

            Console.WriteLine("===============================================================================");

            Console.WriteLine("===============================================================================");

            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("===============================================================================");
            Console.WriteLine("Filtered by suns and year");
            foreach (var weather in weatherList.Where(x => x.Suns > 89 && x.Date.Year == 1991))
            {
                Console.WriteLine(weather);
            }

            Console.WriteLine("===============================================================================");
            Console.ReadKey();
        }
    }

    public class Weather
    {
        private DateTime _date;
        private readonly string _year;
        private readonly string _month;
        private int _afs;
        private int _rains;
        private int _suns;
        private int _tmaxs;
        private int _tmins;

        public Weather(string year, string month, string afs, string rains, string suns, string tmaxs, string tmins)
        {
            if (tmins == null)
                throw new ArgumentNullException("tmins");
            if (tmaxs == null)
                throw new ArgumentNullException("tmaxs");
            if (suns == null)
                throw new ArgumentNullException("suns");
            if (rains == null)
                throw new ArgumentNullException("rains");
            if (afs == null)
                throw new ArgumentNullException("afs");
            if (month == null)
                throw new ArgumentNullException("month");
            if (year == null)
                throw new ArgumentNullException("year");

            //Have to use dictionary for quick translation
            int monthNumber = _months[month];

            //Date time will indicate first day of month but we care only about month and year anyway
            _date = new DateTime(int.Parse(year), monthNumber, 1);
            _year = year;
            _month = month;
            _afs = int.Parse(afs);
            _rains = int.Parse(rains);
            _suns = int.Parse(suns);
            _tmaxs = int.Parse(tmaxs);
            _tmins = int.Parse(tmins);
        }

        public DateTime Date { get { return _date; } }
        public int Afs { get { return _afs; } }
        public int Rains { get { return _rains; } }
        public int Suns { get { return _suns; } }
        public int Tmaxs { get { return _tmaxs; } }
        public int Tmins { get { return _tmins; } }

        private Dictionary<string, int> _months = new Dictionary<string, int>()
                {
                                { "January", 1},
                                { "February", 2},
                                { "March", 3},
                                { "April", 4},
                                { "May", 5},
                                { "June", 6},
                                { "July", 7},
                                { "August",8},
                                { "September", 9},
                                { "October", 10},
                                { "November", 11},
                                { "December", 12}
                };

        public override string ToString()
        {
            return String.Format("| {0, -4} || {1, -10} || {2, -9} || {3, -6} || {4, -6} || {5, -8} || {6, -8} |", _year, _month, _afs, _rains, _suns, _tmaxs, _tmins);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-03
    • 2013-09-29
    • 2018-03-31
    • 2022-01-22
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多