简体   繁体   English

如何从这个以逗号分隔的单行文件制作2D数组?

[英]How can I make a 2D array from this one-line, comma delimited file?

I am trying to figure out how to turn the following, one-line CSV file into a 30x30 2D array. 我试图弄清楚如何将以下单行CSV文件转换为30x30 2D阵列。

http://pastebin.com/8NP7s7N0 http://pastebin.com/8NP7s7N0

I've tried looking it up myself, but I just can't seem to wrap my brain around the concept of multidimensional arrays, and I don't know how to turn a one-line file like this into an array of a specified size. 我已经尝试过自己查找,但我似乎无法将我的大脑包围在多维数组的概念中,而且我不知道如何将这样的单行文件转换为指定大小的数组。

I want to be able to make an array that would look like this when printed: 我希望能够在打印时创建一个看起来像这样的数组:

0,0 = 2 0,0 = 2

0,1 = 2 0,1 = 2

All the way to 30,30. 一直到30,30。

Most of the numbers in the CSV are indeed 2's, but some are 1s. CSV中的大多数数字确实是2,但有些数字是1。 The difference is very important though. 但差异非常重要。 I am trying to make collision detection for a game, and this CSV file is the map. 我正在尝试对游戏进行碰撞检测,这个CSV文件就是地图。 All I need left is how to create this array - leave the rest to me. 我需要的只是如何创建这个数组 - 剩下的就是我。 :) :)

Thank you very much to all, have a nice day. 非常感谢大家,祝你有愉快的一天。

well, first you need to get the numbers... 好吧,首先你需要得到数字......

var numbers = Read_File_As_String().Split(new char[',']).Select(n => int.Parse(n)).ToList();

then, you need to build your array 那么,你需要构建你的数组

const int ROWS = 30;
const int COLS = 30;

var result = new int[ROWS, COLS];

for (int row = 0; row < ROWS; row++)
    for (int col = 0; col < COLS; col++)
        result[row, col] = numbers[(row * COLS) + col];

Assuming your file is 900 elements first you need to read it in.. 假设您的文件首先是900个元素,您需要阅读它...

something along the lines of 一些东西

line = myStreamReader.readLine().Split(',') .. then in John U's example, value would be the next index in this array called line line = myStreamReader.readLine().Split(',') ..然后在John U的例子中, value将是这个数组中名为line的下一个索引

I'll let you work out whats missing from my example :P 我会让你弄清楚我的例子中遗漏了什么:P

for(row=0;row<30;row++)
{
    for(col=0;col<30;col++)
    {
       array[row][col] = value;
    }
}

Value would need to be moved along to point to the next thing each time, but I'm sure you can figure that out. 每次都需要移动价值以指向下一件事,但我相信你可以解决这个问题。

Edited to add: If it's a map it might be easier to store it as an array in the first place. 编辑添加:如果它是一个地图,它可能更容易将它作为一个数组存储在一起。

Since you asked about the concept of multi-dimensional arrays, here are some useful ways of thinking about arrays. 既然你问过多维数组的概念,这里有一些思考数组的有用方法。 Please note these are analogies, meant to help you visualize them. 请注意这些是类比,旨在帮助您可视化它们。

Think of a 1D array as a list of items (not in the programming sense of list!). 将一维数组视为项目列表(不是编程意义上的列表!)。

Think of a 2D array as a table (again, not in the programming sense!). 将2D数组视为一个表(同样,不是编程意义上的!)。 In a table (like a spreadsheet) you have rows and columns, and each dimension in your array accesses one of these. 在表格(如电子表格)中,您有行和列,并且数组中的每个维度都会访问其中一个维度。

For higher dimensional arrays, it may help to think geometrically. 对于更高维数的阵列,可能有助于几何思考。 For instance, you can think of 3D arrays as 3-dimensional points in space, and 4D arrays as 4-dimensional points in space-time. 例如,您可以将3D阵列视为空间中的三维点,将4D阵列视为时空中的四维点。

So if you have a single CSV file, start off by conceptualizing how this would be re-structured as a table. 因此,如果您有一个CSV文件,请首先概念化如何将其重新组织为表格。 Once you have that, you have a pretty straight-forward mapping to the array. 一旦你有了这个,你有一个非常直接的映射到数组。

This should be a complete example using a 5 x 5 grid. 这应该是使用5 x 5网格的完整示例。 I've tried it and seems to work as expected: 我已经尝试过,似乎按预期工作:

namespace ConsoleApplication1
{
    using System;

    class Program
    {
        const int MapRows = 5;
        const int MapColumns = 5;

        static void Main(string[] args)
        {
            // Create map and the raw data (from file)
            var map = new int[MapRows, MapColumns];
            string rawMapData = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25";
            string[] splitData = rawMapData.Split(',');
            int index = 0;

            // Loop through data
            for (int row = 0; row < MapRows; row++)
            {
                for (int column = 0; column < MapColumns; column++)
                {
                    // Store in map and show some debug
                    map[row, column] = int.Parse(splitData[index++]);
                    Console.WriteLine(string.Format("{0},{1} = {2}", row, column, map[row, column]));
                }
            }

            // Wait for user to read
            Console.ReadKey();
        }
    }
}

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

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