使用二维数组中存储的相关数据 [英] Using related data stored in a 2 dimensional array

查看:17
本文介绍了使用二维数组中存储的相关数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解数组并阅读相关主题,但是当您刚刚开始编程并且没有人可以要求解释时,很多文献并不容易让您理解.这是我的二维数组:

I am trying hard to understand arrays and read around the subject, but much of the literature is not easy to get your head around when you've only just started to program and there's no one you can ask to explain. This is my two dimensional array:

        'Declare 2-diensional array of Strings
    Dim cars(,) As String =
    New String(,) {{"BMW", "Coupe", "Reg:2015", "5 Door"},
           {"Ford", "Focus", "Reg:2015", "3 Door"},
           {"Land Rover", "Discovery", "Reg:2014", "5 Door"},
           {"Vauxhall", "Astra", "Reg:2014", "3 Door"},
           {"SEAT", "Ibiza", "Reg:2013", "5 Door"}}

    ' Get bounds of the array.
    Dim bound0 As Integer = cars.GetUpperBound(0)
    Dim bound1 As Integer = cars.GetUpperBound(1)

    ' Loop over all elements.
    For i As Integer = 0 To bound0
        For x As Integer = 0 To bound1
            ' Get element.
            Dim s1 As String = cars(i, x)
            Console.ForegroundColor = ConsoleColor.Green
            Console.Write(s1 & ", ")
        Next
        Console.WriteLine()
    Next
    Console.ReadKey()
    Console.WriteLine("Please enter the name of the record you wish to view")
    Dim s = Console.ReadLine()
    Dim value As String = Array.Find(cars, Function(x) (x.StartsWith(s)))
    Console.WriteLine(value)
    Console.ReadKey()

这是导致问题的线路

Dim value As String = Array.Find(cars, Function(x) (x.StartsWith(s)))

Visual Studio 表明该错误是因为无法从这些参数推断类型参数的数据类型.明确指定数据类型可能会更正此错误."我无法理解这个错误的含义.请有人可以解释一下,就像在和一个 10 岁的孩子交谈一样,或者也许是一个可以帮助我理解这个问题的网站.谢谢

Visual Studio suggests that the error is because "Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error." I cant get my head around what this error means. Please can someone please explain it as though talking to a 10 year old Or perhaps a website that might help me understand this problem. Thanks

推荐答案

关键在于数据相关的.而不是打破你的汽车"分成多个部分存储在不同数组中,一个类将允许您创建一个Car对象,并将各种汽车存储在一个类型化的List中:

The key lies in the fact that the data is related. Rather than breaking up your "car" into pieces to store in different arrays, a Class would allow you to create a Car object, and store various cars in a typed List:

Public Class Car
    Public Property Id As Int32
    Public Property Make As String
    Public Property Model As String
    Public Property Year As Int32
    '... etc
End Class

现在您有一个容器来保存一辆车的所有信息.这就像 Car 对象的外观蓝图.类还可以包含方法(SubFunction)来管理它们存储的数据,以便与 Car、Employee 或 Order 相关的所有内容都可以由该类管理.

Now you have a container to save all the info for one car. This is like a blueprint for what a Car object will look like. A Class can also contain methods (Sub or Function) to manage the data they store so that everything relating to a Car or Employee or Order can be managed by that class.

Dim c As New Car          ' create a new car object
c.Make = "Mazda"
c.Model = "Miata"
c.Year = 2013

或者在声明时初始化:

Dim c As New Car With {.Make = "Mazda", .Model = "Miata" ...}

现在,新千年版本的数组是一个List.这些更容易使用,因为它们可以自行调整大小:

Now, the New Millennium version of arrays, is a List. These are much easier to work with because they size themselves:

Dim Cars As New List(Of Car)

Cars 集合可以存储汽车对象,它存储的每辆汽车都将每个汽车的数据保存在一起.还有许多其他的集合类型,例如 Dictionary 您最终会想要熟悉的.将马自达添加到列表中:

The Cars collection can only store car objects, each car it stores keeps the data together for each one. There are many other collection types such as Dictionary you will eventually want to get familiar with. Add the Mazda to the List:

' c is the car object created above
Cars.Add(c)

与数组不同,无需知道您将使用多少辆汽车,因为它们会自行调整大小.要引用一个,Cars(n) 将引用一个汽车对象:

Unlike arrays there is no need to know how many cars you will be working with because they resize themselves. To reference one, Cars(n) will refer to a car object:

' n is the index of a car in the list
Dim str = Cars(n).Make & " is " & Cars(n).Color

使用临时 Car 变量迭代列表:

Iterate the list, using a temp Car variable:

For Each c As Car In Cars   
    ' c will be Cars(0), Cars(1) etc as we step thru
    Console.WriteLine("Index {0} is a BEAUTIFUL {1} {2}",
           Cars.IndexOf(c), c.Year, c.Model)
    ' e.g
    ' "Index 4 is a BEAUTIFUL 2015 Camry"
Next

找到一个或第一个:

Dim myCar = Cars.FirstOrDefault(Function (f) f.Make = "Mazda" AndAlso f.Year = 2013)

List(Of T) 可以用作某些控件的DataSource:

A List(Of T) can be used as a DataSource for some controls:

myDGV.DataSource = Cars

DataGridView 将为 Car 类中的每个 Property 创建一列,并为列表中的每个汽车对象添加一行 - 很简单!

The DataGridView will create a column for each Property in the Car class, and add a row for each car object in the list - simple!

或者:

myListBox.DataSource
myList.DisplayMember = "Make"
myList.ValueMember = "Id"

用户将在 ListBox(或您定义的任何内容)中看到 Make.SelectedValue 将是他们选择的汽车对象的 ID,SelectedItem 将是整个汽车对象.无需通过不同的阵列来查找相关数据 - 它们始终集中在一个位置.

The user will see the Make in the ListBox (or whatever you define). SelectedValue will be the Id of the car object they selected and SelectedItem will be the entire car object. No need to go rifling thru different arrays to find related data - it is always together in one spot.

这篇关于使用二维数组中存储的相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆