我似乎无法将字符数组转换为2D整数数组。具体如下:
我想将包含特定分隔符的字符数组转换为多维数组,其中每行的结尾由字符数组中找到的分隔符指定(我已经在不同的方法中确保每行都有相同的长度)。
该数组仅包含0和1。
问题在于,当测试方法时,它(方法)跳过一行 - 读取第一行,下一行为0,读取第三行,第四行为0。
这些是方法(所讨论的方法和测试方法):
/// <summary>
/// Converts a character array to 2D int array, where each character is a digit and only the digits of 1 and 0 are
/// allowed.
/// </summary>
/// <returns>The 2D int array.</returns>
/// <param name="charArray">Character array.</param>
/// <param name="delimiter">Delimiter.</param>
public int[,] ConvertCharArrayTo2DIntArray(char[] charArray, char delimiter){
int columnCounter = 0;
//count how many rows
while (charArray [columnCounter] != delimiter) {
columnCounter++;
}
//count how many lines taking into account the delimiter
int rows = charArray.Length/(columnCounter+1);
int[,] twoDimArray = new int[rows, columnCounter];
//count
int h = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columnCounter; j++) {
if (charArray [h] != '\n') {
twoDimArray [i, j] = (int)Char.GetNumericValue(charArray [h]);
//throw exception if the array contains numbers other than 1 or 0
if (twoDimArray [i, j] != 1 && twoDimArray [i, j] != 0)
throw new ArgumentException ();
h++;
} else {
h++;
break;
}
}
}
return twoDimArray;
}
这是测试方法:
[Test()]
public void TestConvertCharArrayTo2DIntArray(){
HelperClass hc = new HelperClass ();
int[,] twoDimArrayExpected = new int[,]{
{ 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0 } };
char[] charArray = new char[] {'0','1','0','1','1','\n','1','1','1','1','1','\n','0','1','1','1','0','\n','0','1','0','1','0','\n'};
int[,] twoDimArrayActual = hc.ConvertCharArrayTo2DIntArray (charArray, '\n');
for (int i = 0; i < twoDimArrayExpected.GetLength (0); i++) {
for (int j = 0; j < twoDimArrayExpected.GetLength (1); j++) {
Console.Write (twoDimArrayActual [i,j]);
//Commented out because it throws exceptions
//Assert.AreEqual(twoDimArrayExpected[i,j],
//twoDimArrayActual[i,j]);
if (j == twoDimArrayExpected.GetLength (1) - 1) {
Console.Write ("\n");
}
}
}
}
输出是这样的:
01011
00000
11111
00000
我可能只是陷入困境,但现在我真的无法解决这个问题。
答案 0 :(得分:0)
当内循环结束时,您将定位在分隔符上。这是您需要跳过增加控制变量public protocol SettingsStore {
func objectForKey(key: String) -> AnyObject?
func setObject(value: AnyObject?, forKey key: String)
func dictionaryForKey(key: String) -> [String:AnyObject]?
}
public class UserDefaultsSettingsStore {
private let userDefaults: NSUserDefaults
public init() {
self.userDefaults = NSUserDefaults.standardUserDefaults()
}
public init(suiteName: String) {
self.userDefaults = NSUserDefaults(suiteName: suiteName)!
}
}
extension UserDefaultsSettingsStore: SettingsStore {
public func objectForKey(key: String) -> AnyObject? {
return self.userDefaults.objectForKey(key)
}
public func setObject(value: AnyObject?, forKey key: String) {
self.userDefaults.setObject(value, forKey: key)
self.userDefaults.synchronize()
}
public func dictionaryForKey(key: String) -> [String : AnyObject]? {
return self.userDefaults.dictionaryForKey(key)
}
}
正如您现在所做的那样,增加控制变量,然后突破内循环。然而,删除中断指令并不起作用,因为退出内部循环会增加外部循环的控制变量h
,从而使每个偶数行都为空且未处理
您可以尝试使用
修复代码i
答案 1 :(得分:0)
如果使用列表对象,则非常简单
List<List<int>> twoDimArrayExpected = new List<List<int>>() {
new List<int>() { 0, 1, 0, 1, 0 },
new List<int>(){ 0, 1, 0, 1, 0 },
new List<int>(){ 0, 1, 0, 1, 0 },
new List<int>(){ 0, 1, 0, 1, 0 } };
string resutls = string.Join("\n", twoDimArrayExpected.Select(x => string.Join("", x)));