简体   繁体   中英

Why when i loop on the List MapsArrays it's writing to the text file the same 2d int array?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace ConvertBitmapToArray
{
    public partial class Form1 : Form
    {
        List<string> MapsFiles = new List<string>();
        List<string> NumbersArray = new List<string>();
        int[,] ret;
        int index = 0;
        string f = "";
        string textfile = @"C:\Temp\Gimp Maps\Mapstest.txt";
        string text1;

        public Form1()
        {
            InitializeComponent();

            if (File.Exists(textfile))
            {
                File.Delete(textfile);
            }
            String[] allfiles = System.IO.Directory.GetFiles(@"C:\Temp\Gimp Maps\Bitmaps", "*.*");
            string text = File.ReadAllText(@"C:\Temp\Gimp Maps\CreateMaps1.txt");
            text1 = File.ReadAllText(@"C:\Temp\Gimp Maps\CreateMaps2.txt");
            string firstTag = "using";
            string lastTag = "class Level";
            index = text.IndexOf(firstTag, 0);
            int g = text.IndexOf(lastTag, index);
            f = text.Substring(index, g + 22);
            CreateArray(allfiles, "map");
        }

        private void CreateArray(String[] bitmaps, String MapName)
        {
            StreamWriter w = new StreamWriter(textfile, true);
            Bitmap bmp;
            List<int[,]> MapsArrays = new List<int[,]>();
            for (int i = 0; i < bitmaps.Length; i++)
            {
                bmp = new Bitmap(bitmaps[i]);
                ret = new int[bmp.Width, bmp.Height];

                int whiteColor = 0;
                int blackColor = 0;
                for (int x = 0; x < bmp.Width; x++)
                {
                    for (int y = 0; y < bmp.Height; y++)
                    {
                        Color color = bmp.GetPixel(x, y);

                        if (color.ToArgb() == Color.White.ToArgb())
                        {
                            whiteColor++;
                            ret[x, y] = 0;
                        }

                        else
                        {
                            blackColor++;
                            ret[x, y] = 1;
                        }
                    }
                }
                MapsArrays.Add(ret);
            }
            for (int i = 0; i < MapsArrays.Count; i++)
            {
                if (i == 0)
                {
                    w.WriteLine(f);
                    w.WriteLine("int[,] " + MapName + " = new int[,]");
                }
                else
                {
                    w.WriteLine("int[,] " + MapName + i + " = new int[,]");
                }
                w.WriteLine("{");
                for (int k = 0; k < MapsArrays[i].GetLength(0); k++)
                {
                    w.Write(" {");
                    for (int l = 0; l < MapsArrays[i].GetLength(1); l++)
                    {
                        var val = ret[k, l];
                        w.Write(val + ",");
                    }
                    w.Write("},");
                    w.WriteLine(string.Empty);
                }
                w.WriteLine("};");
                w.WriteLine(string.Empty);
            }
            w.WriteLine(text1);
            w.Close();
        }

In the first part i'm looping over bitmap files and make 2d int array of each of them and add the arrays to a List So in the end of the first part MapsArrays contain two items/indexs in each index there is a 2d int array. Then i loop over the MapsArrays.GetLength 0 and 1 and write the values to a text file.

But in the end in the text file i see the same 2d int array:

int[,] map = new int[,]
{
 {0,0,0,1,1,1,1,0,},
 {0,0,0,1,0,0,1,0,},
 {0,0,0,1,1,0,1,0,},
 {1,1,0,0,1,0,1,0,},
 {0,1,0,0,1,0,1,0,},
 {0,1,1,1,1,0,1,0,},
 {0,0,0,1,1,0,1,1,},
 {0,0,0,0,0,0,0,0,},
};

int[,] map1 = new int[,]
{
 {0,0,0,1,1,1,1,0,},
 {0,0,0,1,0,0,1,0,},
 {0,0,0,1,1,0,1,0,},
 {1,1,0,0,1,0,1,0,},
 {0,1,0,0,1,0,1,0,},
 {0,1,1,1,1,0,1,0,},
 {0,0,0,1,1,0,1,1,},
 {0,0,0,0,0,0,0,0,},
};

And i checked with a breakpoint MapsArrays contain two items and in each one a different 2d int array. So why in the text file i get the same 2d int array ?

I guess it's something wrong with the part i'm doing the loops here:

for (int k = 0; k < MapsArrays[i].GetLength(0); k++)
                {
                    w.Write(" {");
                    for (int l = 0; l < MapsArrays[i].GetLength(1); l++)
                    {

You don't set ret in your writing loop, so in this line

var val = ret[k, l];

you are always referring to the last ret you created above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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