如何循环多个数组?

时间:2022-09-22 23:56:21

I'm new to this, so sorry if my question has been asked before. I have searched but have not been able to find, or perhaps recognise, an answer. I am using visual studio 2008 and creating an app in vb.net.

我是新来的,很抱歉,如果我之前已经提出过我的问题。我已经搜索过但无法找到或者认出答案。我正在使用visual studio 2008并在vb.net中创建应用程序。

I have 4 arrays named:- account1 account2 account3 account4. They all have 4 elements. I want to assign values to the elements in the arrays in an efficient manner. I thought two for next loops would do it. My pseudo code looks something like this

我有4个数组: - account1 account2 account3 account4。他们都有4个元素。我想以有效的方式为数组中的元素赋值。我认为两个下一个循环会做到这一点。我的伪代码看起来像这样

for i=1 to 4
    for c= 0 to 3
        account(i)(c)= 'mydata' /so account(i) would be account1 etc and c the element
    next c
next i

and thus all the elements of all the arrays are populated without me having to set up a fornext loop for each individual array name. How can I achieve this please.

因此,所有数组的所有元素都被填充,而不必为每个单独的数组名称设置一个fornext循环。我怎么能达到这个目的。

Hope I have provided enough info to be of use, and that you can help. Thanks for all and any advice.

希望我已经提供了足够的信息以供您使用,并且您可以提供帮助。感谢所有和任何建议。

3 个解决方案

#1


If I understand correctly, why not:

如果我理解正确,为什么不:

For i as integer = 0 to 3
    account1(i) = "Account1"
    account2(i) = "Account2"
    account3(i) = "Account3"
    account4(i) = "Account4"
Next

Edit VB.Net for Qua's answer:

编辑VB.Net以获得Qua的答案:

dim accounts(4,4) as integer

for i as integer = 0 To accounts.GetUpperBound(0)
  for j as integer = 0 To accounts.GetUpperBound(1)
     accounts(i, j) = new integer 'not needed for intergers, but if you had a class in here'
     accounts(i, j) = i*j;
  next
next

#2


You should create a multidimensional array instead of 4 arrays, that would allow you to loop genericly through the arrays.

您应该创建一个多维数组而不是4个数组,这将允许您通过数组循环。

int[,] accounts = new int[4,4] // 4 accounts with 4 elements
for (int i = 0 ; i < accounts.GetUpperBound(0); i++)
  for (int j = 0 ; i < accounts.GetUpperBound(1); j++)
     accounts[i,j] = i*j;
  next
next

#3


As I read your code example, I don't think that you need to use 2 seperate loops, as if I'm right you are assigning the same value to the ith position of your array eg:

当我阅读你的代码示例时,我认为你不需要使用2个单独的循环,就好像我是对的你将相同的值分配给数组的第i个位置,例如:

array1(i) = array2(i) = array3(i) = array4(i)

array1(i)= array2(i)= array3(i)= array4(i)

In your example above you could write something like this (in pseudocode):

在上面的示例中,您可以编写类似这样的内容(伪代码):

for i = 0 to 3
   account1(i) = MyData
   account2(i) = MyData
   account3(i) = MyData
   account4(i) = MyData
next i

I think that this is clearer than trying to write a loop for the variable names, especially for the number of arrays you are maintaining

我认为这比尝试为变量名编写循环更清楚,特别是对于您维护的数组的数量

Another option, which might be more appropriate if you have lots of arrays, would be to maintain a list of arrays which can then be iterated through simply enough.

另一个选项,如果你有很多数组可能更合适,那就是维护一个数组列表,然后可以通过简单的迭代来完成。

PseudoCode for this option:

此选项的伪代码:

for each array in listOfArrays
  for i = 0 to 3
    array(i) = MyData
  next i
next

This is definitely clearer than trying to generate the names of the arrays dynamically, and more maintainable as well

这比尝试动态生成数组的名称更加清晰,并且更易于维护

#1


If I understand correctly, why not:

如果我理解正确,为什么不:

For i as integer = 0 to 3
    account1(i) = "Account1"
    account2(i) = "Account2"
    account3(i) = "Account3"
    account4(i) = "Account4"
Next

Edit VB.Net for Qua's answer:

编辑VB.Net以获得Qua的答案:

dim accounts(4,4) as integer

for i as integer = 0 To accounts.GetUpperBound(0)
  for j as integer = 0 To accounts.GetUpperBound(1)
     accounts(i, j) = new integer 'not needed for intergers, but if you had a class in here'
     accounts(i, j) = i*j;
  next
next

#2


You should create a multidimensional array instead of 4 arrays, that would allow you to loop genericly through the arrays.

您应该创建一个多维数组而不是4个数组,这将允许您通过数组循环。

int[,] accounts = new int[4,4] // 4 accounts with 4 elements
for (int i = 0 ; i < accounts.GetUpperBound(0); i++)
  for (int j = 0 ; i < accounts.GetUpperBound(1); j++)
     accounts[i,j] = i*j;
  next
next

#3


As I read your code example, I don't think that you need to use 2 seperate loops, as if I'm right you are assigning the same value to the ith position of your array eg:

当我阅读你的代码示例时,我认为你不需要使用2个单独的循环,就好像我是对的你将相同的值分配给数组的第i个位置,例如:

array1(i) = array2(i) = array3(i) = array4(i)

array1(i)= array2(i)= array3(i)= array4(i)

In your example above you could write something like this (in pseudocode):

在上面的示例中,您可以编写类似这样的内容(伪代码):

for i = 0 to 3
   account1(i) = MyData
   account2(i) = MyData
   account3(i) = MyData
   account4(i) = MyData
next i

I think that this is clearer than trying to write a loop for the variable names, especially for the number of arrays you are maintaining

我认为这比尝试为变量名编写循环更清楚,特别是对于您维护的数组的数量

Another option, which might be more appropriate if you have lots of arrays, would be to maintain a list of arrays which can then be iterated through simply enough.

另一个选项,如果你有很多数组可能更合适,那就是维护一个数组列表,然后可以通过简单的迭代来完成。

PseudoCode for this option:

此选项的伪代码:

for each array in listOfArrays
  for i = 0 to 3
    array(i) = MyData
  next i
next

This is definitely clearer than trying to generate the names of the arrays dynamically, and more maintainable as well

这比尝试动态生成数组的名称更加清晰,并且更易于维护