当前位置:编程学习 > C#/ASP.NET >>

vb.net 二维泛型如何使用?

有一个表,行数不定,列数为5,列的类型不统一,有数字型和字符等
由于行数不定,我想取出进行运算,用泛型的add方法,
可是在度娘找了一圈.只发现vb.net泛型二维数组定义如下
mylistarry.Add(New Double(,) {})
不知道该如何赋值和取数
也不知道我的想法对不对,是否有更好的存数取数方法
请各位大大们指教! 泛型 vb.net 二维数组 类 --------------------编程问答-------------------- 试试:
 Dim dic As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
        '00表示第一行第一列,01表示第一行第二列,依此类推,以他们作为key,将其对应的值存入
        dic.Add("00", "1")
        dic.Add("01", "h"c)
        dic.Add("02", 1)

        dic.Add("10", "2")
        dic.Add("11", "H"c)
        dic.Add("12", 2)
        MsgBox(dic("01").ToString()) '查找第一行第二列的值
--------------------编程问答-------------------- 用List(Of Tuple(Of 第一列类型, Of 第二列类型, Of 第三列类型, Of 第四列类型, Of 第五列类型)) --------------------编程问答-------------------- eg:
Dim list As List(Of Tuple(Of Integer, Of String,Of String, Of Double, Of Double)) = New List(Of Tuple(Of Integer, Of String,Of String, Of Double, Of Double))
list.Add(New Tuple(Of Integer, Of String,Of String, Of Double, Of Double)(1, "北京", "中国", -10.5, 21.5))
list.Add(New Tuple(Of Integer, Of String,Of String, Of Double, Of Double)(2, "上海", "中国", 8.1, 26.3))
list.Add(New Tuple(Of Integer, Of String,Of String, Of Double, Of Double)(3, "伦敦", "英国", 10.2, 25.5))
For Each item As Tuple(Of Integer, Of String,Of String, Of Double, Of Double) In list
    MsgBox item.Item1 & " 最低气温:" & item.Item3 & " 最高气温:" item.Item4
Next
--------------------编程问答-------------------- 请赐教  Of Tuple  是啥

从没见过   百度无结果   --------------------编程问答-------------------- 泛型和原来的数组的类型是一样的,原来数组是什么类型改成泛型是很容易的,只是泛型用起来更顺手,如:
STRING[]改成泛型LIST<STRING>
INT[]改成泛型LIST<INT>
如果是混合类型直接用基类即可LIST<Object>
维数就和一般数组是一样的道理了。 --------------------编程问答--------------------
引用 4 楼 xiaobingking 的回复:
请赐教  Of Tuple  是啥

从没见过   百度无结果  


那请你Google下。 --------------------编程问答--------------------
引用 3 楼 caozhy 的回复:
eg:
Dim list As List(Of Tuple(Of Integer, Of String,Of String, Of Double, Of Double)) = New List(Of Tuple(Of Integer, Of String,Of String, Of Double, Of Double))
list.Add(New Tuple(Of Integer, Of String,Of String, Of Double, Of Double)(1, "北京", "中国", -10.5, 21.5))
list.Add(New Tuple(Of Integer, Of String,Of String, Of Double, Of Double)(2, "上海", "中国", 8.1, 26.3))
list.Add(New Tuple(Of Integer, Of String,Of String, Of Double, Of Double)(3, "伦敦", "英国", 10.2, 25.5))
For Each item As Tuple(Of Integer, Of String,Of String, Of Double, Of Double) In list
    MsgBox item.Item1 & " 最低气温:" & item.Item3 & " 最高气温:" item.Item4
Next


我全部拷代码过去不行啊,提示一堆的错,我的是vb2005
--------------------编程问答-------------------- http://msdn.microsoft.com/zh-cn/library/vstudio/dd414892.aspx --------------------编程问答-------------------- 2005太旧了,要至少2010。

2005可以定义一个结构体代替Tuple。 --------------------编程问答--------------------
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      ' Organization of runningBacks 5-tuple:
      '    Component 1: Player name
      '    Component 2: Number of games played
      '    Component 3: Number of attempts (carries)
      '    Component 4: Number of yards gained 
      '    Component 5: Number of touchdowns   
      Dim runningBacks() =
          { Tuple.Create("Payton, Walter", 190, 3838, 16726, 110),  
            Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99),            
            Tuple.Create("Brown, Jim", 118, 2359, 12312, 106),            
            Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90),            
            Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) } 
      ' Calculate statistics.
      ' Organization of runningStats 5-tuple:
      '    Component 1: Player name
      '    Component 2: Number of attempts per game
      '    Component 3: Number of yards per game
      '    Component 4: Number of yards per attempt 
      '    Component 5: Number of touchdowns per attempt   
      Dim runningStats() = ComputeStatistics(runningBacks)

      ' Display the result.          
      Console.WriteLine("{0,-16} {1,5} {2,6} {3,7} {4,7} {5,7} {6,7} {7,5} {8,7}", 
                        "Name", "Games", "Att", "Att/Gm", "Yards", "Yds/Gm",
                        "Yds/Att", "TD", "TD/Att")
      Console.WriteLine()
      For ctr As Integer = 0 To runningBacks.Length - 1
         Console.WriteLine("{0,-16} {1,5} {2,6:N0} {3,7:N1} {4,7:N0} {5,7:N1} {6,7:N2} {7,5} {8,7:N3}", 
                           runningBacks(ctr).Item1, runningBacks(ctr).Item2, runningBacks(ctr).Item3, 
                           runningStats(ctr).Item2, runningBacks(ctr).Item4, runningStats(ctr).Item3, 
                           runningStats(ctr).Item4, runningBacks(ctr).Item5, runningStats(ctr).Item5)
         Console.WriteLine()  
      Next     
   End Sub

   Private Function ComputeStatistics(players() As Tuple(Of String, Integer, Integer, Integer, Integer)) _
                    As Tuple(Of String, Double, Double, Double, Double)()

      Dim result As Tuple(Of String, Double, Double, Double, Double)
      Dim list As New List(Of Tuple(Of String, Double, Double, Double, Double))()

      For Each player In players
         ' Create result object containing player name and statistics.
         result = Tuple.Create(player.Item1,  
                            player.Item3/player.Item2, player.Item4/player.Item2,
                            player.Item4/player.Item3, player.Item5/player.Item3)
         list.Add(result)         
      Next
      Return list.ToArray()  
   End Function
End Module
' The example displays the following output:
'    Name             Games    Att  Att/Gm   Yards  Yds/Gm Yds/Att    TD  TD/Att
'    
'    Payton, Walter     190  3,838    20.2  16,726    88.0    4.36   110   0.029
'    
'    Sanders, Barry     153  3,062    20.0  15,269    99.8    4.99    99   0.032
'    
'    Brown, Jim         118  2,359    20.0  12,312   104.3    5.22   106   0.045
'    
'    Dickerson, Eric    144  2,996    20.8  13,259    92.1    4.43    90   0.030
'    
'    Faulk, Marshall    176  2,836    16.1  12,279    69.8    4.33   100   0.035


来自微软的例子。

我的写法略微有些问题。因为VB我不熟。 --------------------编程问答--------------------
引用 5 楼 xianfajushi 的回复:
泛型和原来的数组的类型是一样的,原来数组是什么类型改成泛型是很容易的,只是泛型用起来更顺手,如:
STRING[]改成泛型LIST<STRING>
INT[]改成泛型LIST<INT>
如果是混合类型直接用基类即可LIST<Object>
维数就和一般数组是一样的道理了。

怎么推啊,给点实际的用法例子
你用过二维泛型吗 --------------------编程问答--------------------
引用 9 楼 caozhy 的回复:
2005太旧了,要至少2010。

2005可以定义一个结构体代替Tuple。

不是吧,我没有可能换更高版本啊 --------------------编程问答-------------------- VB2005的写法:

Structure SomeType
    Public ID As Integer
    Public Name As String
    ...
End Structure

Dim list As New List(Of SomeType)()
Dim st1 As New SomeType
st1.ID = 1
st1.Name = "a"
...
list.Add(st1) --------------------编程问答-------------------- 那好,给你一个简单例子:
List<List<string>> 字符组 = new List<List<string>>();
            字符组.Add(new string[] { "啊啊啊啊", "啊啊啊啊啊啊啊啊" }.ToList());
怎么用自己去看了。 --------------------编程问答-------------------- 个人觉得可以直接在表里面取出运算,不需要用到数组反而绕了。
这次《代码》模块升级后把我发表的资料丢了,不知怎么搞的,反而觉得不好用了? --------------------编程问答--------------------
引用 15 楼 xianfajushi 的回复:
个人觉得可以直接在表里面取出运算,不需要用到数组反而绕了。
这次《代码》模块升级后把我发表的资料丢了,不知怎么搞的,反而觉得不好用了?

我直接运算1个多小时才跑了200多条数据,不知道是怎么回事!
我想试试取出数后的运算速度 --------------------编程问答--------------------
引用 14 楼 xianfajushi 的回复:
那好,给你一个简单例子:
List<List<string>> 字符组 = new List<List<string>>();
            字符组.Add(new string[] { "啊啊啊啊", "啊啊啊啊啊啊啊啊" }.ToList());
怎么用自己去看了。

你这是VB.NET的代码,有没去试啊,根本无法定义 --------------------编程问答-------------------- 数据表和数组一样都在内存中的不是?

List<int[,]> 图标序集 = new List<int[,]>();
图标序集.Add(new int[,] { { 0, 16 }, { 5, 15 } }); }
--------------------编程问答-------------------- 呵呵,不是VB是C#的 --------------------编程问答-------------------- VB自用C#就没用过,试新建一个:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim list As List(Of Int16(,)) = New List(Of Int16(,))
        list.Add(New Int16(,) {{1, 2}, {3, 4}})
    End Sub

当然我用的是VS2010的 --------------------编程问答--------------------
引用 6 楼 caozhy 的回复:
Quote: 引用 4 楼 xiaobingking 的回复:

请赐教  Of Tuple  是啥

从没见过   百度无结果  


那请你Google下。


原来是  4.5 才有的东西哦

我还生活在2.0 的世界 --------------------编程问答--------------------
引用 20 楼 xianfajushi 的回复:
VB自用C#就没用过,试新建一个:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim list As List(Of Int16(,)) = New List(Of Int16(,))
        list.Add(New Int16(,) {{1, 2}, {3, 4}})
    End Sub

当然我用的是VS2010的


这个还真可以,想问一下如何取数呢
就是如何表示,如上面的的数字3
第一列第一个元素 --------------------编程问答--------------------
引用 1 楼 u010031573 的回复:
试试:
 Dim dic As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
        '00表示第一行第一列,01表示第一行第二列,依此类推,以他们作为key,将其对应的值存入
        dic.Add("00", "1")
        dic.Add("01", "h"c)
        dic.Add("02", 1)

        dic.Add("10", "2")
        dic.Add("11", "H"c)
        dic.Add("12", 2)
        MsgBox(dic("01").ToString()) '查找第一行第二列的值


用这个.... --------------------编程问答--------------------
引用 23 楼 u010031573 的回复:
Quote: 引用 1 楼 u010031573 的回复:

试试:
 Dim dic As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
        '00表示第一行第一列,01表示第一行第二列,依此类推,以他们作为key,将其对应的值存入
        dic.Add("00", "1")
        dic.Add("01", "h"c)
        dic.Add("02", 1)

        dic.Add("10", "2")
        dic.Add("11", "H"c)
        dic.Add("12", 2)
        MsgBox(dic("01").ToString()) '查找第一行第二列的值


用这个....



我一行有几列数呢,要怎么改写 --------------------编程问答--------------------
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim list As List(Of Int16(,)) = New List(Of Int16(,))
        Dim list1 As List(Of Int16(,,)) = New List(Of Int16(,,))
        list1.Add(New Int16(,,) {{{1, 2, 3}, {3, 4, 5}}})
        Dim datatable As DataTable = New DataTable()
        datatable.Columns.Add("a1")
        datatable.Columns.Add("a2")
        datatable.Columns.Add("a3")
        datatable.Columns.Add("a4")
        datatable.Rows.Add()
        datatable.Rows(0)(0) = "1"
        datatable.Rows(0)(1) = "2"
        datatable.Rows(0)(2) = "3"
        datatable.Rows(0)(3) = "4"
        list.Add({{datatable.Rows(0)(0), datatable.Rows(0)(1)}, {datatable.Rows(0)(2), datatable.Rows(0)(3)}})
        Dim 总 As Int16 = list(0)(0, 0) * list(0)(0, 1) * list(0)(1, 0) * list(0)(1, 1)
    End Sub

写了这些,如果还不会,该好好学基础了。 --------------------编程问答--------------------
引用 24 楼 zhangwei12354 的回复:
Quote: 引用 23 楼 u010031573 的回复:

Quote: 引用 1 楼 u010031573 的回复:

试试:
 Dim dic As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
        '00表示第一行第一列,01表示第一行第二列,依此类推,以他们作为key,将其对应的值存入
        dic.Add("00", "1")
        dic.Add("01", "h"c)
        dic.Add("02", 1)

        dic.Add("10", "2")
        dic.Add("11", "H"c)
        dic.Add("12", 2)
        MsgBox(dic("01").ToString()) '查找第一行第二列的值


用这个....



我一行有几列数呢,要怎么改写
 Dim dic As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
        Dim rows As Integer = 2  '行数
        Dim Columns As Integer = 2 '列数

        For j As Integer = 0 To rows
            For i As Integer = 0 To Columns - 1
                dic.Add(j.ToString & i.ToString(), Column.value)   'Column.value  列值
            Next
        Next

        MsgBox(dic("01").ToString()) '查找第一行第二列的值  --------------------编程问答-------------------- rows -1
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,