二维数组中的搜索和计数

时间:2019-05-14 07:42:06

标签: arrays vb.net

我有一个2D数组,我想在数组中搜索相同的数字并计数。这是我从文本文件中读取的:

/**
 * List of allowed domains.
 * Note: Restriction works only for AJAX (using CORS, is not secure).
 *
 * @return array List of domains, that can access to this API
 */
public static function allowedDomains()
{
    return [
        // '*',                        // star allows all domains
        'http://test1.example.com',
        'http://test2.example.com',
    ];
}

/**
 * @inheritdoc
 */
public function behaviors()
{
    return array_merge(parent::behaviors(), [

        // For cross-domain AJAX request
        'corsFilter'  => [
            'class' => \yii\filters\Cors::className(),
            'cors'  => [
                // restrict access to domains:
                'Origin'                           => static::allowedDomains(),
                'Access-Control-Request-Method'    => ['POST','GET'],
                'Access-Control-Allow-Credentials' => true,
                'Access-Control-Max-Age'           => 3600,                 // Cache (seconds)
            ],
        ],

    ]);
}

001和002代表镇,1和2代表天,值从1到23 对于第1天的001镇,我希望程序计算每个值的出现次数,例如。

{001,1,5,6,21},{001,1,5,6,21},(001,1,10,12,18},{002,1,9,10,12},{002,1,6,19,21},{002,1,6,19,21},{001,2,5,6,21},{001,2,6,19,21},{001,2,6,19,21},{002,2,5,6,21},{002,2,6,19,21},{002,2,11,19,21}

该数组为Town day 5 6 9 10 11 12 19 21 001, 1 , 2,2,0, 1, 0, 1, 0, 2 002, 1 , 0,2,1, 1, 0, 1, 2, 2 001, 2 , 1,3,0, 0, 0, 0, 2, 3 002, 2, 1,2,0, 0, 1, 0, 2, 3
这是我尝试进行计数的代码

TwoDeearray(4,23)

1 个答案:

答案 0 :(得分:0)

我没有遵循您在循环中要执行的操作,但这是如何遍历2D数组的方法

   Dim twodeeArray(23, 4) As Integer
   For row As Integer = 0 To twodeeArray.GetLength(0) - 1
      For col As Integer = 0 To twodeeArray.GetLength(1) - 1
         If twodeeArray(row, col) > 0 Then  'Change to your test
             ' Your code here
         End If
       Next
     Next

您使用的是TwoDeeArray.Rank,它仅显示维数。您还似乎在声明数组的边界颠倒了。

希望能使您走上正确的道路。如果您可以解释您在循环中要实现的目标,那么我会帮忙看看。

编辑-回复评论

此代码将遍历您的数组,并为您提供“总计”列表。每个总数均按镇,日期和数字索引,并包含每个总数。您只需要遍历该列表并根据需要对其进行处理。这是完整的代码:

Private Class Totals
    Private _town As Integer
    Private _day As Integer
    Private _num As Integer
    Private _count As Integer

    Public Sub New(town As Integer, day As Integer, num As Integer)
        _town = town
        _day = day
        _num = num
        _count = 1
    End Sub

    Public ReadOnly Property Town As Integer
        Get
            Return _town
        End Get
    End Property

    Public ReadOnly Property Day As Integer
        Get
            Return _day
        End Get
    End Property

    Public ReadOnly Property Num As Integer
        Get
            Return _num
        End Get
    End Property

    Public Property Count As Integer
        Get
            Return _count
        End Get
        Set(value As Integer)
            _count = value
        End Set
    End Property

    Public Function MatchFound(town As Integer, day As Integer, num As Integer) As Boolean
        Return town = _town AndAlso day = _day AndAlso num = _num
    End Function

End Class

Private Sub Test()
    Dim twodeeArray(,) As Integer = {{1, 1, 5, 6, 21}, {1, 1, 5, 6, 21}, {1, 1, 10, 12, 18}, {2, 1, 9, 10, 12}, {2, 1, 6, 19, 21}, {2, 1, 6, 19, 21}, {1, 2, 5, 6, 21}, {1, 2, 6, 19, 21}, {1, 2, 6, 19, 21}, {2, 2, 5, 6, 21}, {2, 2, 6, 19, 21}, {2, 2, 11, 19, 21}}
    Dim TotalList As New List(Of Totals)
    For row As Integer = 0 To twodeeArray.GetLength(0) - 1
        Dim town As Integer = twodeeArray(row, 0)
        Dim day As Integer = twodeeArray(row, 1)
        For col As Integer = 2 To twodeeArray.GetLength(1) - 1
            Dim Matched As Boolean = False
            Dim i As Integer = 0
            While Not Matched And i < TotalList.Count - 1

                If TotalList(i).MatchFound(town, day, twodeeArray(row, col)) Then
                    TotalList(i).Count += 1
                    Matched = True
                End If
                i += 1
            End While
            If Not Matched Then TotalList.Add(New Totals(town, day, twodeeArray(row, col)))
        Next
    Next
End Sub