Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在数组中循环并每隔一步将某些内容放入数组中_Arrays_Vb.net_String_Algorithm - Fatal编程技术网

Arrays 在数组中循环并每隔一步将某些内容放入数组中

Arrays 在数组中循环并每隔一步将某些内容放入数组中,arrays,vb.net,string,algorithm,Arrays,Vb.net,String,Algorithm,所以我最近一直在忙于一项作业,现在我正在用Visual Basic编程,使用Visual Studio 2013 Update 4,并且在.NET Framework中工作 这是我的问题: 我想要完成的是,我运行了一个只有81个字符(长度)的数组。我想遍历这个数组,在for循环的9个步骤之后,我想将这9个字符保存到一个字符串中 我的意思是我想在一个字符串中保存字符0-8,然后在另一个字符串中保存字符9-17,依此类推 (数组将填入我的程序) 我已经做了很多尝试来完成这项工作,但是还没有找到解决方

所以我最近一直在忙于一项作业,现在我正在用Visual Basic编程,使用Visual Studio 2013 Update 4,并且在.NET Framework中工作

这是我的问题: 我想要完成的是,我运行了一个只有81个字符(长度)的数组。我想遍历这个数组,在for循环的9个步骤之后,我想将这9个字符保存到一个字符串中

我的意思是我想在一个字符串中保存字符0-8,然后在另一个字符串中保存字符9-17,依此类推

(数组将填入我的程序)

我已经做了很多尝试来完成这项工作,但是还没有找到解决方案,我已经在互联网上搜索过了,但是我找不到解决方案。 所以希望这里的任何人都能帮助我D


(我要求你为我做一个小算法:/)

我想到了三种可能性

第一种是一种经典的方法,每次构建一个字符串一个字符,直到读取了9个字符,然后重新启动下一个9个字符,直到到达数组的末尾

这里的诀窍是在阅读了9个字符后发现(注意,我从1开始,以避免第一个返回0的0 MOD 9)

另一种方法使用Linq,可读性更高

Dim index = 0
Do while(index < 81)
    Dim s = new String(charactersArray.Skip(index).Take(9).ToArray())
    Console.WriteLine(s)
    index += 9
Loop

这将打印到控制台,而不是向ArrayList之类的东西添加项目,它还修复了原始
For
循环中的一个bug:

Dim charactersArray(81) as character
For intIndex as integer = 0 to 80 Step 9
    Dim s = New String(charactersArray, intIndex, 9)
    System.Console.WriteLine(s)
Next

您可能想使用一个字符串构造函数,它将获取一个“字符数组”并返回一个字符串:-对不起,我没有时间敲出示例代码,也许其他人可以在此基础上构建一个答案。非常感谢你的回答。它让我学到了很多,现在我知道如何在代码中实现这一点。虽然稍后发布的答案更符合我的确切问题;)非常感谢你的回答。这或多或少正是我所需要的。
Dim index = 0
Do while(index < 81)
    Dim s = new String(charactersArray.Skip(index).Take(9).ToArray())
    Console.WriteLine(s)
    index += 9
Loop
Dim charBuffer(8) as Char
Dim index = 0
Do while(index < 81)
    Array.Copy(charactersArray, index, charBuffer, 0, 9)
    Console.WriteLine(new string(charBuffer))
    index += 9
Loop
StringBuilder:66 ms
Linq:706 ms
Array.Copy:40 ms
Dim charactersArray(81) as character
For intIndex as integer = 0 to 80 Step 9
    Dim s = New String(charactersArray, intIndex, 9)
    System.Console.WriteLine(s)
Next