如何显示我的新阵列? [英] How do I display my new array?
问题描述
大家好,
我正在处理一个字符串,其中包含2行,字符为1和0.
我已经放置了字符串到字符串数组中提取行。
理想情况下,我想循环遍历此数组并确定当前元素是1还是0.如果是1我想将一个'#'字符放入另一个char类型的数组中。如果它是0我想放置一个'X'所以当我循环这个新数组时我得到一个类似的数组但是有了新的字符。
对于某些人来说原因,我的字符阵列不打印到屏幕,有人可以指出我正确的方向吗?
我尝试过:
Hi all,
I am working a string consisting of 2 lines with characters 1 and 0.
I have placed the string into a string array to extract the lines.
Ideally, I would like to loop through this array and determine if the current element is 1 or 0. if it is a 1 I would like to place a '#' character into another array of type char. if it is a 0 I would like to place a 'X' so when i loop this new array i get a similar array but with the new characters.
For some reason, my char array doesn't print to screen, Could someone please point me in the right direction?
What I have tried:
class MainClass
{
const string maze = @"
1 1 1 1 1
1 0 0 0 1";
public static void Main(string[] args)
{
//Display(GetMazeArray(maze));
string[] lines = maze.Split(new char[] { '\n' },
StringSplitOptions.RemoveEmptyEntries);
char[][] characters = new char[2][];
characters[0] = lines[0].ToCharArray();
characters[1] = lines[1].ToCharArray();
// new array to store the new characters
char[][] newArr = new char[2][];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
if(characters[i][j] == '1')
{
newArr[i][j] = '#';
}
else if(characters[i][j] == '0')
{
newArr[i][j] = 'X';
}
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
Console.WriteLine(newArr[i][j]);
}
}
}
}
推荐答案
你的问题有三个当前代码:
newArr [0]
和newArr [1]
仍然是null
,因为您尚未将空数组分配给这些值。这样做:
There are three problems with your current code:
newArr[0]
andnewArr[1]
are stillnull
because you didn't assign empty arrays to these values yet. Do this:
// new array to store the new characters
char[][] newArr = new char[2][];
newArr[0] = new char[characters[0].Length];
newArr[1] = new char[characters[1].Length];
.ToCharArray()
还会给你一堆你不想要的字符
数组中的空格,因为你只有0和1,在使用ToCharArray之前先删除空格:
.ToCharArray()
will also give you a bunch of spaces in your characters
array, which you don't want because you only are about 0 and 1. Remove the spaces first before using ToCharArray:char[][] characters = new char[2][];
characters[0] = lines[0].Replace(" ", "").ToCharArray();
characters[1] = lines[1].Replace(" ", "").ToCharArray();
j<你的for循环中有4
;但你的characters [i]数组有5个元素(最后一个索引是4)所以你想要 j< = 4
或 j< 5
。
j < 4
in your for loops; but your characters[i] arrays have 5 elements (the last index is 4) so you want j <= 4
or j < 5
.using System;
class MainClass
{
const string maze = @"
1 1 1 1 1
1 0 0 0 1";
public static void Main(string[] args)
{
//Display(GetMazeArray(maze));
string[] lines = maze.Split(new char[] { '\n' },
StringSplitOptions.RemoveEmptyEntries);
char[][] characters = new char[2][];
characters[0] = lines[0].Replace(" ", "").ToCharArray();
characters[1] = lines[1].Replace(" ", "").ToCharArray();
// new array to store the new characters
char[][] newArr = new char[2][];
newArr[0] = new char[characters[0].Length];
newArr[1] = new char[characters[1].Length];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
if(characters[i][j] == '1')
{
newArr[i][j] = '#';
}
else if(characters[i][j] == '0')
{
newArr[i][j] = 'X';
}
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
Console.WriteLine(newArr[i][j]);
}
}
}
}
你可以减少'字符串创建和数组操作的数量像这样:
You can reduce the amount of 'String creation and array manipulation like this:
private char[] splitary = new char[] {'\n','\r'};
// in some method:
StringBuilder sb = new StringBuilder(maze);
sb.Replace(" ", String.Empty);
sb.Replace('0', 'X');
sb.Replace('1', '#');
var trimmed = sb.ToString().Split(splitary, StringSplitOptions.RemoveEmptyEntries);
char[][] ary = new char[][]
{
(trimmed[0].ToCharArray()),
(trimmed[1].ToCharArray())
};
但是,请记住,使用自动数组初始化程序意味着你不能指定'长度。
But, keep in mind that using an automatic Array initializer means you cannot specify the 'Length.
这篇关于如何显示我的新阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!