Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何找到数组中每个元素的最近值?_C#_Arrays_List - Fatal编程技术网

C# 如何找到数组中每个元素的最近值?

C# 如何找到数组中每个元素的最近值?,c#,arrays,list,C#,Arrays,List,我想找出这个元素最接近的值之间的范围。 元素之间的增量值。它是正数,因为它的模量 class Element { double DeltaValue; double ElementValue; public Element(double n) { ElementValue = n; } static void Main() { list<Element> ListElements = new list<Elements>; ListElement

我想找出这个元素最接近的值之间的范围。 元素之间的增量值。它是正数,因为它的模量

class Element {


double DeltaValue;


double ElementValue;


public Element(double n) {

ElementValue = n;

}

static void Main() {


list<Element> ListElements = new list<Elements>;


ListElements.Add(3);

ListElements.Add(10);

ListElements.Add(43);

ListElements.Add(100);

ListElements.Add(30);

ListElements.Add(140);

for(int i = 0; i < ListElements.Count; i++) { 

ListElements[i].DeltaValue = //and problem is here 


//example as for ListElements[2].DeltaValue will be 13; because 43-30=13;

} 
类元素{
双三角值;
双元素值;
公共元素(双n){
ElementValue=n;
}
静态void Main(){
列表元素=新列表;
增加(3);
增加(10);
增加(43);
增加(100);
增加(30);
增加(140);
对于(int i=0;i

//列表元素的示例[2].DeltaValue将是13;因为43-30=13;

只需按递增顺序对数组排序,当前元素的上一个元素和下一个元素之间的最小差异将解决您的问题。对于最后一个元素,您只需查看其上一个元素的差异。

应该能够通过以下:

public static int GetClosestVal(this int[] values, int place)
{
    return values.OrderBy(v => Math.Abs(v - values[place])).ToArray()[1];
}
以下输出
30

var testArray = new [] {3, 10, 43, 100, 30, 140};
Console.Write(testArray.GetClosestVal(2));
基本上说,您按照每个项目与所选项目之间的绝对差异进行排序,然后抓取列表中的第二个项目,因为第一个项目始终是项目本身(因为n-n=0)


因此,排序后的列表应该是
[43,30,20,3,100,140]

我不确定我是否正确理解您的问题。如果我理解了,那么下面的代码片段可以帮助您:

class Program
{
    static void Main(string[] args)
    {
        Elements ListElements = new Elements();

        ListElements.ElementValue.Add(3);

        ListElements.ElementValue.Add(10);

        ListElements.ElementValue.Add(43);

        ListElements.ElementValue.Add(100);

        ListElements.ElementValue.Add(30);

        ListElements.ElementValue.Add(140);


        ListElements.CreateDeltaValues();


        for (int i = 0; i < ListElements.DeltaValue.Count; i++)
        {

            Console.WriteLine("ListElement["+i+"]: " + ListElements.DeltaValue[i]);


            //example as for ListElements[2].DeltaValue will be 13; because 43-30=13;

        }
        Console.ReadKey();
    }
}

public class Elements
{
    public List<double> DeltaValue = new List<double>();
    public List<double> ElementValue = new List<double>();

    public void CreateDeltaValues()
    {
        this.ElementValue.Sort();

        for (int i = 1; i < this.ElementValue.Count; i++)
        {
            var deltaValue = this.ElementValue[i] - this.ElementValue[i-1];
            this.DeltaValue.Add(deltaValue);
        }
    }
}
类程序
{
静态void Main(字符串[]参数)
{
Elements ListElements=新元素();
liselements.ElementValue.Add(3);
liselements.ElementValue.Add(10);
liselements.ElementValue.Add(43);
liselements.ElementValue.Add(100);
liselements.ElementValue.Add(30);
liselements.ElementValue.Add(140);
CreateDeltaValues();
for(int i=0;i
这是一个控制台应用程序,但此代码也适用于其他应用程序模型

此代码生成以下输出:


你想过对数组进行排序吗?@Sidias Korrado哦,不,我以前从未使用过它当然是的,肖恩,但我刚刚开始,没有足够的报告可以向上投票;)你可以将我的答案标记为正确。你只需点击向下和向上投票下的钩子。因为你是新来的,你应该进行这个小旅行。它将帮助你理解而且stackoverflow稍微好一点。在你完成这次旅行后,你会赚一点钱。你只需要3分钟就可以完成这次旅行。