简体   繁体   English

vb.net获取数组的第一行和最后一行的“值”

[英]vb.net get the *values* of the first and last lines of an array

I have an array of numbers like this: 我有一个这样的数字数组:

{10 20 30 40 50 60 70 80 90}

The start and end numbers will not always be the same (in fact none of them will). 起始编号和结束编号不一定总是相同(实际上,它们都不一样)。

I want to get the value (not the index) of the first and last entries. 我想获取第一个和最后一个条目的 (而不是索引)。

So in the above example, all I'm interested in getting are the values "10" and "90". 因此,在上面的示例中,我感兴趣的是获取值“ 10”和“ 90”。

This array is being constructed from a comma delimited text file which looks like this: 这个数组是用逗号分隔的文本文件构造的,如下所示:

10,3.456
20,3.588
30,3.640
40,3.790
50,3.850
60,3.680
70,3.480
80,3.280
90,3.765

This is my code: 这是我的代码:

    Dim arrName, arrValue As New List(Of String)()
    Dim sdata() As String
    Dim column1, column2 As Decimal

For Each line As String In IO.File.ReadAllLines("c:\file.txt")
    If line <> "" And Not line.StartsWith("#") Then
        sdata = line.Split(","c)
        arrName.Add(sdata(0).Trim())
        arrValue.Add(sdata(1).Trim())
        ' get data from array
        column1 = sdata(0)
        column2 = sdata(1)
End If
Next

So what I want is to be able to create two variables where: 所以我想要的是能够创建两个变量,其中:

column1Lowest=<the lowest value> (in this case 10)
column1Highest=<the highest value> (in this case 90)

I have tried several method, but all I seem to be getting back is the entire array each time, or "-1" (no match found in array I know). 我尝试了几种方法,但每次返回似乎都是整个数组,即“ -1”(在我知道的数组中找不到匹配项)。

Things I have tried: IndexOf(), FirstIndexOf(),LastIndexOf() 我尝试过的事情:IndexOf(),FirstIndexOf(),LastIndexOf()

I am only interested in the lowest an highest values in column1 (but I still use the rest elsewhere). 我只对column1中的最低值和最高值感兴趣(但是我仍然在其他地方使用其余值)。

I am now out of ideas. 我现在没主意了。

Please help - with answers in the form that a novice can understand. 请提供帮助-以新手可以理解的形式提供答案。

Thanks. 谢谢。

EDIT: 编辑:

To add to Steve's answer below, I tried the Inumerable strings option but I believe I was running into issues with my file format - some files were fine - some were not (usually files without a line feed after the last line of data!). 为了补充下面的史蒂夫答案,我尝试了无穷字符串选项,但我认为我的文件格式遇到了问题-有些文件很好-有些则没有问题(通常在最后一行数据后没有换行的文件!)。

This is my code: 这是我的代码:

Dim Lines As Collections.Generic.IEnumerable(Of String) = File.ReadLines("c:\file.txt").Where(Function(q) q.StartsWith("#") = False)
        Line0 = Lines.FirstOrDefault
        LineN = Lines.LastOrDefault
       lowestNum = Line0.Split(New Char() {","c})
        highestNum = LineN.Split(New Char() {","c})
        lowNum = lowestNum(0)
        highNum = highestNum(0)

etc.. 等等..

So if I wanted to use this chunk of code on my already parsed file - how would I use the existing array with it? 因此,如果我想在已经解析的文件上使用这段代码,我将如何在现有的数组上使用它?

I have just tried: 我刚刚尝试过:

Dim Lines As Collections.Generic.IEnumerable(Of String) = {column1}
                FirstValue = Lines.FirstOrDefault
                LastValue = Lines.LastOrDefault

But this still prints the entire array? 但这仍会打印整个数组吗?

To display the output I'm using: 要显示我正在使用的输出:

    ListBox2.Items.Add(FirstValue)
    ListBox1.Items.Add(LastValue)

I've tried inserting the listbox code both in and outside of the "For Each" loop. 我尝试在“ For Each”循环的内部和外部插入列表框代码。 Putting them outside gives me variable issues - so I tried to declare the variables outside of the loop to no avail. 将它们放在外面会给我带来变量问题-因此我试图在循环外声明变量无济于事。

I did say I was a novice! 我确实说我是新手!

There are these pretty IEnumerable extensions that you can use 您可以使用这些漂亮的IEnumerable扩展

Dim first = numbers.First()
Dim last = numbers.Last()

However from your question is not clear if you want just the first and last element of the array or the highest and lowest value in the array. 但是,从您的问题尚不清楚,是只需要数组的第一个和最后一个元素,还是数组中的最高和最低值。

For the second case you have 对于第二种情况

Dim high = numbers.Max()
Dim low = numbers.Min()

I have also noticed that you use strings to manage these values, but if they are numbers and you want to retrieve the highest and lowest numbers then you should absolutely convert these values to integers otherwise you will have a difficult time to explain to your computer that a string "100" is higher than a string "20". 我还注意到,您使用字符串来管理这些值,但是如果它们是数字,并且您想要检索最大和最小的数字,则应将这些值绝对转换为整数,否则您将很难向计算机说明字符串“ 100”高于字符串“ 20”。

EDIT 编辑
Loop over your lines and convert every line to a decimal then get the min and max (Note, no check on the correctness of the input) 循环遍历行并将每行转换为十进制,然后获得最小值和最大值(注意,不检查输入的正确性)

Dim names = new List(Of Decimal)()
Dim values = new List(Of Decimal)()
For Each line As String In IO.File.ReadAllLines("c:\file.txt")
    If line <> "" And Not line.StartsWith("#") Then
        sdata = line.Split(","c)
        names.Add(Convert.ToDecimal(sdata(0).Trim()))
        values.Add(Convert.ToDecimal(sdata(1).Trim()))
    End If
Next
Dim lowValue = names.Min()
Dim highValue = names.Max()

The first element in a list or array always has index 0 and the last element always List.Count - 1 or Array.Length - 1 depending on whether you use an array or a list. 列表或数组中的第一个元素始终具有索引0,而最后一个元素始终为 List.Count - 1Array.Length - 1具体取决于您使用数组还是列表。 So you get the first and the last value from your array using: 因此,您可以使用以下命令从数组中获取第一个和最后一个值:

arrayLowest = arrayVariable(0)
arrayHighest = arrayVariable(arrayVariable.Length - 1)
listLowest = listVariable(0)
listHighest = listVariable(listVariable.Count - 1)

The other methods you mention ( IndexOf() , FirstIndexOf() , LastIndexOf() ) are used to find the index of a given element. 您提到的其他方法( IndexOf()FirstIndexOf()LastIndexOf() )用于查找给定元素的索引。 For example if you wanted to know at which position the element 90 is in the list/array, you'd use these. 例如,如果您想知道元素90在列表/数组中的哪个位置,则可以使用这些元素。

Please note that an array or list are not sorted by default, so if you want to get the minimum and maximum values from your array or list you need to either use the extensions Steve mentioned, or sort the list/array and then get the first and last value, or loop over the list/array and perform comparisons. 请注意,默认情况下不对数组或列表进行排序,因此,如果要从数组或列表中获取最小值和最大值,则需要使用Steve提到的扩展名,或者对列表/数组进行排序,然后再获取第一个和最后一个值,或者循环遍历列表/数组并执行比较。

Try this to get the first and last values: 尝试此操作以获取第一个和最后一个值:

Dim FirstValue As String = sdata(0)
Dim LastValue As String = sdata(sdata.Length - 1)

First make sure that you only try to access the elements if the array has a size > 0, then access the first and last element like this: 首先,请确保仅在数组大小大于0的情况下才尝试访问元素,然后像这样访问第一个和最后一个元素:

Dim lowest, highest As String;
If arrValues.Length > 0 Then
  lowest = arrValues(0);
  highest = arrValues(arrValues.Length - 1);
End If

Array.Length returns the number of elements, as elements are indexed starting from 0, the last element will be Length - 1 . Array.Length返回元素的数量,因为从0开始索引元素,最后一个元素将是Length - 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM