none
No of rows and Columns in an array? RRS feed

  • Question

  • Module Module1
        Sub Main()
            Dim grid(,) As Integer = {{2, 4}, {6, 8}, {10, 12}, {14, 16}, {18, 20}}
        End Sub
    End Module

    I have created an array which has 5 rows and 2 columns. IS there any function which will return the number of rows and columns of a given array?


    Sougata Ghosh

    Friday, July 20, 2018 7:39 AM

Answers

  • Hi

    I hope the following methods can help you.

            Dim arr(,) As Integer = {{2, 4}, {6, 8}, {10, 12}, {14, 16}, {18, 20}}
    
            Console.Write("The number of rows in the array is:")
            Console.WriteLine(arr.GetLength(0))
            Console.Write("The number of columns in the array is:")
            Console.WriteLine(arr.GetLength(1))
    
            For i As Integer = 0 To arr.GetLength(0) - 1
                Dim str As String = ""
    
                For j As Integer = 0 To arr.GetLength(1) - 1
                    str = str & Convert.ToString(arr(i, j)) & " "
                Next
    
                Console.WriteLine(str)
            Next
    
            Console.ReadLine()

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by sougata12 Friday, July 20, 2018 8:47 AM
    Friday, July 20, 2018 7:56 AM
  • Hi 

    According to your description, if you want to know the iterative array above, you must know the rows and columns, just based on the iterative array dimension is not acceptable.

    But you can find the values of rows and columns based on the array dimensions.

    Rows:

    arr.GetUpperBound(arr.Rank - 2) + 1

    Columns:

    arr.GetUpperBound(arr.Rank - 1) + 1

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by sougata12 Monday, July 23, 2018 5:59 PM
    Monday, July 23, 2018 1:17 AM

All replies

  • Hi

    I hope the following methods can help you.

            Dim arr(,) As Integer = {{2, 4}, {6, 8}, {10, 12}, {14, 16}, {18, 20}}
    
            Console.Write("The number of rows in the array is:")
            Console.WriteLine(arr.GetLength(0))
            Console.Write("The number of columns in the array is:")
            Console.WriteLine(arr.GetLength(1))
    
            For i As Integer = 0 To arr.GetLength(0) - 1
                Dim str As String = ""
    
                For j As Integer = 0 To arr.GetLength(1) - 1
                    str = str & Convert.ToString(arr(i, j)) & " "
                Next
    
                Console.WriteLine(str)
            Next
    
            Console.ReadLine()

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by sougata12 Friday, July 20, 2018 8:47 AM
    Friday, July 20, 2018 7:56 AM
  • I was thinking the following approach to iterate through the array mentioned above:

    for i as integer = 0 to Grid.Rank-1

          For Each element in the ith dimension of the Array named Grid....console.writeline

    Next

    Is it possible to write a "for each" loop which will iterate through a particular dimension of an array and display it. Then it moves to the next dimension of the array as the value of i changes? Any thoughts ?

     

    Sougata Ghosh

    Friday, July 20, 2018 9:32 AM
  • Hi 

    According to your description, if you want to know the iterative array above, you must know the rows and columns, just based on the iterative array dimension is not acceptable.

    But you can find the values of rows and columns based on the array dimensions.

    Rows:

    arr.GetUpperBound(arr.Rank - 2) + 1

    Columns:

    arr.GetUpperBound(arr.Rank - 1) + 1

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by sougata12 Monday, July 23, 2018 5:59 PM
    Monday, July 23, 2018 1:17 AM