简体   繁体   English

具有许多值的二维数组

[英]two-dimensional array with many values

I'm trying to convert the data from this table in to a two-dimensional array. 我正在尝试将此表中的数据转换为二维数组。 降序

This is the code I have so far 这是我到目前为止的代码

int[,] distanceArray = new int[10, 10];
        int[] chicago = new int[] { 1004, 921, 1230, 272, 2716, 860, 1307, 2820, 2887 };
        int[] dallas = new int[] { 1753, 1780, 1899, 2570, 1858, 1494, 2734, 571 };
        int[] lasVegas = new int[] { 2752, 2048, 1343, 1732, 2523, 3447, 1099 };
        int[] losAngeles = new int[] { 3017, 1397, 517, 2251, 1278, 1057 };
        int[] miami = new int[] { 1520, 919, 1435, 1322, 2646 };
        int[] newOrleans = new int[] { 1507, 515, 2234, 2420 };
        int[] toronto = new int[] { 609, 2176, 1307 };
        int[] vancouver = new int[] { 3155, 709 };
        int washingtonDC = 448;

        int counter = 10;
        for (int i = 0; i < counter; i++)
        {
            for (int y = 0; y < counter; y++)
            {
                if (counter == 9)
                {
                    distanceArray[(y + 1), y] = chicago[y];
                    distanceArray[y, (y + 1)] = chicago[y];
                }
                if (counter == 8)
                {
                    distanceArray[(y + 2), y] = dallas[y];
                    distanceArray[y, (y + 2)] = dallas[y];
                }
                if (counter == 7)
                {
                    distanceArray[(y + 3), y] = lasVegas[y];
                    distanceArray[y, (y + 3)] = lasVegas[y];
                }
                if (counter == 6)
                {
                    distanceArray[(y + 4), y] = losAngeles[y];
                    distanceArray[y, (y + 4)] = losAngeles[y];
                }
                if (counter == 5)
                {
                    distanceArray[(y + 5), y] = miami[y];
                    distanceArray[y, (y + 5)] = miami[y];
                }
                if (counter == 4)
                {
                    distanceArray[(y + 6), y] = newOrleans[y];
                    distanceArray[y, (y + 6)] = newOrleans[y];
                }
                if (counter == 3)
                {
                    distanceArray[(y + 7), y] = toronto[y];
                    distanceArray[y, (y + 7)] = toronto[y];
                }
                if (counter == 2)
                {
                    distanceArray[(y + 8), y] = vancouver[y];
                    distanceArray[y, (y + 8)] = vancouver[y];
                }
                if (counter == 1)
                {
                    distanceArray[(y + 9), y] = washingtonDC;
                    distanceArray[y, (y + 9)] = washingtonDC;
                }

            }

            counter--;
        }


        for (int i = 0; i < 10; i++)
        {
            for (int y = 0; y < 10; y++)
            {
                Console.Write(distanceArray[i, y] + "\t");
            }
            Console.Write("\n");
        }
        Console.ReadKey();

But I'm getting this output 但我得到了这个输出 出

For some reason the final values are not being applied. 由于某种原因,最终值未被应用。 What am I doing wrong? 我究竟做错了什么? Is there a more efficient way to achieve this goal? 有没有更有效的方法来实现这一目标?

Use an initializer for every value in the grid. 对网格中的每个值使用初始值设定项。 Yes, there is symmetry in the grid of distances, but you asked for an efficient method! 是的,距离网格中存在对称性,但您要求一种有效的方法!

int[,] distanceArray = new int[10, 10]
{
  {0, 1004, 1753, 2752, 3017, 1520, 1507, 609, 3155, 448}, // Boston
  {1004, 0, 921, 1780, ... etc},                           // Chicago
   ...
  {448, 709,1307,2420,2646,1057,1099,571,2887,0}           // Washington DC
};

It's not clear to me why you are initializing the array the way you are. 我不清楚你为什么要按照你的方式初始化数组。 The values in each city's array do not appear to relate to the values you show in the table. 每个城市数组中的值似乎与您在表中显示的值无关。 For example, the third value in the chicago array appears to be the distance between Dallas and Las Vegas (ie not between Chicago and some other city). 例如, chicago阵列中的第三个值似乎是达拉斯和拉斯维加斯之间的距离(即不在芝加哥和其他一些城市之间)。 It's also not clear to me why if you want a separate distance array, you don't just hard-code the values for that . 为什么如果你想要一个单独这也是我不清楚distance数组,你不只是硬编码值。 Nor is it clear to me why you have the counter variable in your nested loops. 我也不清楚为什么你的嵌套循环中有counter变量。

In other words, there's very little about the code you posted that makes sense to me. 换句话说,你发布的代码对我来说很有意义。 However, the basic goal seems clear enough. 但是,基本目标似乎很清楚。 Here's how I'd do it (showing just four cities for the example…I assume you can take the basic example and extend it to all the cities you have): 以下是我的方法(仅显示四个城市的示例......我假设您可以采用基本示例并将其扩展到您拥有的所有城市):

enum City
{
    Boston,
    Chicago,
    Dallas,
    LasVegas,
}

static class CityDistance
{
    private static int[][] _distances =
    {
        new [] { 1004, 1753, 2752 },
        new [] { 921, 1780 },
        new [] { 1230 },
    };

    public static int GetDistance(City city1, City city2)
    {
        if (city1 == city2)
        {
            return 0;
        }

        if (city1 > city2)
        {
            City cityT = city1;
            city1 = city2;
            city2 = cityT;
        }

        int cityIndex1 = (int)city1, cityIndex2 = (int)city2 - (cityIndex1 + 1);

        return _distances[cityIndex1][cityIndex2];
    }
}

class Program
{
    static void Main(string[] args)
    {
        foreach (City city1 in Enum.GetValues(typeof(City)))
        {
            foreach (City city2 in Enum.GetValues(typeof(City)))
            {
                Console.Write("{0,4}   ", CityDistance.GetDistance(city1, city2));
            }
            Console.WriteLine();
        }
    }
}

Notes: 笔记:

  • I introduce the use of an enum type to represent the actual cities. 我介绍了使用enum类型来表示实际的城市。 This is much better than relying on hard-coded integer values. 这比依赖硬编码的整数值要好得多。
  • I have abstracted the logic into a CityDistances class, where the data can be encapsulated, and a method that can map the basic query of "what's the distance between a pair of cities" into the logic required to access the stored data, without requiring the caller to care about how that data is stored. 我已经将逻辑抽象为CityDistances类,其中数据可以被封装,并且可以将基本查询“一对城市之间的距离”映射到访问存储数据所需的逻辑,而不需要调用者关心如何存储数据。

With this approach, you can try different data representations, and the code that calls the GetDistance() method doesn't have to change. 使用此方法,您可以尝试不同的数据表示,并且调用GetDistance()方法的代码不必更改。 In this case, I've stored the data somewhat similarly to how it seems you are trying to do in your example, ie without duplicating data. 在这种情况下,我已经存储的数据有些类似于怎么好像你想在你的例子做的,即不复制数据。 This means the GetDistance() method needs to handle the 0-length scenario as a special case, and needs to normalize the input city values, so that the symmetrical nature of the data can be used. 这意味着GetDistance()方法需要将0长度场景作为特殊情况处理,并且需要规范化输入城市值,以便可以使用数据的对称性。

Here is a complete example that generate the table given. 这是一个生成给定表的完整示例。 Problem is that the values of array mention in your code is less than the values mention in table. 问题是代码中提到的数组值小于表中提到的值。

  class Program
{
    public static int w = 0;
    public static int y = 1;

    static void Main(string[] args)
    {

        int[,] twodimention = new int[10, 10];
        int[] boston = new int[] { 0, 1004, 1753, 2752,3017 ,1520 , 1507, 609, 3155,448 };
        int[] chicago = new int[] { 1004, 0, 921, 1780, 2048, 1397, 919, 515, 2176,709 };
        int[] dallas = new int[] { 1753, 921, 0, 1230, 1399, 1343, 517, 1435,2234,1307 };
        int[] lasVegas = new int[] { 2752, 1708, 1230, 0, 272, 2570, 1732,2251,1322,2420 };
        int[] losAngeles = new int[] { 3017, 2048, 1399, 272,0, 2716,1858,2523,1278,2646 };
        int[] miami = new int[] { 1520, 1397, 1343, 2570, 2716,0,860,1494,3447,1057 };
        int[] newOrleans = new int[] { 1507, 919, 517, 1732,1858,860,0,1307,2734,1099 };
        int[] toronto = new int[] { 609, 515, 1435,2251,2523,1494,1307,0,2820,571 };
        int[] vancouver = new int[] { 3155,2176,2234,1322,1278,3447,2734,2820,0,2887 };
        int[] washington = new int[] { 448, 709, 1307, 2420, 2646, 1057, 1099, 571, 2887,0 };
        int x = 9;

        for (int row = 0; row <= x; row++)
        {
            for (int col = 0; col <=9; col++)
            {
                if (w == 0)
                {
                    if (col ==boston.Length - 1 || col < boston.Length - 1)
                    {
                        twodimention[row, col] = boston[col];

                    }
                }
                if (w==1)
                {
                    if(col==chicago.Length-1 || col<chicago.Length - 1)
                    {
                        twodimention[row, col] = chicago[col];

                    }
                }
                if (w == 2)
                {
                    if (col == dallas.Length - 1 || col < dallas.Length - 1)
                    {


                        twodimention[row, col] = dallas[col];
                    }
                }
                if (w == 3)
                {
                    if (col == lasVegas.Length - 1 || col < lasVegas.Length - 1)
                    {

                        twodimention[row, col] = lasVegas[col];
                    }
                }
                if (w == 4)
                {
                    if (col == losAngeles.Length - 1 || col < losAngeles.Length - 1)
                    {
                        twodimention[row, col] = losAngeles[col];
                    }
                }
                if (w == 5)
                {
                    if (col == miami.Length - 1 || col < miami.Length - 1)
                    {
                        twodimention[row, col] = miami[col];
                    }

                }
                if (w == 6)
                {
                    if (col == newOrleans.Length - 1 || col < newOrleans.Length - 1)
                    {
                        twodimention[row, col] = newOrleans[col];
                    }
                }
                if (w == 7)
                {
                    if (col == toronto.Length - 1 || col < toronto.Length - 1)
                    {
                        twodimention[row, col] = toronto[col];
                    }
                }
                if (w == 8)
                {
                    if (col == vancouver.Length - 1 || col < vancouver.Length - 1)
                    {
                        twodimention[row, col] = vancouver[col];
                    }
                }
                if (w == 9)
                {
                    if (col == washington.Length - 1 || col < washington.Length - 1)
                    {
                        twodimention[row, col] = washington[col];
                    }
                }

            }


            w = w + 1;
        }
        Console.WriteLine();

        for (int row = 0; row <= x; row++)
        {
            for (int col = 0; col <= 9; col++)
            {
           Console.Write(twodimention[row, col]+"\t");

            }



        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM