为什么当我在列表mapsarray上循环时,它's向文本文件写入相同的2d int数组
本文关键字:文件 文本 int 数组 2d 列表 mapsarray 循环 为什么 | 更新日期: 2023-09-27 18:17:57
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();
}
在第一部分中,我正在循环位图文件,并将每个位图文件制作2d int数组,并将数组添加到List中所以在第一部分的末尾,mapsarray包含两个项目/索引,每个索引都有一个2d int数组。然后循环mapsarray。GetLength 0和1,并将值写入文本文件。
但是最后在文本文件中,我看到了相同的2d int数组:
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,},
};
我检查了一个断点MapsArrays包含两个项目,在每一个不同的2d int数组。那么为什么在文本文件中我得到相同的2d int数组?
我猜这部分有问题,我在这里做循环:
for (int k = 0; k < MapsArrays[i].GetLength(0); k++)
{
w.Write(" {");
for (int l = 0; l < MapsArrays[i].GetLength(1); l++)
{
您没有在写作循环中设置ret
,所以在这一行
var val = ret[k, l];
你总是指的是你在上面创建的最后一个ret