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

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

问题描述

我正在努力地了解数组和阅读这个主题,但是当你刚刚开始编程时,很多文献不容易理解,没有人可以要求你解释。这是我的二维数组:

 '声明字符串的二维数组
Dim cars =
新字符串(,){{BMW,Coupe,Reg:2015,5 Door},
{Ford,Focus,Reg: ,3 Door},
{Land Rover,Discovery,Reg:2014,5 Door},
{Vauxhall,Astra,Reg:2014 ,3 Door},
{SEAT,Ibiza,Reg:2013,5 Door}}

获取数组的边界。
Dim bound0 As Integer = cars.GetUpperBound(0)
Dim bound1 As Integer = cars.GetUpperBound(1)

'循环遍历所有元素。
For i As Integer = 0到bound0
对于x As Integer = 0到bound1
'获取元素。
Dim s1 As String = cars(i,x)
Console.ForegroundColor = ConsoleColor.Green
Console.Write(s1&,)
Next
Console.WriteLine()
Next
Console.ReadKey()
Console.WriteLine(请输入您要查看的记录的名称)
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岁的孩子说话,或者一个网站,可能会帮助我理解这个问题。感谢

解决方案

关键在于数据相关的。一个类将允许你创建一个 Car 对象,而不是将你的汽车分解成不同的



五分钟简介和列表



 公共班车Id As Int32 
公共财产使字符串
公共财产模型作为字符串
公共财产年作为Int32
'... etc
结束类

现在你有一个容器来保存一辆车的所有信息。这就像一个 Car 对象的蓝图。类也可以包含管理他们存储的数据的方法( Sub Function ),

  Dim c As New Car'创建一个新的汽车对象
c.Make =Mazda
c.Model =Miata
c.Year = 2013


b $ b

或者在声明时初始化:

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

现在,New Millennium版本的数组a 列表

 这些更容易使用 Car)

Cars >只有存储汽车对象,它存储的每辆汽车保持每个数据在一起。还有很多其他集合类型,如 Dictionary ,你最终会想要熟悉。添加马自达到列表:

 'c是上面创建的汽车对象
Cars.Add(c)

与数组不同,没有必要知道你将使用多少车,因为他们调整自己的大小。要引用一个, Cars(n)将引用一个汽车对象:

 'n是列表中的汽车的索引
Dim str = Cars(n)。 is&汽车(n).Color

迭代列表,使用temp / code>变量:

 对于每个c As Car In Cars 
'c将会是Cars ),汽车(1)等等,因为我们步骤
Console.WriteLine(索引{0}是一个BEAUTIFUL {1} {2},
Cars.IndexOf(c),c.Year, c.Model)
'eg
'索引4是一个美丽的2015年凯美瑞
下一页

查找一种或第一种类型:

  Dim myCar = Cars.FirstOrDefault f)f.Make =MazdaAndAlso f.Year = 2013)

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

  myDGV.DataSource =汽车

DataGridView将创建一个 Car 类中的每个属性列,并为列表中的每个汽车对象添加一行 - simple!

$ b $或$:
$ b myList.ValueMember =Id

用户将看到 在ListBox(或任何您定义)。 SelectedValue 将是他们选择的汽车对象的Id, SelectedItem 将是整个汽车对象。不需要通过不同的数组去寻找相关的数据 - 它总是在一个地方。


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()

This is the line that is causing the problem

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

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

解决方案

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:

Five Minute Intro to Classes and Lists

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

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

Or initialize when you declare it:

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

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)

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)

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

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

Find one or the first of a kind:

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

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

myDGV.DataSource = Cars

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!

Or:

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

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天全站免登陆