2015-11-07 86 views
0

我正在处理vb.net中的项目,我有一个整数数组(例如1,8,9,8),我需要的是通过add 1到每个项目一次,以便第一个数组假设是(2,8,9,8)和第二个(1,9,9,8)等等,我试过这个代码:从一个数组中生成数组的数量

Function myarray(ByVal arra1() As Integer, ByVal arran() As Integer, ByVal i As Integer) As Integer 
    For i = 0 To arra1.Length - 
     arran(i) = arra1(i) 
     arran(i) = arra1(i) + 1 
     Next 
End Function 

回答

0

尽可能多我从当前给定环境的问题,这可能是你所需要的解决方案理解as

Dim a3() As Integer={1,8,9,8} 
Dim a4() As Integer={} 
myarray(a3,a4,1) 
myarray(a3,a4,2) 
myarray(a3,a4,3) 
myarray(a3,a4,4) 

当调用与arra1为(1,8,9,8)
和arrNumber为1这个函数然后艾伦将是(2,8,9,8),
与arrNumber 2-然后阿伦将(1,9,9,8),
,其中arrNumber为3,那么arran将是(1,8,10,8)和
,其中arrNumber为4,那么arran将是(1,8,9,9)

+0

感谢哈拉曼,但我在这一点上得到了例外...如果我=(arrNumber - 1)然后' – rasha

+0

对不起,我得到(对象引用未设置为对象的实例).at arran(i)= arr1(i )不是我之前说的 – rasha

+0

@rasha这只是一个例子,说明如何使用这个最小代码指南来实现您的需求。您必须根据您的要求在您的代码中实施它。所以假设你会初始化'arran'并打印或以任何你想要的方式使用它。检查更新的代码 – haraman

0

首先声明你的初始数组。

' declare initial array 
Dim arr1 = {1, 8, 9, 8} 

然后为结果声明一个参差不齐的数组。由于初始数组有4个元素,它将创建4个数组。

' declare a jagged array for the result 
Dim result(arr1.Length - 1)() As Integer 

然后你克隆初始数组,然后只改变一个值。

For i = 0 To arr1.Length - 1 

    ' clone the initial array 
    result(i) = arr1.Clone() 

    ' only change the element at index = i 
    result(i)(i) += 1 

Next 

结果是:

2 8 9 8 
1 9 9 8 
1 8 10 8 
1 8 9 9 

完整的源:

Module Module1 

    Sub Main() 

     ' declare initial array 
     Dim arr1 = {1, 8, 9, 8} 

     ' declare a jagged array for the result 
     Dim result(arr1.Length - 1)() As Integer 

     For i = 0 To arr1.Length - 1 

      ' clone the initial array 
      result(i) = arr1.Clone() 

      ' only change the element at index = i 
      result(i)(i) += 1 

     Next 

     ' print result 
     For i = 0 To result.Length - 1 
      For j = 0 To result(i).Length - 1 
       Console.Write(result(i)(j) & " ") 
      Next 
      Console.WriteLine() 
     Next 

     Console.ReadKey(True) 

    End Sub 

End Module 

如果你想使一个功能。

Function CopyArray(ByVal source As Integer(), i As Integer) As Integer() 

    ' clone the initial array 
    Dim temp = source.Clone() 

    ' only change the element at index = i 
    temp(i) += 1 

    Return temp 

End Function 

您在迭代中调用函数。

For i = 0 To arr1.Length - 1 

    result(i) = CopyArray(arr1, i) 

Next 

完整的源与功能:

Function myarray(ByVal arra1() As Integer, ByVal arran() As Integer, ByVal arrNumber As Integer) As Integer 
    arran=arra1.Clone() 
    For i As Integer = 0 To arra1.Length - 1 
     If i = (arrNumber - 1) Then ' IF arrNumber is 1 then +1 to index 0, If it is 2 then +1 to index 1 
      arran(i) = arra1(i) + 1 
     Else 
      arran(i) = arra1(i) 
     End If 
    Next 
    'Print the array 
    For i = 0 To arran.Length - 1 
     Console.Write(arran(i) & " ") 
    Next 
    Console.WriteLine() 
    Return 0 
End Function 

你可以称它为:

Module Module1 

    Sub Main() 

     ' declare initial array 
     Dim arr1 = {1, 8, 9, 8} 

     ' declare a jagged array for the result 
     Dim result(arr1.Length - 1)() As Integer 

     For i = 0 To arr1.Length - 1 

      result(i) = CopyArray(arr1, i) 

     Next 

     ' print result 
     For i = 0 To result.Length - 1 
      For j = 0 To result(i).Length - 1 
       Console.Write(result(i)(j) & " ") 
      Next 
      Console.WriteLine() 
     Next 

     Console.ReadKey(True) 

    End Sub 

    Function CopyArray(ByVal source As Integer(), i As Integer) As Integer() 

     ' clone the initial array 
     Dim temp = source.Clone() 

     ' only change the element at index = i 
     temp(i) += 1 

     Return temp 

    End Function 

End Module 
+0

谢谢你的回答,它是无功能的工作,但在功能的情况下它不起作用 – rasha

+0

它与功能。我已经测试过它。看我的编辑。 – Han

+0

现在它的功能正在工作.....非常感谢你Handoko – rasha

相关问题