如何从 Android 中的 ArrayList 中获取最接近的双精度(double)值

huangapple go评论82阅读模式
英文:

How to get closest double values from ArrayList in Android

问题

我有一个名为valuelist的双精度值列表。我有一个edittext和一个按钮。当我按下按钮时,它将显示来自valuelist的最接近edittext的双精度值。怎么做呢?顺便说一下,我是新手应用开发者,不知道要使用哪种方法 如何从 Android 中的 ArrayList 中获取最接近的双精度(double)值

我的代码如下:

List<Double> list = new ArrayList<Double>();
list.add(1.38);
list.add(2.56);
list.add(4.3);

EditText input = (EditText)findViewById(R.id.etinput);
Button convert = (Button)findViewById(R.id.btconvert);
TextView result = (TextView)findViewById(R.id.tvresult);

double ivalue = String.valueOf(result.getText());

所以,如果ivalue是2.5,那么当我按下按钮时,它将将result设置为2.56,因为它是数组列表中最接近的双精度值。
提前致谢。

英文:

I've a list of double values named valuelist. I have a edittext and a button . when I press the button it will show the closest double value of edittext from valuelist.how to do that ?btw I'm new to app development and dont know any method to use 如何从 Android 中的 ArrayList 中获取最接近的双精度(double)值

And my codes are:-

List&lt;Double&gt; list = new ArrayList&lt;Double&gt;();
    list.add(1.38);
    list.add(2.56);
    list.add(4.3);

EditText input = (EditText)findViewById(R.id.etinput);
Button convert = (Button)findViewById(R.id.btconvert);
TextView result = (TextView)findViewById(R.id.tvresult);

double ivalue = String.valueOf(result.getText());

so if ivalue is 2.5 then when I press the button it will set result to 2.56 as it is the nearest double value of array list.
Thanks in advance.

答案1

得分: 0

你可以遍历列表,计算EditText值与当前列表项的差值,最小的差值将导致最接近EditText值的项。

List<Double> list = new ArrayList<Double>();
list.add(1.38);
list.add(2.56);
list.add(4.3);

EditText input = (EditText) findViewById(R.id.etinput);
Button convert = (Button) findViewById(R.id.btconvert);
TextView result = (TextView) findViewById(R.id.tvresult);

convert.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        double ivalue = Double.parseDouble(input.getText().toString());
        double difference = Double.MAX_VALUE;
        double nearestItem = Double.MAX_VALUE;
        for (double item : list) {
            if (difference > Math.abs(ivalue - item)) {
                difference = Math.abs(ivalue - item);
                nearestItem = item;
            }
        }

        result.setText(String.valueOf(nearestItem));
    }
});

确保处理EditText中非double类型的输入值。

英文:

You can iterate over the list and get the difference between the EditText value and the current list item, and the lowest difference value will lead to the nearest item to the EditText value.

List&lt;Double&gt; list = new ArrayList&lt;Double&gt;();
list.add(1.38);
list.add(2.56);
list.add(4.3);

EditText input = (EditText) findViewById(R.id.etinput);
Button convert = (Button) findViewById(R.id.btconvert);
TextView result = (TextView) findViewById(R.id.tvresult);


convert.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        double ivalue = Double.parseDouble(input.getText().toString());
        double difference = Double.MAX_VALUE;
        double nearstItem = Double.MAX_VALUE;
        for (double item : list) {
            if (difference &gt; Math.abs(ivalue - item)) {
                difference = Math.abs(ivalue - item);
                nearstItem = item;
            }
        }

        result.setText(String.valueOf(nearstItem));

    }
});

Make sure to handle the non-double input values to the EditText

huangapple
  • 本文由 发表于 2020年5月5日 01:53:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/61598546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定