如何索引列表列表? [英] How do I index into a list of lists?

查看:93
本文介绍了如何索引列表列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个用于创建部件号的数据库,并且正在努力访问列表列表中的字符串。我有一个.txt文件,我试图从列表列表中加载信息。第一行包含第一个可用的项目编号(如果添加了新项目)并存储为 newProjNum 。之后文件中的每一行都是一个列表,其中包含各个项目的信息,如下所示(其中第一个可用考虑现有的部件号并返回最低的未使用的数字,以便没有数字是多余的):



PI,项目名称,项目编号,第一个可用的装配编号,第一个可用的零件编号



我还声明了变量全局文件如下:



I am trying to make a database for creating part numbers and am struggling to access strings in a list of lists. I have a .txt file that I am trying to load information from and into a list of lists. The first line contains the first available project number (in case a new project is added) and gets stored as newProjNum. Each line in the file after that is a list that contains information for individual projects as follows (where "first available" takes into account existing part numbers and returns the lowest unused number so that no number is redundant):

PI, Project name, Project number, first available assembly number, first available part number

I have also declared variables in a global file as follows:

Public Property projPartNumInfo As New List(Of String)
Public Property allPartNumInfo As New List(Of List(Of String))
Public Property NewProjNum As Integer





我的第一张支票通过并打印出 projPartNumInfo 列表中的五个字符串中的每一个,通过消息框连接在一起。一旦我将 projPartNumInfo 列表添加到 allPartNumInfo 列表列表中,我似乎无法再次检索相同的字符串,虽然。我尝试以三种不同的方式实现check 2,但无法访问 allPartNumInfo 中的信息。版本1执行消息框命令,但有4个新行字符,仅此而已 - 它在每个行中没有看到任何 / code>所以它从第二个for循环中得不到任何东西。版本2和3抛出错误,表示我在尝试索引到 allPartNumInfo 列表列表时索引超出范围。





My first check passes and prints out each of the five strings from the projPartNumInfo list, concatenated together, through a message box. I can't seem to retrieve those same strings again once I add the projPartNumInfo lists into the allPartNumInfo list of lists, though. I have tried implementing check 2 in three different ways and can't access the information in allPartNumInfo. Version 1 executes the message box command but with 4 new line characters and nothing more--it doesn't see any item's in each line so it gets nothing from the second for-loop. Versions 2 and 3 throw errors saying that the index is out of range as soon as I try and index into the allPartNumInfo list of lists.

Private Sub loadAllPartNumInfo()
        'code used from https://msdn.microsoft.com/en-us/library/cakac7e6.aspx and altered

        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(partNumTextFile)
            MyReader.TextFieldType = FileIO.FieldType.Delimited
            MyReader.SetDelimiters(",")
            Dim currentRow As String()
            While Not MyReader.EndOfData
                Try
                    currentRow = MyReader.ReadFields()
                    Dim currentField As String
                    For Each currentField In currentRow
                        If currentRow.Length = 1 Then
                            NewProjNum = currentField
                            Exit Try
                        Else
                            projPartNumInfo.Add(currentField)
                        End If
                    Next
                    '
                    'Check 1-checks whether proper strings are being added to projPartNumInfo list
                    '       -should print out "PI,project,projectNumber,firstAvailableAssemblyNum,firstAvailablePartNum"
                    '
                    'Dim result As String = ""
                    'For Each elem As String In projPartNumInfo
                    '    result &= elem & ","
                    'Next
                    'MsgBox(result)
                    '
                    'Check 1 cleared, prints four lines with five strings properly
                    '
                    allPartNumInfo.Add(projPartNumInfo)
                    projPartNumInfo.Clear()
                Catch ex As Microsoft.VisualBasic.
                            FileIO.MalformedLineException
                    MsgBox("Line " & ex.Message &
                    "is not valid and will be skipped.")
                End Try
            End While
        End Using
        '
        'Check 2-checks whether proper projPartNumInfo lists are being added to allPartNumInfo list of lists
        '       -should print out same as debugger above, but with multiple lines for different projects, separated by a new line
        '
        'Version 1:
        'Dim sresult As String = ""
        'For Each line As List(Of String) In allPartNumInfo
        '    For Each item As String In line
        '        sresult &= item
        '    Next
        '    sresult &= vbNewLine
        'Next
        'MsgBox(sresult)
        '
        '
        'Version 2
        'Dim sresult2 As String = ""
        'Dim line As List(Of String) = Nothing
        'For x As Integer = 1 To 4
        '    line = allPartNumInfo(x)
        '    For y As Integer = 1 To 5
        '        Dim item As String = line(y)
        '        sresult2 &= (item & ",")
        '        item = ""
        '    Next
        '    sresult2 &= vbNewLine
        'Next
        'MsgBox(sresult2)
        '
        '
        'Version 3
        'Dim sresult3 As String = ""
        'For x As Integer = 1 To 4
        '    For y As Integer = 1 To 5
        '        sresult3 &= (allPartNumInfo(x)(y) & ",")
        '    Next
        '    sresult3 &= vbNewLine
        'Next
        'MsgBox(sresult3)
        '
        '
        'Check 2, Version 1 2 and 3 Failed
        '
    End Sub



有什么建议或帮助吗?如果有什么不清楚,请告诉我。提前感谢所有答案。


Any suggestions or help? Let me know if anything isn't clear. Thanks in advance for all answers.

推荐答案

您甚至没有尝试在代码中对其进行索引。

这是给你的提示:列表列表仍然是一个列表,与任何其他元素类型没有区别。索引列表列表时,将获得对此列表元素的引用。谁在乎这个元素也是一个列表?你得到了它的参考。然后,当你有一个内部列表对象的引用时,你可以索引它以获得对它的元素的引用,在你的例子中,是一个字符串。



只记得列表在逻辑上是一个 jugged数组。这意味着每个内部列表具有不同的长度。在尝试通过索引获取任何内容之前,您需要检查 Count 属性:https://msdn.microsoft.com/en-us/library/27b47ht3(v=vs.110).aspx [ ^ ]。



你真的需要学习如何自己编写代码。在提问时,请尝试显示 您尝试了什么



为什么我这次不写任何代码?原因如下: 给一个人一条鱼...... 。 />
感谢您的努力和理解。现在轮到你了。



-SA
You did not even try to index it in your code.
Here is the hint for you: the list of lists is still a list, no different from any other element types. When you index a list of lists, you get a reference to the element of this list. And who cares that this element is also a list? You got a reference to it. And then, when you have a reference to an "inner" list object, you can index it to get a reference to its element, in your case, a string.

Only remember that to list is logically a jugged array. It means that each of the "inner" lists has different length. You need to check the Count property before you try to get anything by the index: https://msdn.microsoft.com/en-us/library/27b47ht3(v=vs.110).aspx[^].

You really need to learn how to write code by yourself. When asking question, try to show what have you tried so far.

Why I'm not writing any code for your this time? Here is why: Give a man a fish….
Thank you for your effort and understanding. This is your turn now.

—SA


就像谢尔盖写的那样在解决方案1中。

如果您有List of List,那么您的外部List.Item将返回一个表示内部List的对象。不多也不少......

如果你想要走过,那么也许它是这样的:

It is like Sergey has written and explained in Solution 1.
If you have a List of List then your outer List.Item returns an object which represents the inner List. No more and not less ...
If you want to walk through then perhaps it works like this :
public InnerList as List (of string)
public OuterList as List (of InnerList)

public Sub MySearcher

dim myList as List (of string)

for i as integer = 1 to OuterList.count
   myList = OuterList.Item(i-1)
   for j as integer = 1 to myList.count
      ' and now do what you are interested to do inside the InnerList
   next
next

end sub





我希望它有所帮助......



I hope it helps ...


这篇关于如何索引列表列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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