如何从下一个项目中减去每个项目? [英] How to subtract each item from next item?
本文介绍了如何从下一个项目中减去每个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个整数列表
Dim IntList作为列表(整数)= {5,7,8,10,15,24,26}
我希望减去下一项中的每一项,
(7-5),(8-7),(10-8),(15- 10),(24-15),(26-24)
ResultList = {2,1,2,5,9,2}
我尝试了什么:
我不知道。
Dim ResultList = IntList.Where(Function(x,i)New KeyValuePair(Byte,Integer)(x.Key,i))。ToList
解决方案
试试这个:
Sub Main()
Dim IntList As 列表( 整数)= 新 List( Of Integer )
IntList.Add( 5 )
IntList.Add( 7 )
IntList.Add( 8 )
IntList.Add( 10 )
IntList.Add( 15 )
IntList.Add( 24 )
IntList.Add( 26 )
{5,7,8,10,15,24,26}
Dim ResList 作为列表( 整数)= 新列表( < span class =code-keyword>整数)
对于 i = 0 IntList.Count - 2
ResList.Add(IntList(i + 1 ) - IntList(i))
下一步
结束 Sub
我找到了解决方案此处
Dim IntList As List(Of Integer)= {5,7,8,10,15,24,26} .ToList
Dim ResultList =(从x在IntList中让nextindex = IntList.IndexOf(x)+ 1让nextelement = IntList.ElementAt(If(nextindex = IntList.Count,nextindex - 1,nextindex))选择nextelement - x)。ToList()
result.RemoveAt(IntList.Count - 1)
ResultList = {2,1,2,5,9,2}
I have a list of integer
Dim IntList as list(of integer) = {5,7,8,10,15,24,26}
I want subtraction each item from next item,
(7-5),(8-7),(10-8),(15-10),(24-15),(26-24)
ResultList = {2,1,2,5,9,2}
What I have tried:
I have no idea.
Dim ResultList = IntList.Where(Function(x, i) New KeyValuePair(Of Byte, Integer)(x.Key, i)).ToList
解决方案
Try this:
Sub Main() Dim IntList As List(Of Integer) = New List(Of Integer) IntList.Add(5) IntList.Add(7) IntList.Add(8) IntList.Add(10) IntList.Add(15) IntList.Add(24) IntList.Add(26) '{5, 7, 8, 10, 15, 24, 26} Dim ResList As List(Of Integer) = New List(Of Integer) For i = 0 To IntList.Count - 2 ResList.Add(IntList(i + 1) - IntList(i)) Next End Sub
I found solution here
Dim IntList As List(Of Integer) = {5, 7, 8, 10, 15, 24, 26}.ToList Dim ResultList = (From x In IntList Let nextindex = IntList.IndexOf(x) + 1 Let nextelement = IntList.ElementAt(If(nextindex = IntList.Count, nextindex - 1, nextindex)) Select nextelement - x).ToList() result.RemoveAt(IntList.Count - 1) ResultList = {2,1,2,5,9,2}
这篇关于如何从下一个项目中减去每个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文